The pros and cons of simple factory and policy models and their differences

Source: Internet
Author: User

First, simple Factory mode

  Advantages:

The separation of object creation and use is realized.

The client does not need to know the class name of the specific product class created, just need to know the corresponding parameters of the specific product class;

By introducing a configuration file, you can change and add new product classes without modifying any client code, to some extent, improve the flexibility of the system.

Disadvantages:

Factory class centralized all the product creation logic, the responsibility is too heavy, once unable to work properly, the entire system must be affected;

Increase the number of classes in the system (introduce a new factory class), increase the complexity of the system and the difficulty of understanding;

System expansion difficult, once added new products have to modify the factory logic;

Factory classes do not scale well because the static factory method is used, resulting in the inability of the factory role to form an inheritance-based hierarchy.

Second, the Strategy model

  Advantages:

The strategy mode provides the perfect support for the "open and close principle", the user can choose the algorithm and the behavior on the basis of not modifying the original system, also can add the new algorithm and the behavior flexibly.

The policy pattern provides a way to manage the associated algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors. Proper use of inheritance can transfer common code into the parent class, thus avoiding duplicate code;

The policy pattern provides a way to replace the inheritance relationship. Inheritance can handle multiple algorithms or behaviors. If the policy mode is not used, then the environment class using the algorithm or behavior may have some subclasses, each of which provides a different algorithm or behavior. However, the user of the algorithm or behavior is mixed with the algorithm or the behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, so that it is impossible to evolve independently. Inheritance makes it impossible to dynamically change an algorithm or behavior;

Use the policy mode to avoid using multiple conditional transfer statements. Multiple transfer statements are difficult to maintain, and it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or the action, all in a multiple transfer statement, more primitive and backward than the method of using inheritance.

  Disadvantages:

The client must know all the policy classes and decide for themselves which policy class to use. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows all the algorithms or behaviors.

Three, the difference between the two

  Structure:

Structurally, the two are similar in that they define the base class (or interface) and, depending on the situation, new instances are different.

  Focus Point:

Their focus is different, the former focuses on the creation of objects, the latter focuses on the choice of behavior.

  The problem solved:

The Factory mode is a design pattern of creation, it accepts the instruction, creates the instance which conforms to the requirement, it mainly solves the unified distribution of the resources, the creation of the object is completely independent, so that the object creation and the specific use of the customer is irrelevant. Main application in multi-database selection, class library file loading and so on.

   The strategy mode is to solve the policy switching and extension, more concise is to define the policy family, respectively encapsulated, so that they can replace each other, the policy mode to make the policy changes independent of the customers using the policy.

  An interesting example of a netizen:    

Factory mode: Depending on the purpose you are giving to produce axes for different purposes, such as cutting people, the factory produces cut-down axes and loggers to produce lumbering axes.

A class of objects that produce different behaviors based on the attributes you give them back to you.

Focus on object creation

Strategy: Use a factory-produced axe to do the right thing, such as chopping a man with an axe and logging with a lumbering axe.

The corresponding method is executed according to the object you are given.

The choice of attention behavior

This article refers to Liu Wei's editor-in-chief's design pattern books.

Reference: http://www.cnblogs.com/me115/p/3790615.html

Thanks for their dedication.

The pros and cons of simple factory and policy models and their differences

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.