Java Design Patterns--Iterator Pattern Iterator, Memento Pattern Memento, Mediator Pattern Mediator

Iterator Mode Iterator
    Collection classes in JDK have iterator() method, which returns a subclass of Iterator interface. Different collections implement Iterator differently. In this way, the method of accessing the collection is uniformly defined for different collections. You can look at the source code.


Memento Mode Memento
    Memento mode is also known as Token mode. Without breaking encapsulation, capture the internal state of an object and save that state outside of the object, so that the object can be restored to the previously saved state later. The disadvantage is that it consumes a lot of money. If there are a lot of internal states, saving a copy will not waste a lot of memory. For
    example, there are scope="request" or scope="session" features in jsp, and the data of the form is copied in the verification process. In the corresponding scope, the form data can be backfilled when the validation fails.
    
Mediator mode Mediator
    uses a mediator object to encapsulate a series of behaviors related to object interaction to reduce the coupling between classes. The interaction between objects depends on the behavior of each other. Modifying one behavior may involve the behavior of many other objects. Through the Mediator mode, only the relationship between the object and the Mediator is concerned, so that the many-to-many relationship is modified into a one-to-many relationship. .
    Code model:
    class Mediator{
        Class1 c1;
        Class2 c2;
    }
    class Class1{
        Mediator m;
    }
    class Class2{
        Mediator m;
    }
    Each member must know the Mediator and contact the Mediator, not other members. Class1 and Class2 have no direct relationship and are associated through Mediator.
    The View Controller in MVC is a kind of Mediator, which is the Mediator between Jsp and the application on the server.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326932607&siteId=291194637