python设计模式-构建器(Builder)设计模式,原型设计模式

构建器模式用于创建复杂对象,通过分离构造过程和表示,提供了一种灵活的构造方法。它具有清晰的层次结构,可以更好地控制构建过程。原型模式则简化了实例化过程,通过复制已有对象来创建新对象,节省时间和资源。这两种模式都在对象创建中起到了关键作用。
摘要由CSDN通过智能技术生成

构建器(Builder)设计模式

构建器(Builder)模式是一种独特的设计模式,它有助于使用简单对象构建复杂对象并使用算法。 这种设计模式属于创建型模式。 在这种设计模式中,构建器类逐步构建最终对象。 该构建器独立于其他对象。

构建器(Builder)模式的优点

  • 它提供了清晰的分离和独特的层次,可以在构建和表示由类创建的指定对象之间进行表示。

  • 它可以更好地控制所建模式的实现过程。

  • 它提供了改变对象内部表示的场景。

如何实现构建器模式?

在本节中,我们将学习如何实现构建器模式。参考以下实现代码

class Director:
   __builder = None

   def setBuilder(self, builder):
      self.__builder = builder

   def getCar(self):
      car = Car()

      # First goes the body
      body = self.__builder.getBody()
      car.setBody(body)

      # Then engine
      engine = self.__builder.getEngine()
      car.setEngine(engine)

      # And four wheels
      i = 0
      while i < 4:
         wheel = self.__builder.getWheel()
            car.attachWheel(wheel)
         i += 1
      return car

# The whole product
class Car:
   def __init__(self):
      self.__wheels = list()
      self.__engine = None
      self.__body = None

   def setBody(self, body):
      self.__body = body

   def attachWheel(self, wheel):
      self.__wheels.append(wheel)

   def setEngine(self, engine):
      self.__engine = engine

   def specification(self):
      print "body: %s" % self.__body.shape
      print "engine horsepower: %d" % self.__engine.horsepower
      print "tire size: %d\'" % self.__wheels[0].size

class Builder:
      def getWheel(self): pass
      def getEngine(self): pass
      def getBody(self): pass

class JeepBuilder(Builder):

   def getWheel(self):
      wheel = Wheel()
      wheel.size = 22
      return wheel

   def getEngine(self):
      engine = Engine()
      engine.horsepower = 400
      return engine

   def getBody(self):
      body = Body()
      body.shape = "SUV"
      return body

# Car parts
class Wheel:
   size = None

class Engine:
   horsepower = None

class Body:
   shape = None

def main():
   jeepBuilder = JeepBuilder() # initializing the class

   director = Director()

   # Build Jeep
   print "Jeep"
   director.setBuilder(jeepBuilder)
   jeep = director.getCar()
   jeep.specification()
   print ""

if __name__ == "__main__":
   main()

执行上面程序,得到以下输出结果

原型设计模式

原型设计模式有助于隐藏该类创建实例的复杂性,在对象的概念将与从头创建的新对象的概念不同。

如果需要,新复制的对象可能会在属性中进行一些更改。这种方法节省了开发产品的时间和资源。

如何实现原型模式? 现在让我们看看如何实现原型模式。代码实现如下 -

import copy

class Prototype:

   _type = None
   _value = None

   def clone(self):
      pass

   def getType(self):
      return self._type

   def getValue(self):
      return self._value

class Type1(Prototype):

   def __init__(self, number):
      self._type = "Type1"
      self._value = number

   def clone(self):
      return copy.copy(self)

class Type2(Prototype):

   """ Concrete prototype. """

   def __init__(self, number):
      self._type = "Type2"
      self._value = number

   def clone(self):
      return copy.copy(self)

class ObjectFactory:

   """ Manages prototypes.
   Static factory, that encapsulates prototype
   initialization and then allows instatiation
   of the classes from these prototypes.
   """

   __type1Value1 = None
   __type1Value2 = None
   __type2Value1 = None
   __type2Value2 = None

   @staticmethod
   def initialize():
      ObjectFactory.__type1Value1 = Type1(1)
      ObjectFactory.__type1Value2 = Type1(2)
      ObjectFactory.__type2Value1 = Type2(1)
      ObjectFactory.__type2Value2 = Type2(2)

   @staticmethod
   def getType1Value1():
      return ObjectFactory.__type1Value1.clone()

   @staticmethod
   def getType1Value2():
      return ObjectFactory.__type1Value2.clone()

   @staticmethod
   def getType2Value1():
      return ObjectFactory.__type2Value1.clone()

   @staticmethod
   def getType2Value2():
      return ObjectFactory.__type2Value2.clone()

def main():
   ObjectFactory.initialize()

   instance = ObjectFactory.getType1Value1()
   print "%s: %s" % (instance.getType(), instance.getValue())

   instance = ObjectFactory.getType1Value2()
   print "%s: %s" % (instance.getType(), instance.getValue())

   instance = ObjectFactory.getType2Value1()
   print "%s: %s" % (instance.getType(), instance.getValue())

   instance = ObjectFactory.getType2Value2()
   print "%s: %s" % (instance.getType(), instance.getValue())

if __name__ == "__main__":
   main()

执行上面程序,将生成以下输出 -

输出中,使用现有的对象创建新对象,并且在上述输出中清晰可见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

资料小助手

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值