python_ object-oriented design patterns _ _ class

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/snailchangquan/article/details/96849802

01 ------------- ------------------- class

class Person(object):
    age = 10;
    def __init__(self, age):
        if age is not None:
            self.age = age

    def speak(self):
        print("hello")

person1 = Person(30)
print(person1.age)

person2 = Person(None)
print(person2.age)

------------------new------------------------------

class Dog(object):

    def __new__(cls):
        print("new")
        return object.__new__(cls)

    def __init__(self):
        print("init")

    def speak(self):
        print("speak")

    def __del__(self):
        print("delete")

dog = Dog();
dog.speak()

---------------------------super----------------------

class Fish(object):
    def __init__(self, name):
        self.name = name

    Swim DEF (Self):
        Print ( "{0} swimming soon" .format (self.name))

    def __del__(self):
        print("delete")

class GoldFish(Fish):
    def __init__(self, name):
        super(GoldFish, self).__init__(name)

    def run(self):
        super(GoldFish, self).swim()

fish = GoldFish ( "goldfish")

 

2. Design simple factory pattern ########### ##############

# Significantly different interfaces, simple to realize separate
class the Pay (Object):
    DEF the __init __ (Self, Money):
        self.money = Money

    the Pay DEF (Self):
        Print ( "Financial of Snail: payment amount is {0}" format (self.money) .)


class WeiXin(object):
    def __init__(self, money):
        self.money = money

    Pay DEF (Self):
        Print ( "payment amount is {0}" format (self.money) .)


class Zhifubao(object):
    def __init__(self, money):
        self.money = money

    Pay DEF (Self):
        Print ( "payment amount is {0}" format (self.money) .)


channel = input ( "Select Payment Method:")
Money = float (the INPUT ( "Please enter a payment amount:"))
IF Channel == "Weixin":
    Weixin (Money) .pay ()
elif Channel == "zhifubao" :
    Zhifubao (Money) .pay ()
the else:
    the Pay (Money) .pay ()

################### factory method ######################

Advantages: No need to be concerned with is how to achieve concrete class, you can use the time to import category.

class Pay(object):
    def __init__(self, money):
        self.money = money

    the Pay DEF (Self):
        Print ( "Financial of Snail: payment amount is {0}" format (self.money) .)


class WeiXin(object):
    def __init__(self, money):
        self.money = money

    Pay DEF (Self):
        Print ( "micro-channel payment amount is {0}" format (self.money) .)


class Zhifubao(object):
    def __init__(self, money):
        self.money = money

    the Pay DEF (Self):
        Print ( "Alipay payment amount is {0}" format (self.money) .)


class PayFactory(object):
    def create(self, money):
        return Pay(money)


class WeiXinFactory(object):
    def create(self, money):
        return WeiXin(money)


class ZhifubaoFactory(object):
    def create(self, money):
        return Zhifubao(money)

------

from method import PayFactory
factory = PayFactory()
pay = factory.create(100)
pay.pay()

from method import WeiXinFactory
factory = WeiXinFactory()
pay = factory.create(1000)
pay.pay()


from method import ZhifubaoFactory
factory = ZhifubaoFactory()
pay = factory.create(10000)
pay.pay()

------ reflection Abstract Factory

model = input ( "Please enter the module name")
class classA with the INPUT = ( "Please enter the class name")
Money = the INPUT ( "Please enter the amount of money.")

model = __import__(model)
object = getattr(model, classA)(money)
print(dir(model))
print(object)
object.pay()

============== ============== singleton

class SingleTone(object):
    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            cls._instance = super(SingleTone, cls).__new__(cls, )
        return cls._instance

single1 =  SingleTone()
single2 = SingleTone()
print(id(single1))
print(id(single2))

Guess you like

Origin blog.csdn.net/snailchangquan/article/details/96849802