Simple Factory mode-python

Source: Internet
Author: User

1. Typical example of a simple factory model is a calculator

def create_operator(op):    if op == ‘+‘: return AddOperation() elif op == ‘-‘: return SubOperation() elif op == ‘*‘: return MulOperation() elif op == ‘/‘: return DivOperation()op = create_operator(‘+‘)op.Calc(1, 2)


Components of the Simple factory model
    • Factory functions

Responsible for the creation of specific product objects, is the core of the model. When the scene is more complex, you may need to use a factory class to create the product.

    • Abstract interface or abstract class for a product

Abstract common interfaces for all products

    • Specific Product Categories

Concrete implementation classes for abstract interfaces

Disadvantages
    • Because the factory class centralizes the creation logic of all instances, it violates the principle of cohesion responsibility allocation and centralizes all creation logic into a factory class.
    • The classes it can create can only be considered in advance, and if you need to add new classes, you need to change the factory class.
    • As the number of specific product classes in the system increases, there may be a need for factory classes to create different instances based on different conditions. This kind of judgment of the condition and the judgment of the specific product type staggered together, it is difficult to avoid the spread of the module function, the maintenance and expansion of the system is very unfavorable;

Simple Factory mode-python

Related Article

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.