Factory design Pattern

Source: Internet
Author: User

This is different from the simple factory, the simple factory model has only one factory, the factory method model has the corresponding factory for each product

Benefit: Add an operation class (for example, N-Class), only need to increase the operation class and the corresponding factory, two classes, do not need to modify the factory class.

Cons: Adding an operation class will modify the client code, and the factory method simply moves the internal logic judgment of the simple factory to the client.

#!/usr/bin/Env python#-*-coding:utf-8-*-classOperation (Object):    """Abstract class"""
def getresult (self): Raise notimplemented
classADD (operation):"""Plus"""
def getresult (self, NUMA, numb):returnNuma +Numb

classOperationfactory (Object): """Factory class"""
def createoperation (self): Raise notimplemented
classaddfactory (operationfactory): def createoperation (self):returnAdd ()
if__name__ = ='__main__': obj=addfactory () ret=obj. CreateOperation () result= Ret. GetResult (2,5) print (result)

Factory design Pattern

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.