The Gof design pattern in javase
Here is a summary of the design patterns commonly used in javase.
What is design mode?
The solution that can be reused is design mode.
What are the common design patterns?
The common design patterns are GOF design patterns and Java EE design patterns.
What are the types of GOF design patterns?
-Object creation is the design pattern in the object creation process. Example: Singleton mode
-Structural type is a larger structure with more classes, more methods, and objects. Example: Adapter mode, Adorner mode
-Behavioral type involves the design pattern of behavior and algorithm. For example: Policy mode
The Gof design pattern in javase
-Iterative sub-pattern, policy mode in set
Iterative sub-pattern: In the collection, all collection interfaces and his sub-interfaces implement the Iterable interface, meaning that these classes can iterate. The iterative sub-pattern is to say that there is no need to know the underlying storage structure and storage details, and uniformly target the iterator interface for iterative iterations. For example: HashSet Bottom is a hash of the data structure, treeset the bottom is a two-tree data structure, ArrayList bottom is an array, LinkedList bottom is a list, but they can iterate, that is, do not need to know the underlying storage details, Unify the iterations for the iterator interface.
Strategy mode: The comparator interchange embodies the strategy mode. There are two scenarios for sorting in TreeSet. Scenario one is to implement the comparable interface in the entity class, overriding the CompareTo method, which is an intrusive approach, and the source code needs to be modified each time the comparison rule is modified, so this method is not recommended; second, create a comparator class, Implementing the comparator interface, overriding the Compare method, using this method can create a number of comparators, which need to use which comparison rules to pass into which comparator to treeset, the idea of this comparator interchange is the embodiment of the strategy model.
-IO in adapter mode, adorner mode
Adapter mode: There are many places in IO that show the adapter mode. Its principle is: The adapter mode has a super interface, there are many abstract methods on the Super interface, there is an implementation class only need to use a method, if you implement the Super interface directly, you need to implement all the methods, this code is very ugly At this time, the Super interface and the implementation class add an adapter to the abstract class, the consists adapter's abstract class to implement the Super interface, all methods are implemented, only the implementation class needs to use the method set to abstract, by the implementation class to inherit the adapter abstract class, This is the only time you need to implement the abstract class of the adapter, and this code is very elegant. In addition, the adapter design pattern is used in the servlet, and the servlet's super interface has five methods service, INIT, Desotry, Getservletinfo, Getservletconfig, If you inherit the servlet interface directly, you need to implement all of these five methods, and we generally only use the service method, so here the Servlet interface is implemented by Genericservlet, The idea of adapter mode is manifested by the abstraction of a service-only method to implement an abstract method that leaves it to us.
Adorner mode: When the closed stream in Io, only need to close the advanced stream, the low-level stream will be automatically called, where the source code reflects the adorner mode. The adornment mode is to decorate the original class and extend the function.
Welcome to Exchange Mr. Zhang [email protected]
The Gof design pattern in javase