包含Java和Python写法。
工厂方法模式(Factory Method Pattern)。
工厂方法模式是一种创建型设计模式,它提供了一种创建对象的最佳方式。
在代码的例子中,它定义了一个抽象类Vehicle和三个具体的子类Car、Bike和Truck。同时还定义了一个抽象工厂类VehicleFactory和三个具体的工厂类CarFactory、BikeFactory和TruckFactory。
通过不同的工厂类,可以创建出不同类型的Vehicle对象。每个具体的工厂类都实现了createVehicle()方法,用于创建对应类型的Vehicle对象。 最后,代码展示了如何使用这些工厂类来创建不同类型的Vehicle对象,并调用它们的getType()方法来获取对象类型。
@abstractmethod是Python中用于定义抽象方法的装饰器。它来自于abc(Abstract Base Class)模块,用于标记某个方法为抽象方法。
抽象方法是指在基类中声明的方法,但没有实现具体的功能,只定义了方法签名。抽象方法必须在子类中被重写(override),否则在实例化子类时将引发TypeError异常。
是一种创建型设计模式,它确保一个类只有一个实例,并提供一个全局访问点。主要解决的问题是保证一个类只有一个实例,并提供一个全局访问点。
Singleton模式的优点是:
但是你要是改变心意了咋办,这不是一个好的设计方法,所以这个模式经常被当成反面教材。
Singleton模式在现实开发中有广泛的应用,例如线程池、缓存、日志对象、配置对象等都可以使用单例模式进行设计。
该模式确保了,无论调用多少次实例,总是返回同一个实例。
在Python版本中,使用__new__方法来控制实例的创建。__new__是在创建实例时由Python解释器自动调用的方法。通过重写__new__方法,我们可以做到只创建一个实例,并将其存储在_instance类属性中。 当第一次调用Singleton()时,_instance为None,会创建一个新的实例并赋值给_instance。后续调用则直接返回已创建的实例。这样就实现了单例模式。
生成器模式是一种创建型设计模式,旨在简化复杂对象的创建过程。它通过将对象的构建过程与其表示分离,使得相同的构建过程可以创建不同的表示。Builder Pattern 对于那些具有多个可选参数或构造步骤的对象特别有用。
Builder Pattern 通常包括以下几个部分:
Builder Pattern(生成器模式)是一种创建型设计模式,旨在简化复杂对象的创建过程。它通过将对象的构建过程与其表示分离,使得相同的构建过程可以创建不同的表示。Builder Pattern 对于那些具有多个可选参数或构造步骤的对象特别有用。
// 使用示例
public class BuilderPatternExample {
    public static void main(String[] args) {
        Computer computer = new Computer.Builder("Intel i7", "16GB")
                                .setUSBPorts(4)
                                .setHasGraphicsCard(true)
                                .build();
        System.out.println(computer);
    }
}
Builder Pattern 是一种创建复杂对象的有效方法,特别适用于那些具有多个可选参数的对象。通过将对象的构建过程与其表示分离,Builder Pattern 提高了代码的可读性和可维护性,同时增加了对象创建的灵活性。
Prototype(原型)是原型范式(Prototype Paradigm)的一部分,主要与基于原型的编程(Prototype-based Programming)有关。这种编程范式特别常见于面向对象编程(OOP)的一种实现方式,与传统的基于类的面向对象编程不同。
原型范式的一个典型示例是 JavaScript。在 JavaScript 中,每个对象都有一个内部链接指向另一个对象(即其原型)。当试图访问一个对象的属性时,如果该对象自身没有这个属性,JavaScript 会沿着原型链向上查找,直到找到该属性或者到达原型链的末端。
// 创建一个原型对象
let person = {
    type: 'human',
    sayHello: function() {
        console.log(`Hello, I am a ${this.type}`);
    }
};
// 通过复制原型对象创建一个新对象
let john = Object.create(person);
john.type = 'developer'; // 修改新对象的属性
john.sayHello(); // 输出: Hello, I am a developer
// 创建另一个对象
let jane = Object.create(person);
jane.type = 'designer';
jane.sayHello(); // 输出: Hello, I am a designer
在这个例子中,person 是一个原型对象。john 和 jane 是通过 Object.create(person) 创建的新对象,并继承了 person 的属性和方法。
原型范式提供了一种不同于基于类的面向对象编程的方法,更加灵活和动态,适合某些特定的编程需求和环境。
适配器模式(Adapter Pattern)是一种结构型设计模式,它的主要目的是将一个接口转换成客户端期望的另一个接口,从而使原本接口不兼容的类可以一起工作。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作。
适配器模式的主要角色:
适配器模式的两种实现方式:
假设我们有一个旧的类OldPrinter,它有一个方法oldPrint,但是我们希望在新系统中使用一个Printer接口,定义了一个方法print。
OldPrinter 类:
class OldPrinter {
    void oldPrint(String text) {
        System.out.println("Old Printer: " + text);
    }
}
Printer 接口:
interface Printer {
    void print(String text);
}
对象适配器实现:
class PrinterAdapter implements Printer {
    private OldPrinter oldPrinter;
    public PrinterAdapter(OldPrinter oldPrinter) {
        this.oldPrinter = oldPrinter;
    }
    @Override
    public void print(String text) {
        oldPrinter.oldPrint(text);
    }
}
客户端代码:
public class Client {
    public static void main(String[] args) {
        OldPrinter oldPrinter = new OldPrinter();
        Printer printer = new PrinterAdapter(oldPrinter);
        printer.print("Hello, World!");
    }
}
在这个示例中,PrinterAdapter实现了Printer接口,并将print方法的调用委托给OldPrinter类的oldPrint方法。客户端代码通过Printer接口与适配器进行交互,而无需了解底层的OldPrinter类。
装饰器设计模式(Decorator Design Pattern)是一种结构型设计模式,用于动态地向对象添加行为或职责,而不影响其他对象。与继承相比,装饰器模式提供了一种更灵活的方法来扩展对象的功能。以下是对装饰器设计模式的介绍:
UML类图
Component
  + operation()
    |
