如果类有两个组件类,如何使构造函数清楚

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果类有两个组件类,如何使构造函数清楚相关的知识,希望对你有一定的参考价值。

如何设计Manager的构造函数更干净?这个问题有什么模式吗?

class Manager():
  def __init__(self, a_1, a_2, a_3, b_1, b_2, b_3):
    self.a = A(a_1, a_2, a_3)
    self.b = B(b_1, b_2, b_3)

class A():
  def __init__(self, a_1, a_2, a_3):
    pass

class B():
  def __init__(self, b_1, b_2, b_3):
    pass
答案

我可以想到两个合适的选择:

  1. dependency injection

将对象的实例作为构造函数参数传递。您修改的构造函数将如下所示:

...
def __init(self, A, B):
  self.a = A
  self.b = B
...
  1. builder pattern和可能fluent interface

它有点复杂,但涵盖了扩展到几种类型的可构建对象。

这是一个例子:

# source: https://gist.github.com/pazdera/1121157

class Director:

    """ Controls the construction process.
    Director has a builder associated with him. Director then
    delegates building of the smaller parts to the builder and
    assembles them together.
    """

    __builder = None

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

    # The algorithm for assembling a car
    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:

    """ The final product.
    A car is assembled by the `Director' class from
    parts made by `Builder'. Both these classes have
    influence on the resulting object.
    """

    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:

    """ Creates various parts of a vehicle.
    This class is responsible for constructing all
    the parts for a vehicle.
    """

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


class JeepBuilder(Builder):

    """ Concrete Builder implementation.
    This class builds parts for Jeep's SUVs.
    """

    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

class NissanBuilder(Builder):

    """ Concrete Builder implementation.
    This class builds parts for Nissan's family cars.
    """

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

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

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

# Car parts
class Wheel:
    size = None

class Engine:
    horsepower = None

class Body:
    shape = None

def main():
    jeepBuilder = JeepBuilder()
    nissanBuilder = NissanBuilder()

    director = Director()

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

    print ""

    # Build Nissan
    print "Nissan"
    director.setBuilder(nissanBuilder)
    nissan = director.getCar()
    nissan.specification()

if __name__ == "__main__":
    main()

以上是关于如果类有两个组件类,如何使构造函数清楚的主要内容,如果未能解决你的问题,请参考以下文章

如果类有一个带有他的名字的方法(函数),为啥要调用构造函数?

如何使类工厂创建所需的派生类

C#中子类构造函数中如何调用父类构造函数

C# 抽象类有构造函数吗?

这个嵌套类构造函数片段可以应用于泛型类吗?

在java语言里如何在子类中调用父类的有参构造函数