Java design mode Template Method mode template Method, design mode Template
Design pattern is a set of summary of code Design experiences that are repeatedly used, known to most people, classified and catalogued. The design pattern is used to make code reusable, make it easier for others to understand, and ensure code reliability. There is no doubt that the design model is a win-win solution for others and the system. The design model enables code compilation to be truly engineered. The design model is the foundation of software engineering, just like the building blocks. The rational use of the design pattern in the project can perfectly solve many problems. Each pattern has a corresponding principle in the present, each pattern describes a recurring problem around us and its core solution, which is also the reason for its wide application.
1. Classification of design patternsCreation Mode:
- Singleton, Singleton mode: ensures that a class has only one instance and provides a global access point to it.
- Abstract Factory, Abstract Factory: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
- Factory Method, Factory Method: defines an interface for creating objects, so that the subclass determines which class to instantiate. Factory Method delays the instantiation of a class to the subclass.
- Builder: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
- Prototype: Use a Prototype instance to specify the type of the object to be created, and copy the Prototype to create a new object.
Behavior mode:
- Iterator, Iterator mode: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
- Observer, Observer mode: defines the one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are automatically updated by notification.
- Template Method, Template Method: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass, templateMethod allows the subclass to redefine a specific step without changing the structure of an algorithm.
- Command: encapsulate a request as an object, so that you can use different requests to parameterize the customer, queue requests and record request logs, and supports unrecoverable operations.
- State: allows an object to change its behavior when its internal State changes. The object seems to have changed its class.
- Strategy: defines a series of algorithms, encapsulates them one by one, and enables them to replace each other. This mode allows algorithms to be independent of customers who use them.
- China of Responsibility, Responsibility chain mode: Enables multiple objects to process requests, so as to avoid coupling between the request sender and receiver
- Mediator: uses an intermediary object to encapsulate object interaction of some columns.
- Visitor, Visitor mode: indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on this element without changing the element classes.
- Interpreter: Specifies a language, defines a representation of its grammar, and defines an Interpreter. This Interpreter uses this representation to explain sentences in the language.
- Memento: captures the internal state of an object without interrupting the object, and saves the State outside the object.
Structural Mode:
- Composite, combination mode: combines objects into a tree structure to represent the relationship between parts of the whole. Composite makes the use of a single object and a combination object consistent.
- Facade: provides a consistent interface for a group of interfaces in the subsystem. fa? Ade provides a high-level interface, which makes the subsystem easier to use.
- Proxy: provides a Proxy for other objects to control access to this object.
- Adapter: the Adapter mode converts a class of interfaces to another interface that the customer wants. The Adapter mode makes those classes unable to work together due to interface incompatibility.
- Decrator: add some additional responsibilities to an object dynamically. In addition, the Decorator mode is more flexible than the subclass generation mode.
- Bridge: separates abstract parts from their implementations so that they can change independently.
- Flyweight, Meta Mode
2. Template Method
Overview The template method mode is the behavior mode of the class. Prepare an abstract class, implement part of the logic in the form of a specific method and a specific constructor, and then declare some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. This is the intention of the template method mode. For example, define the skeleton of an algorithm in an operation and delay the step to the subclass. The template method allows subclass to redefine certain steps of an algorithm without changing the structure of an algorithm. Role abstract class in the mode: implements the template method and defines the skeleton of the algorithm. ConcreteClass: implements the abstract methods in the abstract class and completes the complete algorithm. Design Example
1 // abstract template 2 public abstract class AbstractClass 3 {4 protected abstract void doAnyting (); 5 6 protected abstract void doSomething (); 7 8 public void templateMethod () 9 {10 doAnyting (); 11 doSomething (); 12} 13} 14 15 public class ConcreteClass1 extends AbstractClass16 {17 @ Override18 protected void doAnyting () 19 {20 System. out. println ("do class1 anything"); 21} 22 23 @ Override24 protected void doSomething () 25 {26 System. out. println ("do class1 something"); 27} 28} 29 30 public class ConcreteClass2 extends AbstractClass31 {32 @ Override33 protected void doAnyting () 34 {35 System. out. println ("do class2 anything"); 36} 37 38 @ Override39 protected void doSomething () 40 {41 System. out. println ("do class2 something"); 42} 43} 44 45 public class Client46 {47 public static void main (final String [] args) 48 {49 final AbstractClass c1 = new ConcreteClass1 (); 50 final AbstractClass c2 = new ConcreteClass2 (); 51 c1.templateMethod (); 52 c2.templateMethod (); 53} 54}