My Note / Zeliang YAO
  • Zeliang's Note
  • Dremio
    • Custom Class
  • 💕Python
    • Design Pattern
      • Creational
        • Abstract Factory
        • Factory Method
        • Singleton
        • Builder / Director
      • Structural
        • Adapter
    • Boto3
    • Typing
    • String
    • Requests
    • Iterator & Iterable
      • Consuming iterator manually
      • Lazy Iterable
    • Genrators
    • itertools
    • Collections
    • Customization
      • Customize built-in
      • Logging
      • Hdf5
      • Sqlite3 & Df
    • Pandas
      • Basic
      • Data cleaning
      • Merge, Join, Concat
      • Useful tricks
      • Simple model
      • Pandas acceleration
    • Pandas time series
      • Date Range
      • Datetime Index
      • Holidays
      • Function_to_date_time
      • Period
      • Time zone
    • *args and**kwargs
    • Context Manager
    • Lambda
    • SHA
    • Multithreading
      • Threading
      • Speed Up
    • Email
    • Improvement
    • Useful functions
    • Python OOP
      • Basic
      • @static / @class method
      • attrs module
      • Dataclasses
      • Dataclasses example
      • Others
    • Design patterns
      • Creational Patterns
      • Structural Patterns
      • Behavioral Patterns
  • 🐣Git/Github
    • Commands
  • K8s
    • Useful commands
  • Linux
    • Chmod
Powered by GitBook
On this page

Was this helpful?

  1. Python
  2. Design Pattern
  3. Structural

Adapter

内容:将一个类的接口转换成客户希望的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

1.适用场景:想使用一个已经存在的类,而他的接口不符合要求

2.两种实现方式:

  • 类适配器:使用多继承

# 当我们需要改动部分的代码很少的时候,我们直接多继承+复写方法即可
from abc import ABCMeta,abstractclassmethod

class Payment(metaclass=ABCMeta):
    @abstractclassmethod
    def pay(self,money):
        pass

class Alipay(Payment):
    def pay(self,money):
        print("阿里支付了{}元".format(money))

class Weixinpay(Payment):
    def pay(self,money):
        print("微信支付了{}元".format(money))

class Bankpay:
    def cost(self,money):
        print("银行卡支付{}元".format(money))

class NewBankPay(Payment,Bankpay):
    def pay(self,money):
        self.cost(money)

nb = NewBankPay()
nb.pay(100)

PreviousStructuralNextBoto3

Last updated 2 years ago

Was this helpful?

💕