Design patterns, really very much like a long, we even evolved from the direct evolution of class diagrams that exist in class diagrams.
Some of them, like the class diagram. Some languages are approximate, but it has a different name, different "waistcoat". Let's be quick to recognize.
First Group: Template Method pk Builder Mode PK factory method
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmde3njaxna==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" "height=" >
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmde3njaxna==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" 340 "height=" 279 ">
Template method Builder Mode factory method
Comparison of the three: again, the main idea is to define a framework. Then defer some steps to the child class.
Different points, first. The template method pattern is a behavioral pattern. The Builder mode factory method is the creation mode.
22 Comparison: Template vs Builder
Builder mode using template method, The two of them use scope Differentiate depends on whether the algorithm you build needs to create a different class.
Builders vs Factory method
in the builder mode diagram. If this command class is considered to be the client that is finally called, then the remainder of the figure can be regarded as single Factory mode. In contrast to Factory mode, the builder pattern is more complex when the object is created, so the creation of the object is made independent of the process. A new class--command class.
That is, Factory mode encapsulates all creation of an object in a factory class, from factory class to Client
Is the responsibility of the command class to assemble individual components into a product according to specific rules, and then deliver the assembled product to the client.
Second set of State graphs vs strategy diagrams
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmde3njaxna==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" "height=" >
State diagram strategy diagram
Emma, this is exactly the same. ~ How many, like me, did not find the first time. But since it is exactly the same, why do you say it again two times? If we say something different, I'm afraid it's hard to answer from this picture.
Let's analyze it in detail from other angles.
Let's look at a lot of similarities between them:
- Adding new states or policies is very easy, and there is no need to change the context object that uses them.
- In state mode and policy mode, the context object is closed for changes. There is no need to change the context to add a new state or strategy.
- Just as the context in state mode has an initial state, the policy mode has the same default policy.
- State patterns encapsulate different behaviors in different states. The policy pattern encapsulates different behaviors with different policies.
- They all rely on subclasses to implement related behaviors.
To come down to see the difference in detail
- The state pattern encapsulates the state of the object, while the policy pattern encapsulates the algorithm or policy. Because the state is closely related to the object, it cannot be reused, and by separating the policies or algorithms from the context, we can reuse them.
- In the state mode. Each state is transferred by holding a reference to the context, but each strategy does not hold a reference to the context. They are only used by the context.
- The order of state transitions is very well defined in the state mode, and there is no need for the strategy mode: The client is free to choose whatever strategy.
The
third set of proxy modes vs decorative modes
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmde3njaxna==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "width=" "height=" >
Proxy mode decoration mode
Just look at the class diagram, feeling that the two are not very similar.
But careful classmates will also find that the grammatical form of the two almost identical.
Let's look at a lot of similarities between them:
- For adorner mode, the decorator (decorator) and the decorator (decoratee) all implement the same interface. For proxy mode, the proxy class and the Real processing class (Realclass) all implement the same interface.
- Regardless of which pattern we use, it is very easy to add your own defined methods in front of or behind the methods of real objects.
to come down to see the difference in detail
- the difference is essentially is that the adornment mode should be enhanced for the adorned object, and proxy mode imposes control on the object of the agent. Does not provide enhanced functionality for the object itself.
- and one difference is that proxy mode, for the proxy class is cured in the proxy class, and for the adorner pattern, the decorated class is invoked when instantiated, that is, the decorated class can be dynamically selected at the time of invocation.
Summary
In addition Bridging mode and adapter mode are also comparable. The first time I learned the design pattern, I know. Each design pattern is not independent. Following the 6 principles and the actual need for change and encapsulation, we have the flexibility to choose and apply it.
"Big Talk design mode" learning is over. It means that the design pattern starts to really come into our lives.
The whole process was a lot of my harvest. Contains an understanding of the class diagram. Each relationship and the transformation from the class diagram to the code. There is a constant understanding of change. Design patterns, let me believe that learning comes from life. The more you combine life, the better you can understand what you learn. As for the inadequacy is, the design pattern understanding is not enough in place, also waits in the practice to apply, many experiences.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Design mode article summary find contact controls