Singleton design pattern and plant

Singleton

1, only one singleton class instance.
2, singleton class must create their own unique instance.
3, singleton class must provide this example to all other objects.

"" " 
__New__ ensure that the only memory 
__init__ to initialize the only guarantee 
defaults parameter passing function will only create an in-memory 
" "" 


class Sington: 
    __has = None 
    __firstInit = True 

    DEF __new __ (CLS, * args, ** kwargs) : 
        IF CLS has .__ None IS: 
            CLS has .__ = Super (Sington, CLS) .__ new new __ (CLS, args *, ** kwargs) 
        return CLS has .__ 

    DEF the __init __ (Self, Cart = []): 
        self.cart = Cart 

    DEF the __init __ # (Self): 
    # IF # Self .__ firstInit: 
    # # Sington.cart = [] 
    # # Sington .__ firstInit = False 


C1 = Sington () 
C2 = Sington () 
c1.cart.append ( "ACB")  
C1 .cart.append ( "efg")
C2 .cart.append ( "efg")

print(hex(id(c1)))
print(hex(id(c2)))
print(c1.cart)
print(c2.cart)

 Simple factory pattern: factory class, according to the transmission parameters to create an instance of the corresponding

Simple plant # 
# define a simple factory Factory class, instances of different classes may be returned in accordance with different parameters, 
instances being created # usually have a common parent class. Essence is a factory class based on the parameters passed, dynamic decision should create a product which was built # class instance. 
Car class (Object): 
    DEF RUN (Self): 
        Print ( "bar where it was run") 
    DEF STOP (Self): 
        Print ( "Cengceng parking") 


class BMW (Car): 
    DEF RUN (Self): 
        print ( "BMW ---> right in it in the run") 
    DEF STOP (Self): 
        print ( "BMW ---> Cengceng parking") 


class Benz (Car): 
    DEF rUN (Self): 
        Print ( 'Mercedes-Benz - >>> bar where it was run ...') 

    DEF STOP (Self): 
        Print ( 'Mercedes - >>> Cengceng parking ...') 


class Skoda (Car): 
    DEF rUN (Self): 
        Print ( 'Skoda - >>> bar where it was run ...'



class CarFactory():
    def new_cart(self,name):
        if name == "BMW":
            return BMW()
        elif name == "Benz":
            return Benz()
        elif name == "skd":
            return Skoda()


class CarStore():
    def __init__(self,factory):
        self.factory = factory

    def order(self,name):
        return self.factory.new_cart(name)


car_factory = CarFactory()
car_store = CarStore(car_factory)
car = car_store.order('skd')
car.run()
car.stop()

 2. Factory Method design pattern

The factory class factory method to improve, enhance an abstract class (an interface), the specific product to achieve specific subclass corresponding to do business logic decoupling between the plurality of products.
abc Import ABCMeta from, AbstractMethod 


class Car (Object, the metaclass that = ABCMeta): 
    @abstractmethod 
    DEF RUN (Self): 
        Pass 

    @abstractmethod 
    DEF STOP (Self): 
        Print ( "Cengceng parking") 


class BMW (Car): 
    DEF RUN (Self): 
        Print ( "BMW ---> right in it in the run") 

    DEF STOP (Self): 
        Print ( "BMW ---> Cengceng parking") 


class Benz (Car): 
    DEF rUN (Self ): 
        Print ( 'Mercedes - >>> bar where it was run ...') 

    DEF STOP (Self): 
        Print ( 'Mercedes - >>> Cengceng parking ...') 


class Skoda (Car ): 
    DEF rUN (Self): 
        Print ( 'Skoda - >>> bar where it was run ...') 

    DEF STOP (Self):
        print ( 'Skoda - >>> Cengceng parking ...')


class Factory(metaclass=ABCMeta):
    @abstractmethod
    def create_car(self):
        pass


class BMWFactory(Factory):
    def create_car(self):
        return BMW()


class BenzFactory(Factory):
    def create_car(self):
        return Benz()


class SkodaFactory(Factory):
    def create_car(self):
        return Skoda()


def test():
    bmw = BMWFactory().create_car()
    bmw.run()
    bmw.stop()


test()

 3. Abstract Factory Design Pattern

The so-called abstract factory abstract factory refers to a plant hierarchy can create all the objects in a product family belong to different product hierarchy of the 
abstract factory:
1. Abstract Factory main purpose is to provide an interface for creating families of related objects without specify a specific class.
We need to specify what objects created before 2. Compared to, do not need abstract factory.
abc Import ABCMeta from, AbstractMethod 


class PizzaFactory (the metaclass that = ABCMeta): 
    # vegetables pizza 
    @abstractmethod 
    DEF create_veg_pizza (Self): 
        Pass 

    # not pizza vegetables 
    @abstractmethod 
    DEF create_non_veg_pizza (Self): 
        Pass 


class USAPizzaFactory (PizzaFactory): 
    # USA pizza pizza is a vegetable store corn pizza 
    DEF create_veg_pizza (Self): 
        return CornPizza () 

    # USA store vegetable pizza is not pizza beef 
    DEF create_non_veg_pizza (Self): 
        return BeefPizza () 


class ChinaPizzaFactory (PizzaFactory): 
    # China pizza there is a pizza shop vegetable fruit pizza 
    DEF create_veg_pizza (Self): 
        return FruitsPizza ()

    # Chinese vegetable pizza shop pizza is not pizza lamb 
    DEF create_non_veg_pizza (Self): 
        return MuttonPizza () 


# Next, define four kinds of pizza and their parent (there are no vegetables and vegetables) 
class VegPizza (the metaclass that = ABCMeta): 
    @ AbstractMethod 
    DEF PREPARE (Self, veg_pizza): 
        Pass 


# not vegetable pizza at the pizza on top of the vegetables can be added to meat 
class NonVegPizza (the metaclass that = ABCMeta): 
    @abstractmethod 
    DEF serve (Self, veg_pizza): 
        Pass 


class CornPizza (VegPizza): 
    DEF PREPARE (Self): 
        Print (of the type (Self) .__ name__, 'come') 


class BeefPizza (NonVegPizza): 
    DEF serve (Self, veg_pizza): 
        Print (of the type (Self) .__ name__, 'come, beef plus in ', type (veg_pizza) .__ name__ ,' inside ')


class FruitsPizza (VegPizza): 
    DEF PREPARE (Self): 
        Print (type (Self) .__ name__, 'to a') 


class MuttonPizza (NonVegPizza): 
    DEF serve (Self, veg_pizza): 
        Print (type (Self) .__ name__, ' come, lamb is added to ', type (veg_pizza) .__ name__ ,' inside ') 


class PizzaStore (Object): 
    DEF __init __ (Self): 
        Pass 

    DEF make_pizzas (Self): 
        # create that all objects (all pizza) , rather than a single specified object 
        for Factory in [USAPizzaFactory (), ChinaPizzaFactory ()]: 
            self.factory = Factory 
            self.non_veg_pizza = self.factory.create_non_veg_pizza () 
            self.veg_pizza = self.factory.create_veg_pizza ()  
            # call
            self.veg_pizza. prepare ()
            self.non_veg_pizza.serve(self.veg_pizza)


pizza = PizzaStore()
pizza.make_pizzas()

  

Guess you like

Origin www.cnblogs.com/songdanlee/p/11209805.html