Expand knowledge

Spread

 

 

Factory Pattern

The second method constraints - defines an abstract class

Resume CMDB summary

 

 

 

Factory Pattern

  • Simple mode

    Definition: factory class provides an interface, and to create the corresponding instance of the object based on the client passed parameters. (Create an object)
    They know: only need to change get_flower method FlowerSimpleFactory class incoming class can get the data you want, *** the data is direct subclass
    from abc import ABCMeta, abstractmethod
     
     
    class Flower(metaclass=ABCMeta):
     @abstractmethod
     def show_price(self):
      pass
     
     
    class Rose(Flower):
     def show_price(self):
      print('Rose price: $99')
     
     
    class Tulip(Flower):
     def show_price(self):
      print('Tulip price: $66')
     
     
    class FlowerSimpleFactory:
     def get_flower(self, flower_type):
      return flower_type()
     
     
    if __name__ == '__main__':
     flower_factory = FlowerSimpleFactory()
     rose = flower_factory.get_flower('Rose')
     tulip = flower_factory.get_flower('Tulip')
     rose.show_price()
     tulip.show_price()
     
  • Factory Method pattern

    Definition: The need to define a base class, the different subclasses represent different types of objects. Pattern relative to the simple factory, factory methods having more customizable. (Create an object)
     His understanding: I just need to change the code FlowerShop1 and FlowerShop2 class methods to get different results, **** subclasses decide
    from abc import ABCMeta, abstractmethod
     
     
    class Flower(metaclass=ABCMeta):
     @abstractmethod
     def show_price(self):
      pass
     
     
    class Rose(Flower):
     def show_price(self):
      print('Rose price: $99')
     
     
    class Tulip(Flower):
     def show_price(self):
      print('Tulip price: $66')
     
     
    class Lily(Flower):
     def show_price(self):
      print('Lily price: $33')
     
     
    class FlowerShopFactory(metaclass=ABCMeta):
     def __init__(self):
      self.flowers = []
      self.stock_flowers()
     
     @abstractmethod
     def stock_flowers(self):
      pass
     
     def get_flowers(self):
      return self.flowers
     
     def add_flower(self, flower):
      self.flowers.append(flower)
     
     
    class FlowerShop1(FlowerShopFactory):
     def stock_flowers(self):
      self.add_flower(Rose())
      self.add_flower(Tulip())
     
     
    class FlowerShop2(FlowerShopFactory):
     def stock_flowers(self):
      self.add_flower(Rose())
      self.add_flower(Tulip())
      self.add_flower(Lily())
     
     
    if __name__ == '__main__':
     flower_shop1 = FlowerShop1()
     for flower in flower_shop1.get_flowers():
      flower.show_price()
     
     flower_shop2 = FlowerShop2()
     for flower in flower_shop2.get_flowers():
      flower.show_price()
    
    
    Rose price: $99
    Tulip price: $66
    Rose price: $99
    Tulip price: $66
    Lily price: $33
     
  • Abstract factory pattern

    Definition: The need to define an abstract factory class, and then to create a different set of objects by different sub-classes, a series which represents a group of objects. (Create a set of objects)
     Own understanding: With more abstract class method defined subclasses can inherit the functions can be more, **** parent achievement subclass
    from abc import ABCMeta, abstractmethod
     
     
    class MiniCar(metaclass=ABCMeta):
     @abstractmethod
     def show_size(self):
      pass
     
     
    class SedanCar(metaclass=ABCMeta):
     @abstractmethod
     def show_price(self):
      pass
     
     
    # 国产车
    class DomesticMiniCar(MiniCar):
     def show_size(self):
      print('Domestic mini car size: 111')
     
     
    class DomesticSedanCar(SedanCar):
     def show_price(self):
      print('Domestic sedan car price: 10W')
     
     
    # 英国车
    class EnglishMiniCar(MiniCar):
     def show_size(self):
      print('English mini car size: 222')
       
     
    class EnglishSedanCar(SedanCar):
     def show_price(self):
      print('English sedan car price: 30w')
     
     
    # Abstract factory class 
    class CarFactory (= the metaclass that ABCMeta):
     @abstractmethod
     def create_mini_car(self):
      pass
     
     @abstractmethod
     def create_sedan_car(self):
      pass
     
     
    # Domestic car factory class 
    class DomesticCarFactory (CarFactory):
      DEF create_mini_car (Self):
       return DomesticMiniCar ()
      
     def create_sedan_car(self):
      return DomesticSedanCar()
     
     
    # 英国车
    class EnglishCarFactory(CarFactory):
     def create_mini_car(self):
      return EnglishMiniCar()
      
     def create_sedan_car(self):
      return EnglishSedanCar()

     

The second method constraints - defines an abstract class

import abc

# Abstract 
class Base (= the metaclass that abc.ABCMeta):
    
    # 抽象方法
    @abc.abstractmethod
    def process(self):
        pass
class Foo(Base):
    pass
Foo()

 

Resume CMDB summary

  • project name

    CMDB / Asset Management system / server configuration management system / operation and maintenance automation platform
  • project description

    CMDB is a project for the automated collection of asset information server, because the company for asset maintenance costs are high and the accuracy of the data getting lower and lower, because they are actually built a samba service, excel within the shared realization. Cmdb can be improved by the project assets acquired functions, improve work efficiency to reduce personnel costs, the project has achieved major acquisition in the control machine / restful api / asset management and control platform. 5
     For acquisition control unit can support multiple operating modes, such as: saltstack / ansible / paramiko default, and follows the principle of an open and closed during development using the factory mode scalability insert.
     For api, it is to follow the strict norms restful and use django rest framework framework implementation, and processing assets, including the achievement of lasting change records and assets through reflection.
     Asset management and control platform mainly provides data and support for the operation and maintenance section of the report and director, excel bulk import and export support, support the use of the timeline clearly shows the server life cycle, based on data showing highcharts implement the report.
  • Technical points (Item Function / my duty)

    • For different business development, use paramiko / ansible / saltstack achieve the expansion remote collection of assets.

    • Reference source middleware implemented in conjunction with the plant model, a pluggable developed asset acquisition plug.

    • Considering the rigor of the project, a project for the use of plug-active thrown restrained.

      - There are no other constraints?
           Abc implementation constraints by abstract classes and implement the abstract methods.
       - Why do not you abc? But with an exception?
           abc operate more trouble, it can also be achieved. 
           I think it will be more concise thrown some and I made reference to some source, they are also internal abnormalities achieve. 
       import abc
       # Abstract 
       class Base (= the metaclass that abc.ABCMeta):
           
           # 抽象方法
           @abc.abstractmethod
           def process(self):
               pass
       class Foo(Base):
           pass
       Foo()
    • Built-in authentication components through customized and extended drf, user authentication.

    • Achieve api / version / authentication functions in restful api.

    • The most import support for batch export server assets, internal only operate with xlrd / xlwt module.

    • For the company's assets do server processes data reports based on business lines.

    • Rbac achieve parity information based on privileges.

back to the top

Guess you like

Origin www.cnblogs.com/NiceSnake/p/11918988.html