设计模式实战详解:单例、工厂、抽象工厂、建造者等十大模式代码实现与应用

1. 单例模式(Singleton Pattern)

单例模式确保一个类只有一个实例,并提供一个全局访问点。常见的实现方式有懒汉式、饿汉式、双重校验锁等。

懒汉式(线程不安全)

public class Singleton {
    private static Singleton instance;

    private Singleton() {}

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

懒汉式(线程安全)

public class Singleton {
    private static Singleton instance;

    private Singleton() {}

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

饿汉式

public class Singleton {
    private static final Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

双重校验锁

public class Singleton {
    private static volatile Singleton instance;

    private Singleton() {}

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

单例模式适用于需要全局唯一访问点的场景,如配置管理器、日志记录器等。

2. 工厂模式(Factory Pattern)

工厂模式用于创建对象,而无需暴露创建逻辑。简单工厂模式是工厂模式的基础。

简单工厂模式

public interface Shape {
    void draw();
}

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

public 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;
    }
}

使用示例:

ShapeFactory shapeFactory = new ShapeFactory();
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
Shape shape2 = shapeFactory.getShape("SQUARE");
shape2.draw();

工厂模式将对象的创建逻辑封装在工厂类中,便于扩展和维护。

3. 抽象工厂模式(Abstract Factory Pattern)

抽象工厂模式用于创建一系列相关对象,而无需指定它们的具体类。

抽象工厂类

public interface Shape {
    void draw();
}

public interface Color {
    void fill();
}

public abstract class AbstractFactory {
    abstract Color getColor(String color);
    abstract Shape getShape(String shape);
}

具体工厂类

public class ShapeFactory extends AbstractFactory {
    @Override
    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;
    }

    @Override
    public Color getColor(String color) {
        return null;
    }
}

public class ColorFactory extends AbstractFactory {
    @Override
    public Shape getShape(String shapeType) {
        return null;
    }

    @Override
    public Color getColor(String color) {
        if (color == null) {
            return null;
        }
        if (color.equalsIgnoreCase("RED")) {
            return new Red();
        } else if (color.equalsIgnoreCase("GREEN")) {
            return new Green();
        }
        return null;
    }
}

具体产品类

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

public class Red implements Color {
    @Override
    public void fill() {
        System.out.println("Inside Red::fill() method.");
    }
}

public class Green implements Color {
    @Override
    public void fill() {
        System.out.println("Inside Green::fill() method.");
    }
}

工厂生成器

public class FactoryProducer {
    public static AbstractFactory getFactory(String choice) {
        if (choice.equalsIgnoreCase("SHAPE")) {
            return new ShapeFactory();
        } else if (choice.equalsIgnoreCase("COLOR")) {
            return new ColorFactory();
        }
        return null;
    }
}

使用示例:

AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
Color color1 = shapeFactory.getColor("RED"); // 返回null

AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
Color color2 = colorFactory.getColor("GREEN");
color2.fill();
Shape shape2 = colorFactory.getShape("SQUARE"); // 返回null

抽象工厂模式适用于需要创建一系列相关对象的场景,如UI组件库。

4. 建造者模式(Builder Pattern)

建造者模式用于逐步构建复杂对象,将对象的构建过程与表示分离。

产品类

public class Computer {
    private String cpu;
    private String ram;
    private String hdd;
    private String gpu;

    public Computer(String cpu, String ram, String hdd, String gpu) {
        this.cpu = cpu;
        this.ram = ram;
        this.hdd = hdd;
        this.gpu = gpu;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu='" + cpu + '\'' +
                ", ram='" + ram + '\'' +
                ", hdd='" + hdd + '\'' +
                ", gpu='" + gpu + '\'' +
                '}';
    }
}

建造者类

public class ComputerBuilder {
    private String cpu;
    private String ram;
    private String hdd;
    private String gpu;

    public ComputerBuilder setCpu(String cpu) {
        this.cpu = cpu;
        return this;
    }

    public ComputerBuilder setRam(String ram) {
        this.ram = ram;
        return this;
    }

    public ComputerBuilder setHdd(String hdd) {
        this.hdd = hdd;
        return this;
    }

    public ComputerBuilder setGpu(String gpu) {
        this.gpu = gpu;
        return this;
    }

    public Computer build() {
        return new Computer(cpu, ram, hdd, gpu);
    }
}

使用示例:

Computer computer = new ComputerBuilder()
        .setCpu("Intel i7")
        .setRam("16GB")
        .setHdd("1TB")
        .setGpu("NVIDIA GTX")
        .build();

System.out.println(computer);

建造者模式适用于构建复杂对象的场景,如汽车、计算机等。

5. 原型模式(Prototype Pattern)

原型模式用于复制对象,而无需创建新的实例。

原型类

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

public class Prototype implements Cloneable {
    private String name;
    private List<String> list;

