Common design Patterns

Source: Internet
Author: User

Design patterns are divided into three types: created, structured, and behavioral.     Among them are: First, Singleton, Singleton mode: Ensure that a class has only one instance, and provide a global access point to access it. Abstract Factory, Abstraction Factory: Provides an interface to create a series of related or interdependent objects without specifying their specific classes.     Factory method, Factory methods: Define an interface for creating objects, let subclasses decide which class to instantiate, and Factory method to defer instantiation of a class to subclasses.     Builder, Build mode: Separates the construction of a complex object from his presentation, allowing the same build process to create different representations. V. Prototype, prototype mode: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.     Behavioral types are: VI, Iterator, iterator pattern: Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation.     VII. Observer, Observer pattern: Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified of Automatic Updates.     Template method: Define the skeleton of an algorithm in an operation, and defer some steps into the subclass, Templatemethod so that subclasses can redefine the algorithm to some specific steps without changing the structure of an algorithm.     commands, Command mode: Encapsulates a request as an object, allowing you to parameterize the customer with different requests, queue requests and log requests, and support revocable operations. State mode: Allows an object to change his behavior when its internal state changes.     The object seems to have changed his class.     XI. strategy, Strategy mode: Define a series of algorithms, encapsulate them one by one, and allow them to replace each other, this mode allows the algorithm to be independent of the customers who use them.     12, China of Responsibility, responsibility chain mode: Enables multiple objects to have the opportunity to process the request, thus avoiding the coupling between the communicated and Receiver of the request 13, mediator, Broker pattern: Object interaction that encapsulates some columns with a mediation object.     14. Visitor, Visitor mode: represents an operation that acts on elements of an object structure, allowing you to define new actions that act on this element without changing the individual element classes. XV, interpreter, interpreter mode: Given a language,Define a representation of his grammar and define an interpreter that interprets the sentences in the language using that representation. 16. Memento, Memo mode: captures the internal state of an object without destroying the object, and saves the state outside the object.     Structural type: 17, Composite, combined mode: combine objects into a tree structure to represent a part of the overall relationship, Composite makes the user consistent with the use of individual objects and composite objects.     18, facade, appearance mode: To provide a consistent interface for a set of interfaces in the subsystem, Fa?ade provides a high-level interface, which makes the subsystem easier to use.     19, Proxy, Agent mode: For other objects to provide a proxy to control access to this object 20, Adapter, adapter mode: To convert a class of interfaces to another interface that the customer wants, Adapter mode makes it impossible to work together those classes that are incompatible with the interface.     21, Decrator, Decoration mode: Dynamically add some additional responsibilities to an object, the decorator mode is more flexible than generating subclasses in terms of the added functionality.     22. Bridge mode: Separates the abstract part from its realization, so that they can change independently. 23, Flyweight, enjoy meta mode

Design Patterns(Design Pattern), is a set of repeated use, most people know, the purpose of the classification, code design experience Summary.
Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. Design patterns in self, in others, in the system are multi-win, design patterns to make code real engineering. Design patterns are the cornerstone of software engineering, and the code is truly engineered like a block of bricks and tiles in a building.
Only proficient in the design mode, dare to say that the real understanding of software engineering. It can be said that design patterns are one of the skills that every architect must have. As an object-oriented design programmer, only proficient in the design pattern, can get rid of the fate of the code slave, become a real software engineer, to complete the leap of their own value and the sublimation of design ideas!
1. Design Patterns and Frameworks
Reusable object-oriented software systems are now generally divided into two main categories: the Application Toolbox and framework (framework), the specific software we develop in the usual application, Java API is the Toolbox, and the framework is a class of specific software reusable design of a group of mutually collaborative classes, EJB (Enterprise Java Beans) is the framework that Java applies to enterprise computing.

The framework usually defines design parameters such as the relationship between the overall structure class and the object of the application system, so that the specific application implementation can focus on the specific details of the application itself.  The framework mainly records the common design decision in the software application, the frame emphasizes the design reuse, therefore must use the design pattern in the frame design. In addition, design patterns help to understand the framework structure, mature frameworks often use a variety of design patterns, if you are familiar with these design patterns, no doubt you will quickly grasp the structure of the framework, our general developers if the sudden contact with EJBJ2EE and other frameworks, will feel particularly difficult to learn, difficult to grasp, The first step in mastering the design pattern is undoubtedly giving you a tool to dissect the EJB or the Java EE system.
2, the principle of design mode
In recent years, everyone has begun to pay attention to design patterns. So, why do we use design patterns? Why should we advocate design patterns?
The root cause is to get rid of programming inefficiencies, improve code reuse, enhance code robustness and stability, and increase maintainability.
So how do you implement code reuse?
From process to object-oriented, it is the greatest step since the birth of software design, object-oriented programming has successfully solved the inefficiency problem of process-oriented software programming, and changed people's programming thinking completely, and opened a new chapter for software design. However, to achieve object-oriented design, completely out of the process-oriented design thinking, and not just as long as the use of an object-oriented programming language can be achieved. With object-oriented design, you can design good software, and you can also design bad software. Only by following some specific principles can we design reusable software with high flexibility.
In the use of object-oriented ideas for software design, there are 6 principles to follow:
    1. Single Duty principle (Responsibility Principle)
    2. Richter Replacement principle (Liskov Substitution Principle)
    3. Dependency inversion principle (dependence inversion Principle)
    4. Interface Isolation principle (Interface segregation Principle)
    5. Dimitri (Law of Demeter)
    6. Opening and closing principle (Open Close Principle)
