Java 核心库中的 GoF 设计模式示例
设计模式是软件开发中常用的解决问题的方法论,它提供了一种可重用的解决方案,帮助开发人员更高效地编写可维护和可扩展的代码。在 Java 核心库中,许多 GoF(Gang of Four)设计模式的示例都得到了实现和应用。本文将介绍几个常用的设计模式,并给出相应的示例代码。1. 单例模式单例模式是一种创建型设计模式,它保证一个类只有一个实例,并提供一个全局访问点。在 Java 核心库中,许多类都采用了单例模式来实现,比如 Runtime 类和 Calendar 类。示例代码:javapublic 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 类只有一个私有的静态实例变量 instance,并通过 getInstance() 方法获取该实例。getInstance() 方法采用双重检查锁定来保证线程安全。2. 工厂模式工厂模式是一种创建型设计模式,它定义了一个用于创建对象的接口,但由子类决定要实例化的类是哪一个。在 Java 核心库中,许多类的创建都采用了工厂模式,比如 Calendar 类的 getInstance() 方法。示例代码:javapublic interface Animal { void sound();}public class Dog implements Animal { @Override public void sound() { System.out.println("汪汪汪!"); }}public class Cat implements Animal { @Override public void sound() { System.out.println("喵喵喵!"); }}public class AnimalFactory { public Animal createAnimal(String type) { if (type.equalsIgnoreCase("dog")) { return new Dog(); } else if (type.equalsIgnoreCase("cat")) { return new Cat(); } else { return null; } }}在上述代码中,Animal 接口定义了动物的行为,Dog 和 Cat 类实现了该接口,并分别定义了不同的声音。AnimalFactory 类是一个工厂类,根据传入的参数类型来创建相应的动物对象。3. 观察者模式观察者模式是一种行为型设计模式,它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。在 Java 核心库中,许多类都使用了观察者模式,比如 Observable 类和 Observer 接口。示例代码:javaimport java.util.Observable;import java.util.Observer;public class WeatherData extends Observable { private float temperature; public float getTemperature() { return temperature; } public void setTemperature(float temperature) { this.temperature = temperature; setChanged(); notifyObservers(); }}public class Display implements Observer { private float temperature; @Override public void update(Observable o, Object arg) { if (o instanceof WeatherData) { WeatherData weatherData = (WeatherData) o; temperature = weatherData.getTemperature(); display(); } } public void display() { System.out.println("当前温度:" + temperature + "℃"); }}public class Main { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); Display display = new Display(); weatherData.addObserver(display); weatherData.setTemperature(25.5f); }}在上述代码中,WeatherData 类是主题类,它继承自 Observable 类,并通过 setChanged() 和 notifyObservers() 方法来通知观察者对象。Display 类是观察者类,它实现了 Observer 接口,并在 update() 方法中接收主题对象的更新通知。4. 适配器模式适配器模式是一种结构型设计模式,它将一个类的接口转换成客户端所期望的另一个接口。在 Java 核心库中,许多类都使用了适配器模式,比如 InputStream 和 OutputStream 类。示例代码:javaimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { String input = reader.readLine(); System.out.println("你输入的内容是:" + input); } catch (IOException e) { e.printStackTrace(); } }}在上述代码中,通过使用 BufferedReader 类(适配器类)包装 System.in(被适配者类),实现了从控制台读取用户输入的功能。本文介绍了 Java 核心库中的几个常用的 GoF 设计模式示例,包括单例模式、工厂模式、观察者模式和适配器模式。这些设计模式在实际的软件开发中具有广泛的应用,可以帮助开发人员编写出易于维护和扩展的高质量代码。通过学习和应用这些设计模式,开发人员可以提高自己的编程能力和代码质量。