设计模式通常被分为三种类型:创建型模式、结构型模式和行为型模式。
创建型模式主要关注对象的创建机制,它们提供了一种将对象创建和实例化的机制,使得系统在不直接依赖于具体类的情况下能够灵活地创建对象。
创建型模式的典型代表包括工厂模式、抽象工厂模式、建造者模式、原型模式和单例模式。
- public class Singleton {
- private static Singleton instance;
-
- private Singleton() {}
-
- public static Singleton getInstance() {
- if (instance == null) {
- instance = new Singleton();
- }
- return instance;
- }
- }
- interface Shape {
- void draw();
- }
-
- class Rectangle implements Shape {
- @Override
- public void draw() {
- System.out.println("Drawing a rectangle");
- }
- }
-
- class Circle implements Shape {
- @Override
- public void draw() {
- System.out.println("Drawing a circle");
- }
- }
-
- class ShapeFactory {
- public static Shape getShape(String type) {
- if (type.equalsIgnoreCase("rectangle")) {
- return new Rectangle();
- } else if (type.equalsIgnoreCase("circle")) {
- return new Circle();
- }
- return null;
- }
- }
结构型模式主要关注对象的组合和结构,它们描述了如何将类或对象按照某种方式组合成更大的结构,以满足系统的需求。
结构型模式的典型代表包括适配器模式、桥接模式、组合模式、装饰者模式、外观模式、享元模式和代理模式。
装饰者模式(Decorator Pattern)是一种结构型设计模式,它允许在运行时动态地将责任附加到对象上,同时不改变其接口。装饰者模式通过创建装饰者类,用来包装原始类的对象,并向其添加新的行为或责任,从而实现了对对象功能的动态扩展。
装饰者模式通常涉及以下几种角色:
Component(组件):组件是一个接口或抽象类,定义了被装饰者和装饰者共同的接口。它可以是一个抽象类,也可以是一个接口,用于定义被装饰者和装饰者共同的行为。
ConcreteComponent(具体组件):具体组件是实现了组件接口的具体类,它是被装饰者,负责提供基本的功能。
Decorator(装饰者):装饰者是一个抽象类,它实现了组件接口,并持有一个指向组件的引用。装饰者通常包含一个组件对象,用来装饰或包装真实的组件对象。
ConcreteDecorator(具体装饰者):具体装饰者是实现了装饰者接口的具体类,它负责向组件添加新的行为或责任。
装饰者模式的核心思想是通过嵌套具体装饰者对象来动态地扩展对象的功能,而不是通过继承来实现。这样可以避免继承导致的类爆炸问题,并且使得每个类的职责更加清晰,更易于维护和扩展。
下面是一个简单的装饰者模式的示例代码:
- // 组件接口
- interface Coffee {
- String getDescription();
- double getCost();
- }
-
- // 具体组件
- class Espresso implements Coffee {
- @Override
- public String getDescription() {
- return "Espresso";
- }
-
- @Override
- public double getCost() {
- return 1.99;
- }
- }
-
- // 装饰者抽象类
- abstract class CoffeeDecorator implements Coffee {
- protected Coffee decoratedCoffee;
-
- public CoffeeDecorator(Coffee decoratedCoffee) {
- this.decoratedCoffee = decoratedCoffee;
- }
-
- @Override
- public String getDescription() {
- return decoratedCoffee.getDescription();
- }
-
- @Override
- public double getCost() {
- return decoratedCoffee.getCost();
- }
- }
-
- // 具体装饰者
- class MilkDecorator extends CoffeeDecorator {
- public MilkDecorator(Coffee decoratedCoffee) {
- super(decoratedCoffee);
- }
-
- @Override
- public String getDescription() {
- return super.getDescription() + ", Milk";
- }
-
- @Override
- public double getCost() {
- return super.getCost() + 0.5;
- }
- }
-
- class SugarDecorator extends CoffeeDecorator {
- public SugarDecorator(Coffee decoratedCoffee) {
- super(decoratedCoffee);
- }
-
- @Override
- public String getDescription() {
- return super.getDescription() + ", Sugar";
- }
-
- @Override
- public double getCost() {
- return super.getCost() + 0.2;
- }
- }
-
- // 测试类
- public class Test{
- public static void main(String[] args) {
- Coffee espresso = new Espresso();
- System.out.println("Description: " + espresso.getDescription());
- System.out.println("Cost: $" + espresso.getCost());
-
- Coffee milkCoffee = new MilkDecorator(espresso);
- System.out.println("Description: " + milkCoffee.getDescription());
- System.out.println("Cost: $" + milkCoffee.getCost());
-
- Coffee sugarMilkCoffee = new SugarDecorator(milkCoffee);
- System.out.println("Description: " + sugarMilkCoffee.getDescription());
- System.out.println("Cost: $" + sugarMilkCoffee.getCost());
- }
- }
行为型模式主要关注对象之间的通信和交互,它们描述了对象之间的相互作用和协作方式,以实现系统中的功能。
行为型模式的典型代表包括责任链模式、命令模式、解释器模式、迭代器模式、中介者模式、备忘录模式、观察者模式、状态模式、策略模式、模板方法模式和访问者模式。
观察者模式(Observer Pattern)是一种行为设计模式,它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,当主题对象的状态发生变化时,所有依赖于它的观察者都会得到通知并自动更新。
这种模式主要涉及两种角色:
Subject(主题):也称为被观察者或可观察对象,它是一个具体类,负责维护一组观察者对象,并提供注册和删除观察者的接口,以及通知观察者的方法。
Observer(观察者):也称为订阅者或监听者,它是一个接口或抽象类,定义了一个更新的接口,以便在主题状态变化时接收通知。
观察者模式的优点包括:
观察者模式的典型应用场景包括:
下面是一个简单的观察者模式的示例代码:
- import java.util.ArrayList;
- import java.util.List;
-
- interface Observer {
- void update(String message);
- }
-
- class Subject {
- private List
observers = new ArrayList<>(); -
- public void addObserver(Observer observer) {
- observers.add(observer);
- }
-
- public void notifyObservers(String message) {
- for (Observer observer : observers) {
- observer.update(message);
- }
- }
- }
-
- class ConcreteObserver implements Observer {
- private String name;
-
- public ConcreteObserver(String name) {
- this.name = name;
- }
-
- @Override
- public void update(String message) {
- System.out.println(name + " received message: " + message);
- }
- }
-
- // 测试类
- public class Test{
- public static void main(String[] args) {
- // 创建一个主题对象
- Subject subject = new Subject();
-
- // 创建两个观察者对象
- Observer observer1 = new ConcreteObserver("Observer 1");
- Observer observer2 = new ConcreteObserver("Observer 2");
-
- // 注册观察者
- subject.addObserver(observer1);
- subject.addObserver(observer2);
-
- // 主题对象状态发生变化,通知观察者
- subject.notifyObservers("Hello, observers!");
- }
- }
策略模式(Strategy Pattern)是一种行为型设计模式,它定义了一系列算法(策略),并将每个算法封装到具有公共接口的独立类中,使得它们可以相互替换。策略模式使得算法可以独立于使用它们的客户端而变化,从而使得算法可以根据需要动态地切换、选择或替换。
策略模式主要涉及三种角色:
Context(上下文):上下文是客户端使用策略的入口,它持有一个策略对象,并在需要的时候调用策略对象的方法。上下文类通常会将请求委派给策略对象来执行具体的算法。
Strategy(策略):策略是一个接口或抽象类,它定义了一个算法族的共同接口。具体的策略类实现了这个接口,并实现了具体的算法。
ConcreteStrategy(具体策略):具体策略类实现了策略接口,并提供了具体的算法实现。每个具体策略类都代表了一个具体的算法,客户端可以根据需求选择不同的具体策略来执行相应的算法。
策略模式的优点包括:
下面是一个简单的策略模式的示例代码:
- // 策略接口
- interface Strategy {
- void execute();
- }
-
- // 具体策略A
- class ConcreteStrategyA implements Strategy {
- @Override
- public void execute() {
- System.out.println("Executing strategy A");
- }
- }
-
- // 具体策略B
- class ConcreteStrategyB implements Strategy {
- @Override
- public void execute() {
- System.out.println("Executing strategy B");
- }
- }
-
- // 上下文类
- class Context {
- private Strategy strategy;
-
- public Context(Strategy strategy) {
- this.strategy = strategy;
- }
-
- public void setStrategy(Strategy strategy) {
- this.strategy = strategy;
- }
-
- public void executeStrategy() {
- strategy.execute();
- }
- }
-
- // 测试类
- public class Test{
- public static void main(String[] args) {
- // 创建具体策略对象
- Strategy strategyA = new ConcreteStrategyA();
- Strategy strategyB = new ConcreteStrategyB();
-
- // 创建上下文对象,并设置具体策略
- Context context = new Context(strategyA);
-
- // 执行具体策略
- context.executeStrategy();
-
- // 切换策略
- context.setStrategy(strategyB);
- context.executeStrategy();
- }
- }
应用场景如,多种方式付款:
- interface PaymentStrategy {
- void pay(int amount);
- }
-
- class CreditCardStrategy implements PaymentStrategy {
- private String cardNumber;
- private String cvv;
- private String expirationDate;
-
- public CreditCardStrategy(String cardNumber, String cvv, String expirationDate) {
- this.cardNumber = cardNumber;
- this.cvv = cvv;
- this.expirationDate = expirationDate;
- }
-
- @Override
- public void pay(int amount) {
- System.out.println("Paid " + amount + " using credit card.");
- }
- }
-
- class ShoppingCart {
- private PaymentStrategy paymentStrategy;
-
- public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
- this.paymentStrategy = paymentStrategy;
- }
-
- public void checkout(int amount) {
- paymentStrategy.pay(amount);
- }
- }