In the process of software design, as long as we try to follow the above six design principles, the design of the software will be a good software, it must be strong enough, stable enough, and with great flexibility to meet the needs of the time to change and other factors.


3. Four elements of design pattern
Design patterns make it easier and easier for people to reuse successful designs and architectures. The presentation of proven technologies into design patterns will also make it easier for new system developers to understand their design ideas.
pattern names (pattern name)
A help-person, which uses one or two words to describe the problem, solution, and effect of the pattern. Naming a new pattern adds to our design vocabulary. Design patterns allow us to design at a higher level of abstraction. Based on a pattern glossary, we can discuss patterns between ourselves and our colleagues and use them when writing documents. The pattern name helps us to think, so that we can communicate design ideas and design results to others. Finding the right model name is also one of the difficulties in cataloging our design patterns.
    • Question (problem)
Describes when the pattern should be used. It explains design problems and the causes and consequences of problems, which may describe specific design problems, such as how to use object representation algorithms. It may also describe a class or object structure that results in an inflexible design. Sometimes the problem section includes a series of prerequisites that the usage pattern must meet.
    • Solutions (solution)
Describes the composition of the design, the relationship between them and their respective responsibilities and ways of collaboration. Because the pattern is like a template that can be applied to many different situations, the solution does not describe a specific and specific design or implementation, but rather provides an abstract description of the design problem and how to solve the problem with a general-meaning combination of elements (class or object combination).
    • Effect (Consequences)
It describes the effect of pattern application and the problem that the usage pattern should weigh. Although we do not always refer to pattern effects when we describe design decisions, they are important for evaluating the costs and benefits of design selection and understanding usage patterns. Most software effects focus on measuring time and space, and they also express language and implementation issues. Because reuse is one of the elements of object-oriented design, pattern effects include its impact on system flexibility, extensibility, or portability, and explicitly listing these effects is useful for understanding and evaluating these patterns.


4. Overview of Design Pattern classification
"Design mode", the 1th time the design model to the theoretical height, and standardize it. The book summarizes 23 basic design patterns, "design mode" Download
These 23 design patterns cover almost all the problems in the object-oriented design process, and the 23 design patterns mentioned in the book are:

Design Patterns

(Design Pattern)

Create type

(creational)

Structural type

(Structural)

Behavioral type

(behavioral)

1. Abstract Factory (Abstraction Factory)

2, builder (builder)

3. Factory method (Factory methods)

4. Prototype (prototype)

5, Singleton (single State)

6, Adapter (adapter)

7. Bridge mode

8, Composite (combination)

9. Decorator (decoration)

10, Façade (appearance)

11, Flyweight (enjoy Yuan)

12, Proxy (agent)

13. Chain of Responsibility (responsibility chain)

14. Command (Order)

15. Interpreter (interpreter)

16. Iterator (iterator)

17, Mediator (intermediary)

18. Memento (Memo)

19. OBSERVER (Observer)

20. State (status)

21. Strategy (Strategy)

22. Template Method

23. Visitor (visitor)


5, design pattern design Scenario

So where do so many design patterns come from?
"I Ching" has a cloud: "Easy to have Tai Chi, is born two, two Miriam born four elephant, four elephant health gossip", meaning that all things in the world have origins.
The origin of design pattern is the object-oriented programming idea, which is the essence of object-oriented design-abstraction. Object-oriented objects through classes and objects to achieve abstraction, the implementation of the object-oriented three important mechanisms: encapsulation, inheritance, polymorphism, it is these three mechanisms derived from a variety of design patterns.

Support Design for Design patterns

Objective

Design Patterns

Variable aspects

Create

(creational)

1. Abstract Factory

2. Builder

3, Factory Method

4, Prototype

5, Singleton

Product Object Family

How to create a composite object

Subclasses that are instantiated

The class being instantiated

A unique instance of a class

Structure

(Structural)

6, Adapter

7. Bridge

8, Composite

9, Decorator

10, Façade

11, Flyweight

12. Proxy

interface of the object

Implementation of the object

The structure and composition of an object

The responsibility of the object, not to generate subclasses

interface of a subsystem

Storage overhead for objects

How to access an object, the location of the object

Behavior

(behavioral)

13. Chain of Responsibility

14. Command

15, interpreter

16, Iterator