ConcreteComponent
  + operation()
    |
Decorator
  - component: Component
  + operation()
    |
ConcreteDecoratorA
  - addedState: Type
  + operation()
  + addedBehavior()
    |
ConcreteDecoratorB
  + operation()
  + addedBehavior()
下面是一个使用装饰器设计模式的示例,演示如何向对象动态添加职责。假设我们有一个简单的接口 Coffee,定义了 cost 和 description 方法。
from abc import ABC, abstractmethod
# 组件接口
class Coffee(ABC):
    @abstractmethod
    def cost(self) -> float:
        pass
    @abstractmethod
    def description(self) -> str:
        pass
# 具体组件
class SimpleCoffee(Coffee):
    def cost(self) -> float:
        return 5.0
    def description(self) -> str:
        return "Simple Coffee"
# 装饰器
class CoffeeDecorator(Coffee):
    def __init__(self, decorated_coffee: Coffee):
        self.decorated_coffee = decorated_coffee
    def cost(self) -> float:
        return self.decorated_coffee.cost()
    def description(self) -> str:
        return self.decorated_coffee.description()
# 具体装饰器A
class MilkDecorator(CoffeeDecorator):
    def cost(self) -> float:
        return self.decorated_coffee.cost() + 1.5
    def description(self) -> str:
        return self.decorated_coffee.description() + ", Milk"
# 具体装饰器B
class SugarDecorator(CoffeeDecorator):
    def cost(self) -> float:
        return self.decorated_coffee.cost() + 0.5
    def description(self) -> str:
        return self.decorated_coffee.description() + ", Sugar"
# 使用装饰器模式
coffee = SimpleCoffee()
print(f"Cost: {coffee.cost()}, Description: {coffee.description()}")
coffee_with_milk = MilkDecorator(coffee)
print(f"Cost: {coffee_with_milk.cost()}, Description: {coffee_with_milk.description()}")
coffee_with_milk_and_sugar = SugarDecorator(coffee_with_milk)
print(f"Cost: {coffee_with_milk_and_sugar.cost()}, Description: {coffee_with_milk_and_sugar.description()}")
输出:
Cost: 5.0, Description: Simple Coffee
Cost: 6.5, Description: Simple Coffee, Milk
Cost: 7.0, Description: Simple Coffee, Milk, Sugar
适用场景:
优点:
缺点:
装饰器设计模式是面向对象编程中强大且灵活的工具,用于动态地向对象添加新的功能。
Facade模式是一种结构型设计模式,提供了一个简化的接口,使得复杂系统的子系统之间更加容易使用和理解。该模式通过隐藏系统的复杂性并提供一个统一的接口来访问子系统,从而简化了客户端与系统的交互。
主要特点:
适用场景:
实现示例:
以下是一个使用Facade模式的简单示例。假设我们有一个家庭影院系统,包含多个子系统:DVD播放器、音响系统和投影仪。我们可以使用Facade模式来简化这些子系统的操作。
# 子系统类
class DVDPlayer:
    def on(self):
        print("DVD Player is on.")
    
    def play(self, movie):
        print(f"Playing movie: {movie}")
class SoundSystem:
    def on(self):
        print("Sound System is on.")
    
    def setVolume(self, level):
        print(f"Setting volume to {level}.")
class Projector:
    def on(self):
        print("Projector is on.")
    
    def setInput(self, source):
        print(f"Setting input source to {source}.")
