Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/40031567
Here is a summary of these 23 design patterns today. We use design patterns to reuse code, make code easier to understand, and ensure code reliability. Of course the design pattern is not omnipotent. The actual problems in the project are also analyzed in detail.
We can't use design patterns, but in the process of analyzing problems. The idea of using a design pattern to achieve the desired effect is more advantageous than not using design patterns. Then we should consider using design patterns.
I. General classification of Design patterns
Created (Creator) mode (5 total): Singleton, prototype, builder, factory method, abstract factory.
Structure (Structure) mode (total 7 kinds): Adapter, agent, appearance, bridge, combination, enjoy yuan, decorator.
Behavioral (Behavior) mode (11 total): Strategy, Observer, template method, iterator, responsibility chain, command, memo, status, visitors, mediator, interpreter.
Second, the following talk about my understanding of these 23 design patterns
1. created (Creator) mode (5 total)
① single case (Singleton) is a design pattern that is often used.
In Java applications. Singleton objects are guaranteed to be in one JVM. Only one instance of the object exists. There are two main ways to realize a hungry man and lazy-type.
② Prototype (Prototype): The idea of this mode is to copy and clone an object as a prototype. Produces a new object similar to the original object. There are two main ways of realization: shallow copy and deep copy.
The key to shallow replication is super.clone (), while deep replication requires that the current object be written in the form of a binary stream and then read.
③ Builder: This model is a collection of products that are managed together. Integrating very versatile into a class, this class can create something more complex.
It is concerned with creating multiple parts of a compound object.
④ Factory Methods (Factory method): Call factory methods to produce objects (products).
It comes in 3 ways: 1) Normal Factory mode: It is to build a factory class. The creation of instances of some classes that implement the same interface). 2) Multiple factory method patterns: an improvement to the normal factory method pattern, assuming that a string error is passed in the normal factory method pattern. The object is not created correctly, and multiple factory method patterns provide multiple factory methods. Create objects individually. 3) Static Factory method mode: The method in the above multiple factory method mode is set to static, do not need to create an instance, directly call on it. Suppose you want to use the factory method pattern to be preferred: Static Factory method mode.
⑤ Abstraction Factory (abstract Factory): As the name implies, the factory is abstracted out, different factories produce different products.
2. structure Type (Structure) mode (total 7 kinds)
① Adapter (Adapter): Converting the interface of a class to the client's expectation is also represented by an interface that is intended to eliminate compatibility issues with classes caused by mismatched interfaces. There are three main categories: The adapter mode of the class, the adapter mode of the object, and the adapter mode of the interface.
1) class Adapter mode: When you want to convert a class to a class that satisfies a new interface, you can use the class's adapter pattern, create a new class, inherit the original class, and implement the new interface. 2) The adapter mode of the object: When you want to convert an object to an object that satisfies a new interface, you can create a adapter class that holds an instance of the original class, and in the method of the adapter class, the method that invokes the instance is available.
3) interface Adapter mode: When you do not want to implement all the methods in an interface, you can create an abstract class adapter implement all methods when we write other classes. Inheriting an abstract class is possible.
② Proxy: Proxy mode is actually more than one proxy class out, for the original object to do some operations. For example, I sometimes need a lawyer for a lawsuit. As the lawyer has expertise in the law, can operate for me to express our ideas, this is the meaning of the agent. There are two ways to do this: static proxies (not using the methods inside the JDK), dynamic proxies (Invocationhandler and proxies).
③ appearance (facade): Also known as façade mode.
The appearance pattern is intended to solve the dependency between classes and classes. Like spring, you can configure the relationship between classes and classes into a configuration file, and the appearance pattern is to put their relationship in a facade class. The coupling between class classes is reduced, and the interface is not involved in the pattern.
④ Bridging (bridge): Separating things from their detailed implementations (abstraction and decoupling) so that they can change independently of each other. Bridging mode is actually a thought that transforms n*m into a n+m combination.
⑤ combination (Composite): The combination mode is sometimes called part-the overall mode. Combines objects into a tree structure to represent the "partial-overall" hierarchy.
⑥ (Flyweight): Use shared technology to effectively support a large number of fine-grained objects. The main purpose is to realize the sharing of objects. That is, the shared pool. It can reduce the overhead of memory when there are many objects in the system. In a way. You can think of a singleton as a special case of the element of enjoyment.
⑦ Decorator (Decorator): Dynamically attaches responsibility to an object to extend functionality. Decorators offer an alternative that is more resilient than inheritance. Keep the interface. Enhanced performance.
Behavioral (Behavior) mode (total 11 types)
The ① Policy (strategy) allows the user to choose a method to run an action, that is, the user can choose a different policy to operate. Personally think that the strategy model can use this formula: different XXX has different xxx for the user to choose. For example: different chess pieces have different ways for the user to choose.
② Observer (Observer): Defines a one-to-many dependency between objects. This way, when an object changes state, the object that relies on it will be notified and be proactive with the new. Java has provided a default implementation of the Observer observer pattern, and Java's support for the Observer pattern is now observable classes and observer interfaces.
③ Template method: A skeleton of an algorithm is defined in a method, and some steps are deferred to subclasses. The template method allows subclasses to define some of the steps in the algorithm again without changing the structure of the algorithm.
In short: The template method defines the steps of an algorithm and agrees that the subclass provides the implementation for one or more steps.
④ iterators (Iterator): Provides a way to access individual elements in an aggregated object in a sequential manner. Without revealing their internal representations.
⑤ Chain of Responsibility (chainofresponsibility): There are multiple objects, each holding a reference to the next object. A chain is formed, and the request is passed on the chain until an object decides to process the request, but the issuer is not sure which object is finally handling the request.
⑥ command: Encapsulates a request (command/password) into an object to use a different request, queue, or log to count its objects. The command mode also supports undo operations.
⑦ Memo (Memento): The main purpose is to save a state of an object so that it can be restored at the appropriate time.
⑧ state: The Consent object changes its behavior when the internal state changes, and the object looks as if it has changed its class. State mode It is a behavior that behaves differently to a state than to a different state, which in fact is an extension of the statement of Switch case.
⑨ Interpreter (interpreter): It defines what value is obtained after an object has been manipulated with an object.
The main application is in the development of the compiler in OOP development. So the applicable surface is narrower.
⑩ Mediator (mediator): Mainly used to reduce the coupling between classes and classes. Because of the assumption that the class has a dependency relationship with the class. Not conducive to the expansion and maintenance of functions, because only to change an object, the other associated objects have to be changed.
? Interview (Visitor): The data structure and action on the structure of the decoupling, so that the operation set can be relatively free evolution.
The interview mode is suitable for systems with relatively stable data structures and easy to change algorithms. The advantage of the interview mode is that it is easy to add. Because adding an operation means adding new visitors, its disadvantage is that it is very difficult to add new data structures.
Java Design Pattern Rookie series folder
Java Design Pattern Rookie series (I) Modeling and implementation of strategy mode
Java Design Patterns Rookie series (ii) modeling and implementation of observer patterns
Java Design Pattern Rookie series (iii) modeling and implementation of decorator model
Java Design Pattern Rookie series (iv) Modeling and implementation of plant method model
Java Design Pattern Rookie series (v) Modeling and implementation of abstract factory model
Java Design Pattern Rookie series (vi) modeling and implementation of single case model
Java Design Pattern Rookie series (vii) Modeling and implementation of command pattern
Java Design Pattern Rookie series (eight) modeling and implementation of adapter mode
Java Design Pattern Rookie series (ix) modeling and realization of appearance pattern
Java Design Patterns Rookie series (10) Modeling and implementation of template method pattern
Java Design Patterns Rookie series (11) Modeling and implementation of iterator mode
Java Design Patterns Rookie series (12) modeling and implementation of combinatorial patterns
Java Design Patterns Rookie series (13) State model modeling and implementation
Java Design Pattern Rookie series (14) Modeling and implementation of proxy mode
Java Design Patterns Rookie series (15) Builder Model Modeling and implementation
Java Design Pattern Rookie series (16) modeling and implementation of prototype model
Java Design Patterns Rookie series (17) modeling and implementation of bridging mode
Java Design Patterns Rookie series (18) modeling and realization of responsibility chain model
Java Design Patterns Rookie series (19) model and implementation of Memo mode
Java Design Patterns Rookie series (20) The modeling and implementation of the interpreter pattern
Java Design Pattern Rookie series (21) The modeling and realization of the meta-model
Java Design Patterns Rookie series (22) The model and implementation of the mediator pattern
Java Design Patterns Rookie series (23) The model and implementation of the interview pattern
Java Design Pattern Rookie series Tutorial pdf:http://download.csdn.net/detail/jave_lover/8714347
Java Design mode Blog full folder