System Architect study note _ Chapter 7 _ serialization

Source: Internet
Author: User

7.1 design pattern overview

A typical problem encountered repeatedly. The so-called model is formed to describe these common problems and solutions to these problems.

7.1.1 Design Pattern history

The mode consists of the following parts:
A specific Context refers to the circumstances under which the mode works;
System of Force refers to the problem or expected goal;
Solution: balances the motivations or constructs or configurations of the problems described.

Each mode describes a problem that occurs repeatedly in a specific situation and the core of the solution to the problem.

7.1.2 why should I use the design mode?

Encapsulation, strength, dependency, flexibility, and reusability must be considered in object-oriented design.

1. simplified and accelerated Design

You do not need to start from the underlying layer, reuse successful designs, save development time, and improve software quality.

2. Convenient communication between developers

It can more accurately describe the problem and solution, so that the solution is consistent.

3. Reduce Risks

4. Helps to switch to object-oriented technology

Developers often have resistance or rejection to new technologies and are skeptical about the effects of new technologies.

Mature design patterns have the following features:

1. Clever.

2. Generic, independent from systems, languages, and fields.

3. not just in theory.

4. Simple.

5. reusable.

6. Object-oriented.

7.1.3 components of the design model

1. The model name briefly describes the nature of the model and helps us think about it.

2. The problem or intention explains the cause and effect of the design problem and the problem, and may describe the specific design problem.

3. The scenario tells us the applicability of this model.

4. Motivation: Describe related motivations and constraints. Generally, we need to sort the expected goals in a limited order. Motivation clarifies the complexity of the problem, defines the various balancing measures taken in the case of conflicts.

5. solution. Because the mode is like a template, the solution does not describe a specific and specific design or implementation, instead, it provides an abstract description of the design problem and how to use a combination of general elements.

6. Examples help readers understand the specific usage of the model.

7. The result scenario describes the status and side effects of the model.

8. Basic principles: explain how and why the current problem can be solved.

9. Related modes, including static and dynamic, Migration Mode, subsequent mode, and alternative mode.

10. For known applications, a summary is provided before the normal mode, which provides a brief summary and overview to draw a clear picture for the mode, provides quick information about how this mode can solve the problem.

The mode should describe its target reader and its knowledge requirements.

7.1.4 classification of design patterns

Software models can be divided into design, analysis, organization, and process models.

The design mode is mainly used for concise and flexible system design.

Based on the purpose of the design model, creation type, structural type, behavior type;
Divided by the scope of design patterns, class design patterns, Object design patterns.

1. The Creation Mode abstracts the Object Instantiation process and uses interfaces defined by abstract classes to encapsulate information such as how to create and combine objects.
2. Structured mode: how to combine existing classes and objects and obtain a larger structure.
3. The behavior mode not only describes the object or class mode, but also describes the communication mode between them, in particular, it describes how peer-to-peer objects collaborate to complete tasks that cannot be completed independently by any of them.

7.2 Design mode instance

7.2.1 Creation Mode

This subclass is used to create objects. However, this may limit the type or number of objects created in the system.

1. Abstract Factory Mode

When no specific class is specified, APIs are provided for creating column-related or mutually dependent objects.

Provides an abstract class that can determine the appropriate class.

Advantages:

It can be separated from a specific class.
It is easier to switch between products.
Improves product consistency.

Abstract Factory mode should be used in the following cases:

The system is independent of the creation, composition, and representation of products.
The system is configured as a series with multiple products.
Related Product object series are used together, and this must be ensured.
You want to provide the product class library and only open its interfaces.

2. Builder Mode

Separates components and representations of complex objects. Different objects can be created in the same construction process by specifying only the object type and content.

You can create all complex objects at a time, but you can create only one object at a time in other modes.

Advantages:

You can change the internal representation of the product.
Separates the constructor code from the representation code.

The Builder mode should be used in the following situations:

Algorithms are independent of component objects.
The construction process must allow different representations of the component objects.

3. Factory Method mode

The instantiation work is handed over to its subclass. A new class can be introduced without modifying the code, because the new class only implements interfaces.