# Facade类
class HomeTheaterFacade:
    def __init__(self, dvd_player, sound_system, projector):
        self.dvd_player = dvd_player
        self.sound_system = sound_system
        self.projector = projector
    
    def watchMovie(self, movie):
        print("Get ready to watch a movie...")
        self.dvd_player.on()
        self.dvd_player.play(movie)
        self.sound_system.on()
        self.sound_system.setVolume(10)
        self.projector.on()
        self.projector.setInput("DVD Player")
# 客户端代码
dvd_player = DVDPlayer()
sound_system = SoundSystem()
projector = Projector()
home_theater = HomeTheaterFacade(dvd_player, sound_system, projector)
home_theater.watchMovie("Inception")
优点:
缺点:
Facade模式通过提供一个简化的接口,使得客户端可以更方便地与复杂的子系统交互。它隐藏了系统的复杂性,降低了客户端与子系统之间的耦合度,适用于需要简化复杂系统接口的场景。
策略模式(Strategy Pattern)是一种行为设计模式,它定义了一系列算法,并将每个算法封装起来,使它们可以相互替换。策略模式使得算法可以在不影响客户端的情况下发生变化。
核心概念
结构图
classDiagram
    class Context {
        -strategy: Strategy
        +setStrategy(Strategy): void
        +executeStrategy(): void
    }
    class Strategy {
        <<interface>>
        +execute(): void
    }
    class ConcreteStrategyA {
        +execute(): void
    }
    class ConcreteStrategyB {
        +execute(): void
    }
    Context --> Strategy
    Strategy <|.. ConcreteStrategyA
    Strategy <|.. ConcreteStrategyB
实例代码
假设我们有一个应用,需要计算商品的折扣,不同的客户有不同的折扣策略:
from abc import ABC, abstractmethod
# 策略接口
class DiscountStrategy(ABC):
    @abstractmethod
    def apply_discount(self, amount):
        pass
# 具体策略:无折扣
class NoDiscountStrategy(DiscountStrategy):
    def apply_discount(self, amount):
        return amount
# 具体策略:百分比折扣
class PercentageDiscountStrategy(DiscountStrategy):
    def __init__(self, percentage):
        self.percentage = percentage
    def apply_discount(self, amount):
        return amount * (1 - self.percentage / 100)
# 具体策略:固定金额折扣
class FixedAmountDiscountStrategy(DiscountStrategy):
    def __init__(self, discount):
        self.discount = discount
    def apply_discount(self, amount):
        return max(0, amount - self.discount)
# 上下文类:使用策略
class Order:
    def __init__(self, amount, discount_strategy: DiscountStrategy):
        self.amount = amount
        self.discount_strategy = discount_strategy
    def get_total(self):
        return self.discount_strategy.apply_discount(self.amount)
# 使用策略模式
order1 = Order(100, NoDiscountStrategy())
print(f"Total (No Discount): ${order1.get_total()}")  # Total: $100
order2 = Order(100, PercentageDiscountStrategy(10))
print(f"Total (10% Discount): ${order2.get_total()}")  # Total: $90
order3 = Order(100, FixedAmountDiscountStrategy(15))
print(f"Total ($15 Discount): ${order3.get_total()}")  # Total: $85
策略模式适用于以下场景:
优点:
缺点:
现实中的应用:
策略模式在实际应用中非常常见,特别是在需要灵活调整行为或算法的场景中,为代码的扩展和维护提供了极大的便利。
观察者模式(Observer Pattern)是一种行为设计模式,用于定义对象间的一种一对多的依赖关系,当一个对象的状态发生变化时,所有依赖于它的对象都会自动收到通知并更新。
核心概念
结构图
classDiagram
    class Subject {
        +registerObserver(Observer)
        +removeObserver(Observer)
        +notifyObservers()
    }
    class Observer {
        <<interface>>
        +update()
    }
    class ConcreteSubject {
        -state: String
        +getState(): String
        +setState(state: String): void
    }
    class ConcreteObserver {
        -subject: ConcreteSubject
        +update(): void
    }
    Subject <|-- ConcreteSubject
    Observer <|-- ConcreteObserver
    ConcreteSubject --> Observer : notify
假设我们有一个天气监测系统,气象站会监测天气数据,并通知所有注册的显示设备(观察者)更新显示数据:
from abc import ABC, abstractmethod
# 主题接口
class Subject(ABC):
    @abstractmethod
    def register_observer(self, observer):
        pass
    @abstractmethod
    def remove_observer(self, observer):
        pass
    @abstractmethod
    def notify_observers(self):
        pass
# 观察者接口
class Observer(ABC):
    @abstractmethod
    def update(self, temperature, humidity, pressure):
        pass
