Java Design Pattern Rookie series summary and blog full list

Source: Internet
Author: User
Tags shallow copy switch case

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/40031567


Here is a summary of these 23 design patterns today. The purpose of our design pattern is to reuse code, make it easier for others to understand, ensure code reliability, and of course design patterns are not omnipotent, and the actual problems in the project are analyzed in detail. We can not use design patterns, but in the process of analyzing the problem, think of using a design pattern can achieve the effect we need, and than not using design patterns more advantages, then we should consider the use of 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, visitor, mediator, interpreter.


second, I would like to talk about the 23 types of design patterns of understanding

1. created (Creator) mode (5 total)

① single case (Singleton) is a common design pattern. In Java applications, singleton objects guarantee that only one instance of the object exists in a JVM. There are two main ways to realize the A hungry man type than the lazy type.

② Prototype (Prototype): The idea of this mode is to use an object as a prototype, copy it, clone it, and produce 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 (), whereas deep replication requires that the current object be written in the form of a binary stream before it is read.

③ Builder: This model is a collection of products that are managed together. By integrating many functions into a class, this class can create more complex things. It is concerned with creating multiple parts of a compound object.

④ Factory Methods (Factory method): Call factory methods to produce objects (products). It has 3 implementations: 1) Normal Factory mode: It is to build a factory class to create instances of some classes that implement the same interface. 2) Multiple factory method modes: is an improvement to the common factory method pattern, in the normal factory method mode, if a string error is passed, the object cannot be created correctly, and multiple factory method patterns provide multiple factory methods to create the object 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, call directly. If you want to use the factory method mode, you can choose the 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): Converts the interface of a class to another interface that the client expects to represent, in order to eliminate compatibility issues with classes caused by an interface mismatch. 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 another 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 another 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 the line. 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 the methods, we write other classes, when we inherit the abstract class.

② Proxy: Proxy mode is actually more than one proxy class out, for the original object to do some operations. For example, we sometimes need to ask a lawyer, because lawyers have expertise in the law, you 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 to solve the dependency between classes and classes, like spring, the relationship between classes and classes can be configured into a configuration file, and the appearance pattern is to put their relationship in a facade class, reducing the coupling between class classes, which does not involve interfaces.

④ Bridging (bridge): separating things from their concrete 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 combined mode is sometimes called part-whole mode, which combines objects into a tree structure to represent the "partial-whole" 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, when there are many objects in the system can reduce the memory overhead. In a way, you can think of a singleton as a special case of the element of enjoyment.

⑦ Decorator (Decorator): Dynamically attaches responsibility to objects, and to extend functionality, decorators provide a more resilient alternative than inheritance. Maintain interfaces for enhanced performance.


Behavioral (Behavior) mode (total 11 types)

The ① Policy (strategy) allows the user to choose a method of performing an action, that is, the user can choose a different policy to operate on. Personally think the strategy mode 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, so that when an object changes state, the object that relies on it is notified and automatically follows the new. Java has already provided the default implementation of the Observer observer pattern, and Java's support for the observer pattern is mainly embodied in the observable class and the Observer interface.

③ 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 redefine some of the steps in the algorithm without changing the structure of the algorithm. In short: The template method defines the steps of an algorithm and allows subclasses to provide implementations for one or more steps.

④ iterators (Iterator): Provides a way to sequentially access individual elements in an aggregation object without exposing their internal representations.

⑤ responsibility Chain (chainofresponsibility): There are multiple objects, each holding a reference to the next object, forming a chain that requests to pass through the chain until an object decides to process the request, but the issuer is not sure which object will eventually process the request.

⑥ command: Encapsulates a request (command/password) into an object to parameterize its objects with different requests, queues, or logs. 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: Allows an object to change its behavior when the internal state changes, and the object looks as if it has modified its class. State mode It's just a matter of having a different state, a different state, a different behavior, and it's actually an extension of a statement like switch case.

⑨ Interpreter (interpreter): It defines what value is obtained after an object has been manipulated with an object. It is generally used in the development of the compiler in OOP development, so the application surface is rather narrow.

⑩ Mediator (mediator): Mainly used to reduce the class-to-class coupling, because if the class and the class has dependencies, it is not conducive to the extension and maintenance of the function, because as long as you modify an object, the other associated objects have to be modified.

? Visitor (Visitor): The data structure and the action on the structure are decoupled, so that the operation set can evolve relatively freely. The visitor pattern is suitable for systems with relatively stable data structures and easy to change algorithms. The advantage of the visitor pattern is that it is easy to increase the operation because increasing the operation means adding new visitors, and its disadvantage is that it is difficult to add new data structures.


Java Design Pattern Rookie series catalogue

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) visitor pattern modeling and implementation



Java Design Pattern Rookie series summary and blog full list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.