Advantages:

The Code only processes the interface, so any class that implements the interface can be used.
Creating objects in a class is more flexible than creating objects directly on the client.

In the following cases, the Factory Method mode should be used:

Class cannot predict the class of the object it must create.
Class.
Class transfers the responsibility to a help subclass, and the user wants to locate the authorized help subclass.

4. Prototype mode

You only need to define the object class to be able to copy itself.

Advantages:

You can add or delete products at runtime.
Specify a new object by changing the value.
Create new objects by changing the structure.
Reduce subclass generation and use.
You can use classes to dynamically configure applications.

Prototype mode should be used in the following cases:

During runtime, specify the class to be instantiated, such as dynamic loading.
Avoid building a factory class hierarchy similar to the product class hierarchy.

5. Singleton Mode

Make sure that a class has only one instance and provides a global access portal. Make sure that all objects in this instance use the same instance.

Advantages:
Controlled access to a single instance.
The number of namespaces is reduced.
Operations and representations can be improved.
Number of instances allowed to be changed.
It is more flexible than class operations.

7.2.2 Structural Model

The institutional model controls a majority of relationships.

It will affect the application in different ways.

Allows you to create a system when additional or custom code is required.

Enhanced reusability and application performance.

1. Adapter Mode

It can act as a medium between two classes. It can be used by another class to convert the interfaces of one class so that classes with incompatible interfaces can be used by the system.

Advantages:

Multiple incompatible objects are allowed for interaction and communication.
Improve the reuse of existing functions.

The Adapter mode should be used in the following cases:

To use an existing class, the class interface does not match the required interface.
To create reusable classes, this class can collaborate with irrelevant or unknown classes.
Objects must be used in an interface environment different from known object interfaces.
You must perform Interface Conversion between multiple sources.

2. Bridge Mode

Divides a complex component into two independent but related inheritance hierarchies: Functional abstraction and internal implementation.

Advantages:

Interfaces and implementations are separated.
Improved scalability.
The implementation details are hidden from the client.

The Bridge mode should be used in the following cases:

Avoid permanent binding between abstraction and its implementation.
The abstraction and its implementation can be extended using subclasses.
The abstract implementation is changed without re-compiling the code.

3. Composite Mode

Create a tree hierarchy to change complexity.

Advantages:

Defines a class hierarchy consisting of the main object and the object that meets the requirements.
It is easier to add a new component type.
Structure Flexibility and manageability interfaces.

The Composite mode should be used in the following cases:

The hierarchy of the entire or part of the object.
You want the client to ignore the differences between a compliant object and a single object.
Structures can have any level of complexity and are dynamic.

4. Decorator Mode

Add or delete an object without modifying its appearance and functions.

Advantages:

It is more flexible than static inheritance.
This avoids feature loading classes from being too high in the hierarchy.
Simplified encoding.
Improved object scalability.

Decorator mode should be used in the following cases:

Dynamically and transparently add responsibilities to a single object without affecting other objects.
Add responsibilities to objects that may be modified in the future.
Extension cannot be implemented through static subclass.

5. Facade Mode

Provides a unified interface for a group of interfaces in the subsystem. It is easier to use advanced sub-system interfaces.

Advantages:

A simple interface is provided for a complex system without reducing the options provided by the system.
The subsystem components are blocked.
Increase coupling.
Convert client requests and send them to subsystems that can process these requests.

Facade mode should be used in the following cases:

Provides simple interfaces for complex subsystems.
There are many dependencies between the client and the abstract implementation class.
You want to layer subsystems.

6. Flyweight Mode

Reduce the number of objects by sharing objects.

Share an interface to avoid overhead caused by multiple instances with the same information.

Advantages:

Reduces the number of objects to be processed.
If the object persists, the memory and storage devices can be reduced.

In the following cases, the Flyweight mode should be used:

Applications use a large number of objects.
The large number of objects leads to high storage overhead.
Does not depend on the identity of the object.

7.2.3 Behavior Model

