Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

define

Provides a way to access elements of an aggregate object sequentially without exposing the internal representation of the object.

The advantages and disadvantages

Advantages: 1. It allows you to traverse an aggregate object in different ways. Iterators simplify aggregate classes. 3. You can have multiple traversals on the same aggregate. 4. In iterator mode, it is easy to add new aggregate classes and iterator classes without modifying the existing code.

Disadvantages: Since the iterator pattern separates the responsibility of storing data and traversing data, adding new aggregation classes needs to correspond to adding new iterator classes, and the number of classes increases in pairs, which increases the complexity of the system to some extent.

Usage scenarios

Access the content of an aggregate object without exposing its internal representation. 2. You need to provide multiple traversal methods for aggregate objects. Provide a unified interface for traversing different aggregation structures.

implementation

We’ll create an Iterator interface that describes navigation methods and a Container interface that returns iterators. The entity class that implements the Container interface is responsible for implementing the Iterator interface.

IteratorPatternDemo, our demo class uses the entity class NamesRepository to print Names stored as collections in NamesRepository.

Step 1

Create interface:

Iterator.java

public interface Iterator {

    boolean hasNext(a);

    Object next(a);
}
Copy the code

Container.java

public interface Container {
    Iterator getIterator(a);
}
Copy the code

Step 2

Create an entity class that implements the Container interface. This class has an inner class NameIterator that implements the Iterator interface.

NameRepository.java

public class NameRepository implements Container {

    private String names[] = {"1"."2"."3"."4"};

    @Override
    public Iterator getIterator(a) {
        return new NameIterator();
    }

    private class NameIterator implements Iterator {

        int index;

        @Override
        public boolean hasNext(a) {
            if (index < names.length) {
                return true;
            }
            return false;
        }

        @Override
        public Object next(a) {
            if (hasNext()) {
                return names[index++];
            }
            return null; }}}Copy the code

Step 3

Use NameRepository to get the iterator and print the name.

IteratorPatternDemo.java

public class IteratorPatternDemo {

    public static void main(String[] args) {
        NameRepository namesRepository = new NameRepository();

        for(Iterator iter = namesRepository.getIterator(); iter.hasNext();) { String name = (String)iter.next(); System.out.println("Name : "+ name); }}}Copy the code

Step 4

Execute the program and output the result:

Name : 1
Name : 2
Name : 3
Name : 4
Copy the code