Design Pattern Adapter

The adapter mode of the design pattern (mainly adapts the interface that does not conform to the rules to conform to the rules by certain means, and hides the specific adaptation details):

from abc import abstractmethod,ABCMeta
 class Payment(metaclass= ABCMeta):
     #Payment interface class 
    @abstractmethod
     def pay(self,money):
         pass
    
    
   
# -------------The payment class that conforms to the rules------------ 
class Alipay(Payment): #The
     payment method that conforms to the rules 
    def pay(self,money):
         print ( ' Use Ali to pay %s yuan ' % money)
 class Applepay(Payment):
     def pay(self,money):
         print ( ' Use Apple to pay %s yuan ' % money)



# ------------- Payment classes that do not conform to the rules (that is, classes to be adapted)------------ 
class Wechatpay:
     # classes to be adapted 
    def zhifu(self ,money):
         print ( ' Use WeChat to pay %s yuan ' % money)
 class Unionpay :
     #The class to be adapted 
    def yinlianpay(self,money):
         print ( ' Use UnionPay to pay %s yuan ' % money)




# --------Adaptation (use class adapter for adaptation)------------ 
class WechatRealpay(Payment,Wechatpay): #Class
     adapter def 
    pay (self,money) :
         return self.zhifu(money)
 class UnionRealpay(Payment,Unionpay): #class
     adapter def 
    pay (self,money):
         return self.yinlianpay(money)




# ----------- for adaptation (use object adapter for adaptation)------------ 
class WechatPayAdapter(Wechatpay): #Object
     adapter def 
    __init__  ( self,payment = Wechatpay()): #This
         adapter is made for WeChat, the default payment method is WeChat payment 
        self.payment = payment
     def pay(self,money):
         return self.payment.zhifu(money)
 class UnionPayAdapter(Unionpay) :
     def  __init__ (self,payment= Unionpay ()):
         #This object adapter is made for UnionPay payment, and the default payment method is UnionPay payment 
        self.payment = payment
     def pay(self,money):
         returnself.payment.yinlianpay(money)




# ------------Create a payment factory for testing use ------------ 
class Factory:
     #Factory class for testing adapters 
    @classmethod
     def create_payment(cls,method) :
         if method == ' ali ' :
             return Alipay()
         elif method == ' apple ' :
             return Applepay()
         elif method == ' wechat ' :
             #Note that the real payment class is called here, that is, it has been adapted Class 
            # return WechatRealpay() # Class Adapter 
            return WechatPayAdapter() #Object adapter 
        elif method == ' yinlian ' :
             # return UnionRealpay() 
            return UnionPayAdapter()
         else :
             raise NameError(method)
        
        
# 测试
# payment = Factory.create_payment('wechat')
payment = Factory.create_payment('yinlian')
payment.pay(50)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325072844&siteId=291194637