17, mediator

18, Memento

19, Observer

20. State

21, strategy

22. Template Method

23, Visitor

An object that satisfies a request

When and how to satisfy a request

Grammar and interpretation of a language

How to traverse and access the elements of an aggregation

How objects interact and interact with each other

What private information in an object is stored outside of the object and when it is stored

Multiple objects depend on another object, and how these objects remain consistent

The state of the object

Algorithm design

Some steps in the algorithm

Some classes that can be used for operations on a (group) object, but do not modify those objects




6. Classification and description of design patterns

Design pattern, divided into the creation mode, the structure pattern, the behavior type pattern

Classification description of Design Patterns

Create pattern

1. Abstract Factory mode

Provides a unified creation interface for a product family. When you need a series of this product family, you can create a specific factory class from the abstract factory by selecting the appropriate family.

2. Builder mode

Separating the construction of a complex object from its representation allows the same build process to create different representations.

3. Factory method Mode

Defines an interface for creating objects, but lets subclasses decide which class to initialize. The factory method delegates the initialization of a class to subclasses.

4. Prototype mode

Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.

5. Single-Case mode

Ensure that a class has only one instance and provides global access to the instance.

5. Multi-instance Mode

Make sure that a class has only named instances and provides global access to those instances.

Object Pool Mode

Avoid the costly costs of acquiring and freeing resources by recycling objects.

Lazy Initial Mode

Delaying the creation of objects, calculation of data, etc., requires more resource-intensive operations, which are performed only on the first visit.

Resource acquisition as initialization

Ensure that resources are freed appropriately by binding to the life cycle of the appropriate object.

Structural mode

6. Adapter mode

Transforms the interface of a class into another interface representation that the client expects. The adapter mode eliminates class compatibility issues caused by interface mismatches.

7. Bridging mode

Decoupling an abstraction from the implementation so that the two can change independently.

8. Combination mode

Multiple objects are organized into a tree structure to represent the local and the whole, so that the user can treat the combination of individual objects and objects in the same way.

9. Decoration mode

Add more features dynamically to an object. A decorated pattern is another way to extend functionality in addition to class inheritance.

10. Appearance mode

Provides a consistent interface for a set of interfaces in a subsystem, and the appearance pattern defines a high-level interface that makes this subsystem easier to use.

11. Enjoy RMB

By sharing to effectively support a large number of small granular objects.

12. Agent

Provides a proxy for other objects to control access to this object.

Behavioral patterns

13. Responsibility chain

The coupling between the sender and receiver of the cancellation request allows multiple objects to have the opportunity to process the request. Link the objects together and pass the request along the chain until an object handles it.

14. Command

Encapsulates a request as an object so that you can parameterize the customer with different requests, queue requests or log requests, and support actions that can be canceled.

15. Interpreter

Given a language, define a representation of its grammar and define an interpreter that uses that representation to interpret sentences in the language.

16. Iterators

Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation.

17. Intermediary person

Wraps a series of objects interacting in such a way that they do not have to interact with each other so that they can be loosely coupled. When the role of some objects changes, it does not immediately affect the role of some other objects, to ensure that these effects can be independent of each other.

18. Memo

A Memo object is an object that is used to store a snapshot of the internal state of another object. The purpose of the memo mode is to capture the state of an object without destroying the package, and to externally and store it so that it can be restored to the stored state at the appropriate time in the future.

19. Observer pattern

Defines a one-to-many connection between objects, whereby when an object changes state, all other related objects are notified and refreshed automatically.

20. State

When an object changes its internal state, its behavior changes as well. State mode requires the creation of a subclass of a State class for each of the possible states of a system. When the state of the system changes, the system changes the selected subclass.

21. Strategy

Define a series of algorithms, separate them, and make them interactive. The strategy mode allows the algorithm to change independently when the user is in use.

22. Template method

The template method pattern prepares an abstract class, implements some of the logic in the form of concrete methods and concrete construction subclasses, and then declares some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. First, build a top-level logical framework, and leave the details of the logic to the specific subclasses to implement.

23. Visitors

Encapsulates some operations that are applied to a data structure element. Once these operations need to be modified, the data structure that accepts the operation can remain intact. The visitor pattern is suitable for systems with relatively indeterminate data structures, which frees up the coupling between the structure and the operation of the structure so that the set of operations can evolve relatively freely.

Empty Object

Avoid null references by providing default objects.

Blackboard

A generalized observer communicates information within a system, allowing multiple readers and creators.

Specifications

A bindable business logic that is represented in a Boolean form.


In the development of object-oriented software design, in addition to the 23 design patterns mentioned in the Book of design patterns, new design patterns continue to emerge.

To learn more about the background of object-oriented design, refer to interface mode, cohesion, and coupling.
To learn more about the context of object-oriented programming, refer to inheritance, overloading, polymorphism, interfaces
Reference Source: Design mode (wiki)

Common design Patterns

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.