Java常见设计模式

设计模式是在软件开发中经过验证的解决问题的方法。它们是对于特定问题的最佳实践。Java作为一种面向对象的编程语言,提供了许多常见的设计模式来解决各种软件开发问题。本文将介绍一些常见的Java设计模式,并提供相应的代码示例。

1. 单例模式

单例模式用于确保一个类只有一个实例,并提供全局访问点。以下是一个使用懒汉式实现的单例模式的示例代码:

public class Singleton {
    private static Singleton instance;

    private Singleton() {
        // 私有构造函数
    }

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

在上面的示例中,私有构造函数使得其他类无法直接实例化Singleton类。getInstance方法提供了对唯一实例的访问,并使用双重检查锁定来确保线程安全。

2. 工厂模式

工厂模式用于创建对象的过程,将对象的创建与使用解耦。以下是一个简单的工厂模式的示例代码:

public interface Shape {
    void draw();
}

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

public class Rectangle implements Shape {
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class ShapeFactory {
    public static Shape createShape(String shapeType) {
        if (shapeType.equalsIgnoreCase("circle")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("rectangle")) {
            return new Rectangle();
        }
        return null;
    }
}

在上面的示例中,Shape接口定义了一个draw方法,Circle和Rectangle类分别实现了这个接口。ShapeFactory类根据参数创建相应的对象,将对象的创建过程封装起来,使得客户端代码无需关心具体的实现细节。

3. 观察者模式

观察者模式用于一对多的依赖关系,当一个对象的状态发生变化时,所有依赖于它的对象都会得到通知。以下是一个简单的观察者模式的示例代码:

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

public interface Observer {
    void update();
}

public class ConcreteObserver implements Observer {
    public void update() {
        System.out.println("Observer is notified");
    }
}

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

    public void attach(Observer observer) {
        observers.add(observer);
    }

    public void detach(Observer observer) {
        observers.remove(observer);
    }

    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }
}

在上面的示例中,Observer接口定义了一个update方法,ConcreteObserver类实现了这个接口。Subject类维护了一个观察者列表,当它的状态发生变化时,调用notifyObservers方法通知所有观察者。

4. 装饰器模式

装饰器模式用于动态地给一个对象添加额外的功能,而无需改变它的结构。以下是一个简单的装饰器模式的示例代码:

public interface Component {
    void operation();
}

public class ConcreteComponent implements Component {
    public void operation() {
        System.out.println("Executing the original operation");
    }
}

public class Decorator implements Component {
    private Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    public void operation() {
        component.operation();
        System.out.println("Adding additional functionality");
    }
}

在上面的示例中,Component接口定义了一个operation方法,ConcreteComponent类实现了这个接口。Decorator类也实现了Component接口,并持有一个Component对象作为成员变量,它的operation方法先调用Component对象的operation方法,然后再添加额外的功能。

流程图

下面是工厂模式的流程图:

flowchart TD
    A(客户端代码) --> B{创建对象的方式}
    B --> C[调用工厂方法]