Builder mode

Introduction

In the software system, sometimes it is faced with the creation of "a complex object", which is usually composed of sub objects of each part with a certain algorithm; due to the change of requirements, each part of this complex object is often faced with dramatic changes, but the algorithm that combines them is relatively stable.

concept

Builder pattern is a unique design pattern, which is helpful to use simple objects to construct complex object usage algorithms.

Class diagram

Example analysis

For example, mobile phone is composed of screen, motherboard, processor, etc., but these parts of mobile phone are variable.

Different models of mobile phones use different components; for example, the processors include MediaTek, Qualcomm, NVIDIA, etc.

The same is true for motherboards and mobile screens.

Complex object

In this case, the complex object refers to the mobile phone, which is composed of multiple sub objects.

Change part

The changing part refers to the sub objects that make up the mobile phone. They are variable.

For example, the processors include MediaTek, Qualcomm, NVIDIA, etc.

Algorithm part

Suppose that all mobile phones are constructed as follows: motherboard - > processor - > Screen - > finish

The main board is equipped with a processor and then a mobile phone is installed on the screen.

Implementation code

class Mobile:
    def __init__(self):
        self.__cpu = None
        self.__screen = None
        self.__master = None

    def set_cpu(self, value):
        self.__cpu = value

    def set_screen(self, value):
        self.__screen = value

    def set_master(self, value):
        self.__master = value

    def __repr__(self):
        return f' <Mobile(master={self.__master}screen={self.__screen}cpu={self.__cpu})>'


class AbsMobileBuild(abc.ABC):

    def __init__(self):
        self._mobile = Mobile()

    # CPU Part
    @abc.abstractmethod
    def set_cpu(self):
        pass

    # Motherboard part
    @abc.abstractmethod
    def set_master(self):
        pass

    # Screen part
    @abc.abstractmethod
    def set_screen(self):
        pass

    def get_result(self) -> Mobile:
        return self._mobile


class MobileDirector:

    def __init__(self, build: AbsMobileBuild):
        self.build = build

    def __call__(self) -> Mobile:
        self.build.set_master()
        self.build.set_cpu()
        self.build.set_screen()
        return self.build.get_result()


class XiaoMiMobileBuild(AbsMobileBuild):
    def set_cpu(self):
        self._mobile.set_cpu("millet CUP")

    def set_master(self):
        self._mobile.set_master("Millet motherboard")

    def set_screen(self):
        self._mobile.set_screen("Millet screen")


class HuaWeiMiMobileBuild(AbsMobileBuild):
    def set_cpu(self):
        self._mobile.set_cpu("HUAWEI CUP")

    def set_master(self):
        self._mobile.set_master("HUAWEI motherboard")

    def set_screen(self):
        self._mobile.set_screen("HUAWEI screen")


if __name__ == "__main__":
    build = XiaoMiMobileBuild()
    director = MobileDirector(build)
    mobile = director()
    print(mobile)

    build1 = HuaWeiMiMobileBuild()
    director1 = MobileDirector(build1)
    mobile1 = director1()

    print(mobile1)

Keywords: Python Mobile

Added by simonp on Thu, 28 Nov 2019 23:22:20 +0200