Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of the iterative submodel of python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what is the function of the iterative sub-model of python", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the function of the iterative sub-model of python"?

Iterative subpatterns can sequentially access the elements within the collection without knowing the internal representation of the collection.

Multiple objects are gathered together to form the concept of collection, so collection objects are also called containers, just like pools that contain n multi-objects. Collection objects need to provide methods so that internal objects can be accessed sequentially. The common problems of collection objects are divided into two categories: one is to convert one collection object into another collection object, because the collection objects traverse the collection differently, it is necessary to modify the client code (contrary to the opening and closing principle); second, the collection itself is unchanged and the iterative method changes, so it is necessary to modify the collection object. Both of these problems involve the need to modify the code, which violates the opening and closing principle (the function can be extended without modifying the code, which actually needs to separate the immutable from the changeable). In order to solve this problem, an iterative sub-layer is added between the client and the collection object, which makes the coupling between the client and the collection object change from direct to indirect.

The class diagram of the iterative subpattern is roughly as follows

Aggregate collection: an interface for creating iterators

ConcreteAggregate concrete collection: implementing iterative subinterfaces

Iterator iterative subinterface: gives the interface to iterate over each element

ConcreteIterator concrete iterator: implement the iterative method.

If a collection object provides a method to modify internal elements, then this interface is called wide interface. If you do not provide a method to modify the element, it is called a narrow interface. In fact, I think it doesn't matter. The key is that the collection class provides a modification interface, which breaks the encapsulation of the collection object. At this time, the iterator controls the iteration of the element externally. The function is equivalent to a cursor, which is called a cursor iterator. The improved approach is that the collection object does not provide a method to modify the element, but only provides a wide interface to the iterator.

Let's use the code to illustrate it more vividly.

The collection class provides several methods, the first is to get the iterator, the second is to get the elements at the specified location, and the third is to get the number of collection elements

Public abstract class Aggregate {public abstract MyIterator iterator (); public abstract Object getElement (int index); public abstract int size ();}

For the implementation of the specific collection class, an array is used as the static internal element. If the dynamic external element is used, it needs to be modified.

Public class ConcreteAggregate extends Aggregate {private String [] arr= {"A", "B", "C"}; @ Override public MyIterator iterator () {return new ConcreteIterator (this);} public Object getElement (int index) {return arr [index];} public int size () {return arr.length;}}

Abstract iterator

Public interface MyIterator {/ / move to the first object public void first (); / / whether the last public boolean isLast (); / / move the next public void next (); / / current object public Object current ();}

A concrete implementation

Public class ConcreteIterator implements MyIterator {Aggregate agg; int size=0; int index=0; public ConcreteIterator (Aggregate agg) {this.agg=agg; size=agg.size ();} @ Override public void first () {index=0;} @ Override public boolean isLast () {return index > = size;} @ Override public void next () {if (index

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report