Python design pattern, creating simple Factory mode

Source: Internet
Author: User

This system, feel refreshing, believe in more practice, think more, will be familiar with.

http://www.jianshu.com/p/2450b785c329

#!/usr/bin/evn python#Coding:utf8classPizza (object):defPrepare (self, type):Print 'Prepare {type} pizza'. Format (type=type)defBake (self, type):Print 'Bake {type} pizza'. Format (type=type)defCut (self, type):Print 'cut {type} pizza'. Format (type=type)defbox (self, type):Print 'box {type} pizza'. Format (type=type)classCheesepizza (Pizza):def __init__(self): Self.name="Cheese Pizza"classClampizza (Pizza):def __init__(self): Self.name="Clam Pizza"classVeggiepizza (Pizza):def __init__(self): Self.name="Viggie Pizza"classSimplepizzafactory (object):defCreate_pizza (self, type): Pizza=NoneifType = ="Cheese": Pizza=Cheesepizza ()elifType = ="Clam": Pizza=Clampizza ()elifType = ="Veggie": Pizza=Veggiepizza ()returnPizzaclassPizzastore (object):def __init__(self, Factory): Self.factory=FactorydefOrder_pizza (self, type): Pizza=Self.factory.create_pizza (type) pizza.prepare (type) pizza.bake (type) pizza.cut (type) Pizz A.box (type)returnPizzaif __name__=='__main__': Store=Pizzastore (Simplepizzafactory ()) Pizza= Store.order_pizza ('Cheese')    PrintPizza.name Pizza= Store.order_pizza ('Clam')    PrintPizza.name Pizza= Store.order_pizza ('Veggie')    PrintPizza.name

Python design pattern, creating simple Factory mode

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.