A learning journey based on design patterns-Preface

Source: Internet
Author: User
Design-based learning tour ----- Preface

After several years of work, I discovered the importance of the Design Pattern in development and re-reviewed the design pattern. This series of articles is used to record your learning process and share your personal understanding of the design model.

 

1. What is the design model?

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.

2. Purpose of the design model

A. Improve system scalability

B. Improve code quality and readability

C. Improve Personal abilities

3. Introduction to the design mode

Design Pattern Classification: creation, structure, and Behavior

3.1 Creation type

The Creation Mode abstracts the instantiation process. They help a system to create, combine, and express its objects independently. A class creation mode uses inheritance to change the class to be instantiated, while an object Creation Mode delegates the Instantiation to another object.

 

The creation modes include:

Abstract factory, BUILDER, factory method, PROTOTYPE, SINGLETON)

 

Intention of the pattern:

A. Abstract Factory: provides an interface for creating A series of related or mutually dependent objects without specifying their specific classes.

B. Generator: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

C. Factory method: Define an interface for creating objects, and let the subclass decide which class to instantiate. Factory Method delays the instantiation of a class to its subclass.

D. prototype: Use the prototype instance to specify the object type and copy the prototype to create a new object.

E. Single Piece: ensure that a class has only one instance, and provide a global access point to access it.

3.2 structural

The schema involves how to combine classes and objects to obtain a larger structure. The structure class mode uses an Inheritance Mechanism to combine interfaces or implementations. A simple example is to combine two or more classes into one class using the multi-Inheritance Method. As a result, this class contains the nature of all parent classes. This mode is particularly helpful for the collaboration of Multiple Independently Developed class libraries.

The structured object mode not combines interfaces and implementations, but describes how to combine some objects to implement new functions. Because the object composite relationship can be changed at runtime, the object composite method has more flexibility, and this mechanism is impossible to achieve with static class composite.

 

The schema includes the following types:

ADAPTER, BRIDGE, COMPOSITE, DECORATOR, FACADE, FLYWEIGHT, PROXY)

 

Intention of the pattern:

A. Adapter: converts an interface of A class to another interface that the customer wants. The A d a p t e r mode makes those classes that cannot work together due to interface incompatibility work together.

B. Bridging: separates abstract parts from their implementations so that they can all change independently.

C. Combination: Combine objects into a tree structure to represent the "part-whole" hierarchy. C o m p o s I t e makes the use of a single object and a composite object consistent.

D. Decoration: dynamically add some additional responsibilities to an object. With the added function, the D e c o r a t o r mode is more flexible than the subclass generation mode.

E. Appearance: provides a consistent interface for a group of interfaces in the subsystem. The F a c a d e mode defines a high-level interface, which makes the subsystem easier to use.

F. Share: Use the sharing technology to effectively support a large number of fine-grained objects.

G. Proxy: provides a proxy for other objects to control access to this object.

3.3 Behavior Type

The behavior pattern involves the assignment of duties between algorithms and objects. The behavior pattern not only describes the object or class pattern, but also describes the communication pattern between them. These modes describe complex control flows that are difficult to trace at runtime. They shift your attention from the control flow to the contact information between objects. The behavior class mode uses the Inheritance Mechanism to distribute actions among classes. Behavior object mode uses Object combination instead of inheritance.

The schema includes the following types:

Chain of responsibility (responsibility chain), COMMAND, INTERPRETER, ITERATOR, MEDIATOR, MEMENTO, OBSERVER (OBSERVER), STATE, STRATEGY, template method, VISITOR)

Intention of the pattern:

A. responsibility chain: Enables multiple objects to process requests, so as to avoid coupling between request senders and receivers. Connect these objects into a chain and pass the request along the chain until an object processes it.

B. Command: encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations.

C. Interpreter: for a given language, define a representation of its syntax and define an interpreter which uses this interpreter to interpret sentences in the language.

D. iterator: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.

E. intermediary: uses an intermediary object to encapsulate a series of object interactions. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.

F. Memorandum: capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.

G. Observer: defines a one-to-many dependency between objects. When the State of an object changes, all objects dependent on it are notified and automatically updated.

H. State: allows an object to change its behavior when its internal state changes. The object seems to have modified its class.

I. Strategy: Define a series of algorithms, encapsulate them one by one, and make them replaceable. This mode allows algorithms to change independently of customers who use it.

J. template method: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. Te m p l a t e M e t h o d allows the subclass to redefine certain steps of the Algorithm without changing the structure of an algorithm.

K. Visitor: an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the classes of each element.

4. How to select the design mode

A. Consider how the design model solves the design problem.

B. View Intention

C. How to Associate Research Models

D. Similar research objectives

E. Check the reason for redesign

F. Consider which variables are involved in your design.

5. How to Use the design mode

A. browsing mode

Pay special attention to the applicability and effect sections to determine whether it is suitable for your problem.

 

B. Back-to-study structure, participants, and collaboration

Make sure you understand the classes and objects in this mode and how they are associated.

 

C. Check the sample code to see the specific example of the mode code.

Studying the code will help you implement the pattern.

 

D. Select the name of the mode participant to make them meaningful in the application context.

The name of the design mode participant is usually too abstract to appear directly in the application.

 

E. Definition class

Declare their interfaces, establish their inheritance relationships, and define instance variables that represent data and object references. The recognition mode will affect the classes in your application and make corresponding changes.

 

F. Operation name used by definition mode for application

Use the responsibilities and collaboration associated with each operation as a guide. Also, your name conventions must be consistent.

 

G. Implement the responsibility and collaboration operations in the Execution Mode

The implementation section provides clues to guide you through implementation. Examples in the sample code section can also help.

6. References

Gof design mode, HeadFirst design mode, and existing materials on the network

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.