Reflection on design patterns (II)

Source: Internet
Author: User

Reflection on design patterns (II)

The previous blog talked about the structural mode of the design mode. This time we will take a look at the Creation Mode and summarize some of the experiences we have gained.

Ii. Create Type 1 and three factories (1) Simple Factory



① In a simple factory, instances of different classes are returned based on different parameters. The created instances usually share the same parent class.

② Advantages and disadvantages:

Separation of object creation and use, and encapsulation of changes in the factory

Reduce user memory and directly input the required parameters without having to know the specific class name.

By introducing a configuration file, you can add specific product classes without changing the client code to make the system more flexible.

③ Principle:

It violates the principle of openness and closure, and complies with the principle of Demeter and dependency reversal.

(2) Factory Method -- Factory Method

Example

① In the factory method model, we no longer provide a uniform factory class to create all product objects, but provide different factories for different products, the system provides a factory level structure corresponding to the product level structure

② Factory Method Pattern: defines an interface used to create an object, so that the subclass decides which class to instantiate. The factory method mode delays the instantiation of a class to its subclass.

③ Advantages and disadvantages

With polymorphism design, the factory can determine the product object to be created, and the details of how to create this object are completely encapsulated in a specific factory.

Good scalability, fully compliant with the open and closed principles

The disadvantage is that the complexity of the system is increased and the comprehensibility is relatively poor.

④ Principles

Fully compliant with the open and closed principles and dependency reversal principles

(3) Abstract Factory -- Factory Method


In the abstract factory model, each specific factory provides multiple factory methods to generate a variety of different types of products that constitute a product family.

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

② Advantages and disadvantages

Compliant with the open and closed principles, and better system scalability

Added system flexibility by adding configuration files and reflection

The system is complex and hard to understand

③ Principle

Compliant with the principle of open/closed and dependency reversal

(4) three factory relationships

A simple factory has only one production line to produce one product. The factory method has multiple production lines, but each production line can only produce one product. The abstract factory has multiple production lines, each production line can produce a variety of products.

The three factories can be converted under certain conditions (using the extreme idea ). Simple factories are special cases of factory methods, while factory methods are special cases of abstract factories.

2. Builder mode-Builder


(1) It separates the creation process of a client from a complex object that contains multiple components (or components). The client does not need to know the internal components and assembly methods of the complex object, you only need to know the type of the builder.

(2) Builder Pattern: separates the construction of a complex object from its representation so that different representations can be created during the same construction process.

(3) advantages and disadvantages

Decoupling the product itself from the product creation process so that different product objects can be created during the same creation process.

It is easy to expand and complies with the open and closed principles. Each specific builder is relatively independent of other specific builders, so it is easy to replace or add new specific builders, you can use different builders to obtain different product objects. Because the conductor class is programmed for the abstract builder, adding new specific builders does not need to modify the code of the original class library. This facilitates system expansion and complies with the "open and closed principle"

You can more precisely control the creation process. The creation steps of complex products are divided into different methods to make the creation process clearer and easier to use programs to control the creation process.

Objects Created in the builder mode generally have many similarities, and their components are similar. Otherwise, this mode is not suitable for users.

Generally, the product to be created is relatively simple and less variable. If the product is complicated internally, we need to use multiple builders to encapsulate changes, increasing the complexity of the system.

(4) Principles

In line with the principle of open and closed, the principle of dependency reversal, and the law of Demeter

3. Prototype


When using the prototype, we need to first create a prototype object, and then create more objects of the same type by copying this prototype object.

(1) Prototype Pattern: Use a Prototype instance to specify the type of the object to be created and copy the Prototype to create a new object.

(2) advantages and disadvantages

This improves the efficiency of creating new instances.

Good scalability. Because the prototype is provided in the prototype mode, you can program the prototype on the client and write the prototype in the configuration file, adding or removing product categories has no impact on the original system.

The prototype mode provides a simplified creation structure. The factory method mode usually requires a factory level structure that is the same as the product class level structure, which is not required in the prototype mode, in prototype mode, product replication is implemented by cloning methods encapsulated in the prototype class, and no special factory class is required to create the product.

The object state can be saved by means of deep cloning, and the object state can be copied and saved in prototype mode, it can be used as needed (such as restoring to a certain historical State) to assist in undo operations.

Each class requires a clone method, and the clone method is located inside a class. When modifying an existing class, you need to modify the source code, which violates the "open/closed principle ".

When deep cloning is required, you need to write more complex deep cloning code, and pay attention to whether there are multiple nested references.

(3) Principles

Violating the principle of opening/closing and complying with the principle of single responsibility

Iii. Feelings

From this lecture, we learned not only the design model, but also how to learn.

Learning is a process from simplicity to complexity, and then from complexity to simplicity. The first step in learning is to flat it first, and think it is very simple and easy. Then, with the deep learning, you will find it very complicated and then go through your own abstraction, encapsulation makes it simple. This is the learning process. This kind of learning is not easy to use and requires you to practice it yourself.

Sometimes, when thinking about a problem, we may wish to extreme the problem and see what unexpected gains we have. For example, before doing one thing, let's first think about the best situation and the worst case. If we can handle both cases, then the other cases will be So Easy.

To sum up, learning is an endless road, and we have to take a lifetime.

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.