码迷,mamicode.com
首页 > 编程语言 > 详细

《精通Python设计模式》学习之建造者模式

时间:2018-08-03 14:27:05      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:enum   ret   and   The   col   分享   exce   orm   input   

这种模式,就是将一个最终对象分级分层建造出来。

在软件发布过程中,不同的JAVA,PYTHON,NODE.JS,ZIP压缩包,

就可以使用不同的阶段来使用建造者模式的。

技术分享图片

from enum import Enum
import time

PizzaProgress = Enum(PizzaProgress, queued preparation baking ready)
PizzaDough = Enum(PizzaDough, thin thick)
PizzaSauce = Enum(PizzaTopping, tomato creme_fraiche)
PizzaTopping = Enum(PizzaTopping, mozzaralla double_mozzarella bacon ham mushrooms red_onion oregano)
STEP_DELAY = 3

class Pizza:
    def __init__(self, name):
        self.name = name
        self.dough = None
        self.sauce = None
        self.topping = []

    def __str__(self):
        return self.name

    def prepare_dough(self, dough):
        self.dough = dough
        print(preparing the {} dough of your {}....format(self.dough.name, self))
        time.sleep(STEP_DELAY)
        print(done with the {} dough.format(self.dough.name))

class MargaritaBuilder:
    def __init__(self):
        self.pizza = Pizza(margarita)
        self.progress = PizzaProgress.queued
        self.baking_time = 5

    def prepare_dough(self):
        self.progress = PizzaProgress.preparation
        self.pizza.prepare_dough(PizzaDough.thin)

    def add_sauce(self):
        print(adding the tomato sauce to your margarita...)
        self.pizza.sauce = PizzaSauce.tomato
        time.sleep(STEP_DELAY)
        print(done with the tomato sauce)

    def add_topping(self):
        print(adding the topping (double mozzarella, oregano) to your margarita)
        self.pizza.topping.append([i for i in
                                   (PizzaTopping.double_mozzarella, PizzaTopping.oregano)])
        time.sleep(STEP_DELAY)
        print(done with the topping (double mozzarrella, oregano))

    def bake(self):
        self.progress = PizzaProgress.baking
        print(baking your margarita for {} seconds.format(self.baking_time))
        time.sleep(self.baking_time)
        self.progress = PizzaProgress.ready
        print(your margarita is ready)

class CreamyBaconBuilder:
    def __init__(self):
        self.pizza = Pizza(creamy bacon)
        self.progress = PizzaProgress.queued
        self.baking_time = 7

    def prepare_dough(self):
        self.progress = PizzaProgress.preparation
        self.pizza.prepare_dough(PizzaDough.thick)

    def add_sauce(self):
        print(adding the creme fraiche sauce to you creamy bacon)
        self.pizza.sauce = PizzaSauce.creme_fraiche
        time.sleep(STEP_DELAY)
        print(done with the creme fraiche sauce)

    def add_topping(self):
        print(adding the topping (mozzaralla, bacon, ham, mushrooms, red onion, oregano) to you creamy bacon)
        self.pizza.topping.append([t for t in
                                   (PizzaTopping.mozzaralla, PizzaTopping.bacon,
                                    PizzaTopping.ham, PizzaTopping.mushrooms,
                                    PizzaTopping.red_onion, PizzaTopping.oregano)])
        time.sleep(STEP_DELAY)
        print(done with the topping(mozzarella, bacon, ham, mushrooms, red onion, oregano))

    def bake(self):
        self.progress = PizzaProgress.baking
        print(baking your creamy bacon for {} seconds.format(self.baking_time))
        time.sleep(self.baking_time)
        self.progress = PizzaProgress.ready
        print(your creamy bacon is ready)

class Waiter:
    def __init__(self):
        self.buider = None

    def construct_pizza(self, builder):
        self.builder = builder
        [step() for step in (builder.prepare_dough,
                             builder.add_sauce, builder.add_topping, builder.bake)]

    @property
    def pizza(self):
        return self.builder.pizza

def validate_style(builders):
    try:
        pizza_style = input(What pizza would you like, [m]argarita or [c]reamy bacon,)
        builder = builders[pizza_style]()
        valid_input = True
    except KeyError as err:
        print(Sorry , only margarita (key m) and creamy bacon (key c) are available)
        return (False, None)
    return (True, builder)

def main():
    builders = dict(m=MargaritaBuilder, c=CreamyBaconBuilder)
    valid_input = False
    while not valid_input:
        valid_input, builder = validate_style(builders)
    print()
    waiter = Waiter()
    waiter.construct_pizza(builder)
    pizza = waiter.pizza
    print()
    print(Enjoy your {}!.format(pizza))

if __name__ == __main__:
    main()

技术分享图片

《精通Python设计模式》学习之建造者模式

标签:enum   ret   and   The   col   分享   exce   orm   input   

原文地址:https://www.cnblogs.com/aguncn/p/9413223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!