# 具体主题类
class WeatherStation(Subject):
    def __init__(self):
        self.observers = []
        self.temperature = 0
        self.humidity = 0
        self.pressure = 0
    def register_observer(self, observer):
        self.observers.append(observer)
    def remove_observer(self, observer):
        self.observers.remove(observer)
    def notify_observers(self):
        for observer in self.observers:
            observer.update(self.temperature, self.humidity, self.pressure)
    def set_measurements(self, temperature, humidity, pressure):
        self.temperature = temperature
        self.humidity = humidity
        self.pressure = pressure
        self.notify_observers()
# 具体观察者类
class DisplayDevice(Observer):
    def update(self, temperature, humidity, pressure):
        print(f"Display updated: Temperature={temperature}, Humidity={humidity}, Pressure={pressure}")
# 使用观察者模式
weather_station = WeatherStation()
display = DisplayDevice()
weather_station.register_observer(display)
# 更新天气数据,并通知观察者
weather_station.set_measurements(25, 65, 1013)  # Display updated: Temperature=25, Humidity=65, Pressure=1013
# 再次更新天气数据
weather_station.set_measurements(22, 70, 1012)  # Display updated: Temperature=22, Humidity=70, Pressure=1012
# 移除观察者
weather_station.remove_observer(display)
# 更新天气数据,不会再通知观察者
weather_station.set_measurements(20, 60, 1011)  # No output, as the observer is removed
观察者模式适用于以下场景:
优点:
缺点:
现实中的应用:
观察者模式通过解耦对象之间的依赖关系,为系统的扩展和维护提供了更高的灵活性和可维护性。
状态模式(State Pattern)是一种行为设计模式,它允许对象在其内部状态改变时,改变其行为。也就是说,对象看起来似乎修改了它的类。状态模式的核心在于将与状态相关的行为封装到独立的状态对象中,使得对象的行为能够根据其内部状态的变化而变化。
关键要点
状态模式的结构包括以下几个部分:
State 对象的引用,这个对象定义了当前的状态。State 对象委派所有与状态相关的请求。State 接口,提供对应于 Context 的特定状态的行为。+----------------------+
|      Context         |
+----------------------+
| - state: State       |
+----------------------+
| + request(): void    |
+----------------------+
         |
         V
+----------------------+
|       State          |
+----------------------+
| + handle(context):   |
|   void               |
+----------------------+
         |
         V
+--------------------------+
|    ConcreteStateA        |
+--------------------------+
| + handle(context): void  |
+--------------------------+
         |
         V
+--------------------------+
|    ConcreteStateB        |
+--------------------------+
| + handle(context): void  |
+--------------------------+
假设:一个简单的电梯系统:
考虑一个简单的电梯系统,电梯可以处于以下几种状态之一:停止、上升 和 下降。我们可以使用状态模式来实现电梯的行为。
from abc import ABC, abstractmethod
class Context:
    def __init__(self, state: State):
        self._state = state
    def set_state(self, state: State):
        self._state = state
    def request(self):
        self._state.handle(self)
class State(ABC):
    @abstractmethod
    def handle(self, context: Context):
        pass
class StoppedState(State):
    def handle(self, context: Context):
        print("Elevator is stopped. Switching to rising state.")
        context.set_state(RisingState())
class RisingState(State):
    def handle(self, context: Context):
        print("Elevator is rising. Switching to falling state.")
        context.set_state(FallingState())
class FallingState(State):
    def handle(self, context: Context):
        print("Elevator is falling. Switching to stopped state.")
        context.set_state(StoppedState())
# 客户端代码
if __name__ == "__main__":
    # 创建初始状态
    stopped_state = StoppedState()
    # 创建上下文对象
    elevator = Context(stopped_state)
    # 请求行为
    elevator.request()  # Elevator is stopped. Switching to rising state.
    elevator.request()  # Elevator is rising. Switching to falling state.
    elevator.request()  # Elevator is falling. Switching to stopped state.
    elevator.request()  # Elevator is stopped. Switching to rising state.
代码解析:
Context 类:
    State)。set_state 方法来改变状态。request 方法来处理请求,这个方法会调用当前状态的 handle 方法。State 抽象类:
    handle 方法,所有具体的状态都要实现这个方法。StoppedState、RisingState 和 FallingState 类:
    handle 方法,定义了状态的具体行为。handle 方法中,状态类不仅处理请求,还负责改变上下文的状态。优点:
if-else 或 switch-case 分支条件。缺点:
状态模式通过将状态转换的行为封装在独立的状态类中,使得对象的行为可以根据状态变化而变化,从而简化了代码逻辑和提高了扩展性。在设计需要根据状态变化而改变行为的系统时,状态模式是一种非常有用的设计模式。