preface:
Simple factory pattern and strategy pattern are two design patterns that most programmers come into contact with the earliest when learning design patterns or use more in work practice.
One is creative and the other is behavioral. However, there are some similarities between the two different types of models. At the same time, it can play a particularly good effect when combined in a certain scene.
Question:
I think the simple factory model is very similar to the strategic model. How similar? All three business subclasses inherit the abstract parent class. By passing in parameters to the container class (factory class of factory mode and Content class of policy mode), select the corresponding class for behavior operation.
In fact, UML diagrams are not very different in appearance, but they are very different in essence.
Simple factory mode
As mentioned above, the simple factory mode is the creation mode. As the name suggests, the creation mode is the design mode that will be selected only when the bottleneck is encountered when creating objects. So what should be used.
The essence of the simple factory pattern is that a factory class dynamically determines which instance of a product class (which inherits from a parent class or interface) should be created and returned according to the passed in parameters.
That is to say:
1. There are known product categories
2. You cannot know exactly which product class to compile
3. You need to decide which product class to create at runtime
4. There are not many product categories
Obviously, it has high flexibility in creating objects, but the factory class can only create product classes that may be used. If a new product class is added, the factory class must be modified, which will violate the opening and closing principle.
Strategy mode
Policy pattern is a behavioral pattern, which defines a series of algorithms, encapsulates each algorithm, and makes them replaceable with each other. The strategy pattern allows the algorithm to change independently of the customers who use it.
In a piece of code, logic control (if else, swich case) is used to determine the algorithm. The algorithm has similar methods and functions, so the strategy mode can be selected.
That is to say:
1. There are multiple conditional statements in a method, and there are many behavior processes in the conditional statement code block.
2. Its algorithm can be encapsulated into policy class
2. Algorithm switching at will
3. The algorithm is isolated from the client
In this way, select the corresponding policy class and transfer it to the Content class as a parameter to configure the corresponding algorithm at runtime.
Difference summary
It can be concluded from the above description that both are configured by passing in parameters at runtime. The simple factory mode is to select and create the required objects, while the policy mode is to configure the required behavior algorithms. One is object creation, the other is behavior algorithm replacement.
combination
Here is a section of code for policy mode.
using System; using System.Net.Configuration; namespace StrategyWithFactory { class Program { static void Main(string[] args) { Strategy strategyContent = null; //Pseudo code. Get input algorithm type EStrategy inputType = RequestInput(); if (inputType == EStrategy.A) { new Content(new StrategyA()).ContentInterface(); } else if (inputType == EStrategy.B) { new Content(new StrategyB()).ContentInterface(); } else if (inputType == EStrategy.C) { new Content(new StrategyC()).ContentInterface(); } } } //Algorithm Abstract Class abstract class Strategy { public abstract void AlfoeirhmInterface(); } //A algorithm class class StrategyA : Strategy { public override void AlfoeirhmInterface() { Console.WriteLine("this is the StrategyA"); } } //Class B algorithm class StrategyB : Strategy { public override void AlfoeirhmInterface() { Console.WriteLine("this is the StrategyB"); } } //Class B algorithm class StrategyC : Strategy { public override void AlfoeirhmInterface() { Console.WriteLine("this is the StrategyC"); } } //Context class class Content { private readonly Strategy _strategy; public Content(Strategy strategy) { _strategy = strategy; } public void ContentInterface() { _strategy.AlfoeirhmInterface(); } } //Algorithm enumeration enum EStrategy { A = 1, B = 2, C = 3 } }
The above code is the prototype of the policy mode. If the Main function is the client, then every algorithm added in the future will have to be modified on the client and an else if will be added, causing unnecessary trouble. Well, in the current situation, first of all, we know the three existing ABC algorithms, but we are not sure which algorithm to use at runtime. At the same time, in order to separate the client from the business logic code, we can transfer the business logic of the client to the Cotent class and add a method to create the algorithm factory.
using System; namespace StrategyWithFactory { class Program { static void Main(string[] args) { //Pseudo code. Get input algorithm type EStrategy inputType = RequestInput(); new Content(inputType).ContentInterface(); } } //Context class class Content { private readonly Strategy _strategy; public Content(EStrategy eStrategy) { _strategy = CreateFactory(eStrategy); } public Strategy CreateFactory(EStrategy eStrategy) { Strategy strategy = null; switch (eStrategy) { case EStrategy.A: strategy = new StrategyA(); break; case EStrategy.B: strategy = new StrategyB(); break; case EStrategy.C: strategy = new StrategyC(); break; } return strategy; } public void ContentInterface() { _strategy.AlfoeirhmInterface(); } } }
Then, the combined application of strategy and simple factory is realized.
Author: Gong Chen
Origin: http://www.cnblogs.com/skychen1218/
About the author: focus on the project development of Microsoft platform. If you have any questions or suggestions, please give me more advice!
Copyright notice: the copyright of this article belongs to the author and the blog park. Reprint is welcome, but this paragraph must be retained without the consent of the author, and the original link must be given in an obvious position on the article page.
Support bloggers: if you think the article is helpful to you, you can click on the lower right corner of the article to recommend it. Your encouragement is the greatest driving force for the author to adhere to originality and continuous writing!