Looking back, is the strategy and simple factory still ?, Let's look back at the policy factory.

Source: Internet
Author: User

Looking back, is the strategy and simple factory still ?, Let's look back at the policy factory.
This blog is a draft I prepared a long time ago, but it has been dragging on to the present. I just attended a class in Baoding, and my notebook was useless. I finally picked up these drafts by looking at my cell phone, decided to finish writing.

Before talking about the design model, let's talk about the open and closed principles.

I. Open and Close principle (ocp)
I used to read these official interpretations, but I always think they are very official. Now I am reading them and I think the sentence is classic. Share with you

The modules designed following the Open and closed principles have two main features: (1) Open for extension ). This means that the behavior of the module can be expanded. When the application needs to change, we can expand the module to meet the new changes. That is to say, we can add module functions.

(2) Closed for modification ). You do not need to modify the source code or binary code of the module when extending the module behavior. The binary executable version of the module, whether it is a connectable library, DLL or. EXE file, does not need to be changed.
Note:: The open/closed principle is for a module. Otherwise, it makes no sense to open/close the module.

These two models are confusing because the class diagrams are similar. I have already introduced these design patterns in my previous blog. I will not describe them in detail here. This blog is just a bit of feeling when I read my notes recently. Let's talk about the differences between these two design patterns.

2. You are still you, I am still me-strategy VS simple factory
1. Different problem domains


First, let's take a look at the definitions of the two modes:

Rule Mode

Strategy: it defines a series of algorithms, encapsulates each algorithm, and allows them to replace each other. The policy pattern prevents algorithm changes from affecting customers who use the algorithm.


Simple Factory

Simple Factory Pattern is an innovative class mode, also known as Static FactoryMethod Pattern. It is used to create instances of other classes by defining a specific class, the created instance usually has a common parent class.

The main difference is that the rule mode is behavior-based design mode, while the simple factory mode is creation-based. The two design models have different problem domains and different focuses. They can open your eyes and learn these two models.


2. Open and Close principles

Rule ModeFeatures:Create, select the responsibility of different policies on the client.

What if there are extensions? The following changes are required:

1: Add a new policy class to the policy end. You do not need to change the existing policy code ---- open and close Principle

2: Add a new policy string to the interface,Modify the client call---- The client must be modified in any case.

So I think the simple factoryFeatures:Select and create the responsibility on the algorithm side.

Therefore, the following changes are required for Scaling:

1: Add a new algorithm class to the algorithm end,The modification to the called algorithm is still on the algorithm side., You need to change the two items-violation of the open and closed Principle

2: Add a new algorithm string to the customer interface-the client must be modified.

This is the difference 2: one follows the principle of opening and closing, and the other does not. Another point is that it is not difficult to follow the principle of opening or closing. The specifics should be based on the requirements and specific treatment.

Whether the problem domain is different on the macro level and whether the open and closed principles need to be followed in terms of details is sufficient to allow us to select simple factories and Policy modes in a timely manner.


3. The strategy is the same as that of a simple factory.


If the two models do not consider the problem domain, they only look at the class graph. They all implement the expansion of subclass through inheritance, which can solve the dynamic change function. Therefore, there are some commonalities:

 Advantages:

1. unit testing is simplified because each algorithm has its own class and can be tested independently through its own interface.
2. Avoid using multiple conditional transfer statements in the program to make the system more flexible and easy to expand.

  Disadvantages:
1. Because each specific policy class generates a new class, the number of classes to be maintained by the system is increased.

Summary:In fact, the 23 design patterns have to be combined, so I would like to say: they are similar, because their unified purpose is: abstract, inheritance, polymorphism, encapsulation, so, each and every design pattern has an imaginary place. Perhaps because of this, there are three categories of patterns: creation, structure, and behavior. Then, each small area has different focuses.

This is a little bit of my understanding of the design model:
Learning design pattern: not global, not a certain domain.
Use the design pattern: knowing and knowing why.



An old song, sang by a man, and the lyrics are just as simple as you can, and then you can look back and look back at me again...

Let's look back at Jiang yuheng.

Among them, the lyrics are "@ $ still sound, and I still think about it again ..." What is the song name and lyrics?

Looking back
Original Song: Jiang yuheng
No matter how much pain and confusion tomorrow will face
I used to ask questions in the dark
Only then can we know that being cool and cool is the most authentic
Looking back, just like a dream
Looking back, my heart is still
Only the endless long path with me

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.