    public Prototype(String name) {
        this.name = name;
        this.list = new ArrayList<>();
    }

    public void addItem(String item) {
        list.add(item);
    }

    public List<String> getList() {
        return list;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Prototype prototype = (Prototype) super.clone();
        prototype.list = new ArrayList<>(this.list);
        return prototype;
    }
}

使用示例:

Prototype prototype1 = new Prototype("Prototype1");
prototype1.addItem("Item1");
prototype1.addItem("Item2");

try {
    Prototype prototype2 = (Prototype) prototype1.clone();
    prototype2.addItem("Item3");
    System.out.println("Prototype1: " + prototype1.getList());
    System.out.println("Prototype2: " + prototype2.getList());
} catch (CloneNotSupportedException e) {
    e.printStackTrace();
}

原型模式适用于需要复制对象的场景,如文档编辑器。

6. 适配器模式(Adapter Pattern)

适配器模式用于将一个类的接口转换为另一个类的接口。

目标接口

public interface MediaPlayer {
    void play(String audioType, String fileName);
}

已有类

public class AudioPlayer implements MediaPlayer {
    @Override
    public void play(String audioType, String fileName) {
        if (audioType.equalsIgnoreCase("mp3")) {
            System.out.println("Playing mp3 file. Name: " + fileName);
        } else {
            System.out.println("AudioPlayer cannot play the format: " + audioType);
        }
    }
}

适配器类

public interface AdvancedMediaPlayer {
    void playVlc(String fileName);
    void playMp4(String fileName);
}

public class VlcPlayer implements AdvancedMediaPlayer {
    @Override
    public void playVlc(String fileName) {
        System.out.println("Playing vlc file. Name: " + fileName);
    }

    @Override
    public void playMp4(String fileName) {
        System.out.println("VlcPlayer cannot play the format: " + fileName);
    }
}

public class Mp4Player implements AdvancedMediaPlayer {
    @Override
    public void playVlc(String fileName) {
        System.out.println("Mp4Player cannot play the format: " + fileName);
    }

    @Override
    public void playMp4(String fileName) {
        System.out.println("Playing mp4 file. Name: " + fileName);
    }
}

public class MediaAdapter implements MediaPlayer {
    private AdvancedMediaPlayer advancedMediaPlayer;

    public MediaAdapter(String audioType) {
        if (audioType.equalsIgnoreCase("vlc")) {
            advancedMediaPlayer = new VlcPlayer();
        } else if (audioType.equalsIgnoreCase("mp4")) {
            advancedMediaPlayer = new Mp4Player();
        }
    }

    @Override
    public void play(String audioType, String fileName) {
        if (audioType.equalsIgnoreCase("vlc")) {
            advancedMediaPlayer.playVlc(fileName);
        } else if (audioType.equalsIgnoreCase("mp4")) {
            advancedMediaPlayer.playMp4(fileName);
        }
    }
}

使用示例:

AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp3", "beyond the horizon.mp3");

MediaAdapter mediaAdapter = new MediaAdapter("vlc");
mediaAdapter.play("vlc", "youve got to hide your love away.vlc");

mediaAdapter = new MediaAdapter("mp4");
mediaAdapter.play("mp4", "alone.mp4");

适配器模式适用于需要将不兼容的接口转换为兼容的接口的场景。

7. 装饰器模式(Decorator Pattern)

装饰器模式用于动态地给对象添加额外的功能。

抽象组件类

public interface Shape {
    void draw();
}

具体组件类

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

抽象装饰器类

public abstract class ShapeDecorator implements Shape {
    protected Shape decoratedShape;

    public ShapeDecorator(Shape decoratedShape) {
        this.decoratedShape = decoratedShape;
    }

    public void draw() {
        decoratedShape.draw();
    }
}

具体装饰器类

public class RedShapeDecorator extends ShapeDecorator {
    public RedShapeDecorator(Shape decoratedShape) {
        super(decoratedShape);
    }

    @Override
    public void draw() {
        decoratedShape.draw();
        setRedBorder(decoratedShape);
    }

    private void setRedBorder(Shape decoratedShape) {
        System.out.println("Border Color: Red");
    }
}

使用示例:

Shape circle = new Circle();
Shape redCircle = new RedShapeDecorator(new Circle());

circle.draw();
System.out.println("n");
redCircle.draw();

装饰器模式适用于需要动态添加功能的场景,如图形绘制。

8. 代理模式(Proxy Pattern)

代理模式用于控制对真实对象的访问。

主题接口

public interface Image {
    void display();
}

真实主题类

public class RealImage implements Image {
    private String fileName;

