Summary of Java Design Patterns

Our section 5.3 talked about the java design pattern. I actually listened very confusedly in the class, so after the class, I went down to read the courseware and searched for the materials, and sorted out this section so that I could have a clearer understanding of this section.

 

Adapter :

One sentence describes the feeling of the adapter mode: src->Adapter->dst , that is, src is given to the Adapter in a certain form (three forms corresponding to three adapter modes), and finally converted into dst.


Class adapter mode : The Adapter class, by inheriting the src class, implements the dst class interface ( implementation and inheritance are completed at the same time ) , and completes the adaptation of src->dst.

Eg:public class Adapter extends src implements dst

Because this requires that dst must be an interface, there are certain limitations;
and the methods of the src class will be exposed in the Adapter, which also increases the cost of use.

But also because it inherits the src class, it can rewrite the methods of the src class according to the needs, which enhances the flexibility of the Adapter.

 

 

Adapter mode for objects : just modify the Adapter class, this time, instead of inheriting the src class, it holds an instance of the src class to solve the compatibility problem.
That is: hold the  src class, implement the dst class interface , and complete the adaptation of src->dst.

Eg:public class Adapter implements dst {

private src s;

......

}

 

Adapter mode of interface : When you do not need to implement all the methods provided by the interface, you can first design an abstract class to implement the interface, and provide a default implementation (empty method) for each method in the interface, then the subclass of the abstract class can Selectively override some methods of the parent class to achieve the requirements, it is suitable for a situation where an interface does not want to use all its methods . ( I don't really understand this part)

 

 

 

Decorator :

Allows adding new functionality to an existing object without changing its structure. A decorator can add its own behavior before or after the behavior of the delegated decoree to achieve a specific purpose .

That is: the original class is passed into the decoration class (multiple) as a parameter (the parent class of the decoration class is the implementation of the same interface as the original class, that is to say: the decoration class also needs to implement the content in the interface, but the decoration class is implemented by Delegate to the original class variable in the decorated class to implement the decorated methods and methods in the interface)

 

 

 

(It should be clear here that the decorator is the decoration of src, and the user is unaware that src has been decorated (the user's usage remains unchanged). After the object is adapted, the user's usage will still change.

 

 

Facade (Facade Mode):

The Facade pattern provides a unified interface for accessing a group of interfaces in a subsystem. Facades define a high-level interface to make the subsystem easier to use.

 

 

Facade : The unified interface to the outside of the system, the entry point for the client to connect to the subsystem functions.

SubSystem : There can be one or more subsystems at the same time, each subsystem is not a separate class, but a collection of classes. Each subsystem can be called directly by the client, or by the facade role. The subsystem does not know the existence of the facade. For the subsystem, the facade is just another client

SystemA , SystemB , and SystemC are three classes of the same level, and operationA , operationB , and operationC in Facade can delegate calls to the three of them at the same time, thereby realizing different functions.

 

 

Strategy (Strategy Mode):

Defines a series of algorithms, encapsulates each algorithm, and makes them interchangeable, allowing the algorithm to change independently of the client using it.

 

My understanding is to implement multiple algorithm classes by implementing the algorithm interface, and then use the interface as a variable type in the class used, and attach different implementation classes to the variable under different circumstances, so as to realize the algorithm that cannot be tolerated.

 

 

 

Template (template mode):

 Defining the skeleton of an algorithm in an operation, while deferring some steps to subclasses, template methods allow subclasses to redefine certain steps of the algorithm without changing the structure of the algorithm.

  The popular understanding is : to complete a thing, there are a fixed number of steps, but each step is different according to the object, and the implementation details are different; you can define a general method to complete the thing in the parent class, according to the completion event The required steps call the implementation method of each step. The concrete implementation of each step is done by subclasses.

 

 

 

AbstractClass : Implements the template method and defines the skeleton of the algorithm.

ConcreteClass : Implements abstract methods in abstract classes, that is, the specific implementation details of different objects.

 

I think this mode is the most commonly used mode now. In lab3 , we use this mode for different points, edges and graphs.

 

Iterator (iteration mode) :

The Iterable interface guarantees that objects that implement this interface are iteratively traversable (must have an Iterator iterator() method)

 

 

An implementation of the Iterator interface that enables the implementing class to use an explicit or implicit iterator class.

 

 

Let your collection class implement the Iterable interface, and implement your own unique Iterator class

Eg

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324597693&siteId=291194637