8. python-设计模式(build模式)

# -*- coding: UTF-8 -*-
class RequestThread():
    def __init__(self, build):
        self.messageClass = build.messageClass
        self.thread_name = build.thread_name
        self.base_url = build.base_url
        self.id_count = build.id_count
        self.offset = build.offset
        self.count = build.count
        self.batch_list = build.batch_list
        self.batch_size = build.batch_size
        self.success_count = build.success_count
        self.is_city = build.is_city
        self.log_error = build.log_error

    class Build:
        def __init__(self):
            self.messageClass = "meta"
            self.thread_name = "0"
            self.base_url = "baidu.com"
            self.id_count = 0
            self.offset = 1001
            self.count = 1000
            self.batch_list = []
            self.batch_size = 5000
            self.success_count = 0
            self.is_city = "黄浦区"
            self.log_error = True
        def set_messageClass(self,messageClass):
            self.messageClass = messageClass
            return self
        def set_thread_name(self,thread_name):
            self.thread_name = thread_name
            return self
        def set_base_url(self,base_url):
            self.base_url = base_url
            return self
        def set_id_count(self,id_count):
            self.id_count = id_count
            return self
        def set_offset(self,offset):
            self.offset = offset
            return self
        def set_count(self,count):
            self.count = count
            return self
        def set_batch_list(self,batch_list):
            self.batch_list = batch_list
            return self
        def set_batch_size(self,batch_size):
            self.batch_size = batch_size
            return self
        def set_success_count(self,success_count):
            self.success_count = success_count
            return self
        def set_is_city(self,is_city):
            self.count = is_city
            return self
        def set_log_error(self,log_error):
            self.log_error = log_error
            return self
        def build(self):
            return RequestThread(self)



requestThread = RequestThread.Build().set_base_url("xxx.live").set_batch_size(10000000).build()
print requestThread.offset

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
建造者模式是一种创建型设计模式,旨在通过将对象的构造与其表示分离来解决复杂对象的创建问题。该模式允许您按照步骤构建对象,同时也提供一种灵活的方式来创建不同类型的对象。 在Python中,建造者模式通常使用类和方法来实现。以下是一个基本的建造者模式示例: ```python class Computer: def __init__(self): self.__monitor = None self.__keyboard = None self.__mouse = None def set_monitor(self, monitor): self.__monitor = monitor def set_keyboard(self, keyboard): self.__keyboard = keyboard def set_mouse(self, mouse): self.__mouse = mouse class ComputerBuilder: def __init__(self): self.__computer = Computer() def get_computer(self): return self.__computer def build_monitor(self): monitor = "Dell Monitor" self.__computer.set_monitor(monitor) def build_keyboard(self): keyboard = "Logitech Keyboard" self.__computer.set_keyboard(keyboard) def build_mouse(self): mouse = "Microsoft Mouse" self.__computer.set_mouse(mouse) class Director: def __init__(self, builder): self.__builder = builder def build_computer(self): self.__builder.build_monitor() self.__builder.build_keyboard() self.__builder.build_mouse() builder = ComputerBuilder() director = Director(builder) director.build_computer() computer = builder.get_computer() ``` 在上面的示例中,我们定义了一个Computer类,它包含了电脑的一些属性,如显示器、键盘和鼠标等。我们还定义了一个ComputerBuilder类,它允许我们按照步骤构建Computer对象。最后,我们定义了一个Director类,它协调ComputerBuilder的构建过程。 当我们需要创建一个新的Computer对象时,我们只需创建一个ComputerBuilder实例并将其传递给Director类。Director类将协调ComputerBuilder的构建过程,并返回一个完整的Computer对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我先森

鼓励一个吧,哈哈

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值