    public RealImage(String fileName) {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }

    @Override
    public void display() {
        System.out.println("Displaying " + fileName);
    }

    private void loadFromDisk(String fileName) {
        System.out.println("Loading " + fileName);
    }
}

代理类

public class ProxyImage implements Image {
    private RealImage realImage;
    private String fileName;

    public ProxyImage(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void display() {
        if (realImage == null) {
            realImage = new RealImage(fileName);
        }
        realImage.display();
    }
}

使用示例:

Image image = new ProxyImage("test_10mb.jpg");
image.display();
image.display();

代理模式适用于需要控制对真实对象访问的场景,如图片加载。

9. 观察者模式(Observer Pattern)

观察者模式用于监听对象的状态变化。

主题接口

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

public interface Subject {
    void register(Observer obj);
    void unregister(Observer obj);
    void notifyObservers();
}

具体主题类

public class StockGrabber implements Subject {
    private List<Observer> observers;
    private double ibmPrice;
    private double aaplPrice;
    private double googPrice;

    public StockGrabber() {
        observers = new ArrayList<>();
    }

    @Override
    public void register(Observer newObserver) {
        observers.add(newObserver);
    }

    @Override
    public void unregister(Observer deleteObserver) {
        int observerIndex = observers.indexOf(deleteObserver);
        System.out.println("Observer " + (observerIndex + 1) + " deleted");
        observers.remove(observerIndex);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(ibmPrice, aaplPrice, googPrice);
        }
    }

    public void setIbmPrice(double ibmPrice) {
        this.ibmPrice = ibmPrice;
        notifyObservers();
    }

    public void setAaplPrice(double aaplPrice) {
        this.aaplPrice = aaplPrice;
        notifyObservers();
    }

    public void setGoogPrice(double googPrice) {
        this.googPrice = googPrice;
        notifyObservers();
    }
}

观察者接口

public interface Observer {
    void update(double ibmPrice, double aaplPrice, double googPrice);
}

具体观察者类

public class StockObserver implements Observer {
    private double ibmPrice;
    private double aaplPrice;
    private double googPrice;
    private static int observerIdTracker = 0;
    private int observerId;
    private Subject stockGrabber;

    public StockObserver(Subject stockGrabber) {
        this.stockGrabber = stockGrabber;
        this.observerId = ++observerIdTracker;
        System.out.println("Creating Observer #" + this.observerId);
        stockGrabber.register(this);
    }

    @Override
    public void update(double ibmPrice, double aaplPrice, double googPrice) {
        this.ibmPrice = ibmPrice;
        this.aaplPrice = aaplPrice;
        this.googPrice = googPrice;
        printThePrices();
    }

    public void printThePrices() {
        System.out.println(observerId + "IBM: " + ibmPrice + " AAPL: " + aaplPrice + " GOOG: " + googPrice);
    }
}

使用示例:

StockGrabber stockGrabber = new StockGrabber();
StockObserver observer1 = new StockObserver(stockGrabber);
StockObserver observer2 = new StockObserver(stockGrabber);

stockGrabber.setGoogPrice(600);
stockGrabber.setAaplPrice(666);
stockGrabber.setIbmPrice(195);

stockGrabber.unregister(observer1);
stockGrabber.setIbmPrice(190);

观察者模式适用于需要监听对象状态变化的场景,如股票价格监控。

10. 策略模式(Strategy Pattern)

策略模式用于定义一系列算法,并在运行时动态选择算法。

策略接口

public interface Strategy {
    int doOperation(int num1, int num2);
}

具体策略类

public class OperationAdd implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1 + num2;
    }
}

public class OperationSubtract implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1 - num2;
    }
}

public class OperationMultiply implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1 * num2;
    }
}

上下文类

public class Context {
    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public int executeStrategy(int num1, int num2) {
        return strategy.doOperation(num1, num2);
    }
}

使用示例:

Context context = new Context(new OperationAdd());
System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

context = new Context(new OperationSubtract());
System.out.println("10 - 5 = " + context.executeStrategy(10, 5));

context = new Context(new OperationMultiply());
System.out.println("10 * 5 = " + context.executeStrategy(10, 5));

策略模式适用于需要在运行时动态选择算法的场景,如排序算法。

posted @ 2025-04-06 09:53  软件职业规划  阅读(20)  评论(0)    收藏  举报
相关博文:
阅读排行:
· 突发,CSDN 崩了!程序员们开始慌了?
· 完成微博外链备案,微博中直接可以打开园子的链接
· C# WinForms 实现打印监听组件
· C#实现欧姆龙 HostLink 通讯协议库
· 一个基于 .NET 开源、模块化 AI 图像生成 Web 用户界面
点击右上角即可分享
微信分享提示