常用设计模式

设计模式是软件开发中解决常见问题的可重用解决方案。它们提供了一种结构化的方法来解决设计问题,并帮助开发者编写可维护和可扩展的代码。在本教程中,我们将介绍几种常用的设计模式,包括它们的理论解释、代码示例和实践练习。

1. 单例模式 (Singleton Pattern)

1.1 理论解释

单例模式确保一个类只有一个实例,并提供一个全局访问点。这在需要控制资源访问或确保系统中只有一个实例时非常有用。

1.2 代码示例

public class Singleton {
    private static Singleton instance;

    // 私有构造函数,防止外部实例化
    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    public void showMessage() {
        System.out.println("Hello, I am a Singleton!");
    }
}

1.3 实践练习

编写一个程序,使用单例模式来管理数据库连接。确保在整个应用程序中只有一个数据库连接实例。

2. 工厂模式 (Factory Pattern)

2.1 理论解释

工厂模式提供了一种创建对象的方式,而无需指定具体的类。它通过定义一个接口或抽象类来创建对象,具体的实现由子类决定。

2.2 代码示例

interface Shape {
    void draw();
}

class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a Circle");
    }
}

class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a Square");
    }
}

class ShapeFactory {
    public Shape getShape(String shapeType) {
        if (shapeType == null) {
            return null;
        }
        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        }
        return null;
    }
}

2.3 实践练习

编写一个程序,使用工厂模式来创建不同类型的汽车(如轿车、卡车)。工厂类应根据输入参数返回相应的汽车对象。

3. 观察者模式 (Observer Pattern)

3.1 理论解释

观察者模式定义了对象之间的一对多依赖关系,当一个对象的状态发生变化时,所有依赖它的对象都会收到通知并自动更新。

3.2 代码示例

import java.util.ArrayList;
import java.util.List;

interface Observer {
    void update(String message);
}

class Subject {
    private List<Observer> observers = new ArrayList<>();

    public void attach(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);
    }
}

3.3 实践练习

编写一个程序,使用观察者模式来实现一个新闻发布系统。当有新新闻发布时,所有订阅者(观察者)都会收到通知。

4. 装饰器模式 (Decorator Pattern)

4.1 理论解释

装饰器模式允许你动态地为对象添加功能,而无需修改其代码。它通过创建一个包装类来实现这一点,该包装类包含原始对象并添加额外的行为。

4.2 代码示例

interface Coffee {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Coffee {
    @Override
    public String getDescription() {
        return "Simple Coffee";
    }

    @Override
    public double getCost() {
        return 1.0;
    }
}

abstract class CoffeeDecorator implements Coffee {
    protected Coffee decoratedCoffee;

    public CoffeeDecorator(Coffee coffee) {
        this.decoratedCoffee = coffee;
    }

    @Override
    public String getDescription() {
        return decoratedCoffee.getDescription();
    }

    @Override
    public double getCost() {
        return decoratedCoffee.getCost();
    }
}

class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + ", Milk";
    }

    @Override
    public double getCost() {
        return super.getCost() + 0.5;
    }
}

4.3 实践练习

编写一个程序,使用装饰器模式来实现一个披萨订购系统。用户可以选择不同的配料(如奶酪、蘑菇)来装饰基本的披萨。

5. 策略模式 (Strategy Pattern)

5.1 理论解释

策略模式定义了一系列算法,并将每个算法封装起来,使它们可以互换。策略模式使得算法可以独立于使用它的客户端而变化。

5.2 代码示例

interface PaymentStrategy {
    void pay(int amount);
}

class CreditCardStrategy implements PaymentStrategy {
    private String name;
    private String cardNumber;

    public CreditCardStrategy(String name, String cardNumber) {
        this.name = name;
        this.cardNumber = cardNumber;
    }

    @Override
    public void pay(int amount) {
        System.out.println(amount + " paid with credit/debit card");
    }
}

class PayPalStrategy implements PaymentStrategy {
    private String email;

    public PayPalStrategy(String email) {
        this.email = email;
    }

    @Override
    public void pay(int amount) {
        System.out.println(amount + " paid using PayPal");
    }
}

class ShoppingCart {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void checkout(int amount) {
        paymentStrategy.pay(amount);
    }
}

5.3 实践练习

编写一个程序,使用策略模式来实现一个购物车系统。用户可以选择不同的支付方式(如信用卡、PayPal)来支付购物车中的商品。

总结

设计模式是软件开发中的重要工具,它们帮助我们编写可维护和可扩展的代码。通过学习这些常用的设计模式,你将能够更好地理解和应用它们来解决实际问题。希望本教程对你有所帮助,祝你在编程学习中取得进步!