The behavior mode affects the status and behavior flow of the system.

Simplify, optimize, and improve the maintainability of applications.

1. Chain of Responsibility Mode

Establish a chain in the system, and either process it at the level it receives first, or locate the object that can process it.

Advantages:

Reduced coupling.
This increases the flexibility of object-oriented responsibility formulation.
A collection of classes can be used as a whole.

The Chain of Responsibility mode should be used in the following cases:

Multiple objects can process one request, but their processors are unknown.
Send a request to one of several objects without specifying the exact request receiver object.
Dynamically specify the object set that can process the request.

2. Command mode

The request is encapsulated in the object.

Advantages:

Separate the object that calls the operation from the object that knows how to complete the operation.
It is easier to add new commands because you do not need to modify existing classes.

In the following cases, the Command mode should be used:

The action to be executed to parameterize the object.
Specify, sort, and execute requests at different times.
It must support Undo, logging, or transactions.

3. Interpreter Mode

The language used to explain the definition of its syntax. It provides a statement interpreter.

Advantages:

It is easy to modify and extend the syntax.
It is easier to implement the syntax.

Interpreter mode should be used in the following cases:

The syntax of the language is relatively simple.
Efficiency is not the main problem.

4. Iterator Mode

Provides consistent methods for ordered access in a set, which is independent from the basic set.

Advantages:

Supports different traversal of sets.
Simplified the set interface.

The Iterator mode should be used in the following cases:

Access the content of the Set object without opening the internal representation of the Set object.
Supports multiple traversal of collection objects.
Provides a unified interface for Traversing different structures in a set.

5. Mediator Mode

By introducing an object that can manage message distribution between objects, the communication between objects in the system is simplified. The loose coupling between objects is improved, and interactions between objects can be changed independently.

Advantages:

Remove the impact between objects.
Simplifies inter-object protocols.
Centralized Control.
Since you no longer need to directly transmit messages to each other, a single component becomes simpler and easier to process.
Components become more common because they no longer need to contain logic to process communication between components.

The Mediator mode should be used in the following cases:

The object set needs to communicate in a standardized but complex way.

6. Memento Mode

A "snapshot" that maintains the object state. An object can return to its initial state without disclosing its content to the outside world.

Advantages:

Maintain encapsulation integrity.
The operations required to return to the initial status are simplified.

Memento mode should be used in the following cases:

The snapshot of the object state must be saved and restored.

7. Observer Mode

Defines one to multiple dependencies between objects. When an object changes its status, it will automatically notify and update all its dependent objects.

Advantages:

Abstract The coupling relationship between the subject and the Observer.
Supports broadcast communication.

The Observer mode should be used in the following cases:

Modifying an object involves modifying other objects, and you do not know how many objects need to be modified.
Objects should be able to notify other objects on the premise of different hypothetical object identifiers.

8. State Mode

When the internal status of an object changes, its behavior is changed and Its Class is modified.

Advantages:

Divides behaviors based on different States to enable explicit state conversion.

9. Strategy Mode

Defines a group of classes that can be used to represent a set of possible behavior. These actions can be used in the application to modify the application functions.

Advantages:

Another subclass method.
Each behavior is defined in the class itself, reducing the number of condition statements.
It is easier to expand the model.

The Strategy mode should be used in the following cases:

Many related classes only differ in behavior.
Different variants of the algorithm are required.
The algorithm uses unknown data from the client.

10. Template Method mode

This method allows the subclass to overload some methods without rewriting.

Perform some steps by subclass.

Advantage: the basic technology of code reuse.

The Template Method mode should be used in the following cases:

The unchanged part of an algorithm is implemented at a time, and sub-classes implement variable behavior of the algorithm.

11. Visitor Mode

Define a new operation without changing the class of the Operation element.

Advantages:

Easy to add new operations.
Exclude irrelevant operations in a centralized manner.

The Visitor mode should be used in the following cases:

Contains many object classes with different interfaces, and you want to operate these objects dependent on specific classes.
Classes that define the object structure are rarely modified, but new operations need to be defined on this structure.

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.