设计模式概述
设计模式是一种被反复使用的、经过分类的、代码设计中被广泛认可的优秀代码设计经验。它不仅能解决常见的问题,还能提升代码的可读性和灵活性。设计模式主要分为三大类:创建型模式、结构型模式和行为型模式。
创建型模式
创建型模式关注对象的创建过程,主要有单例模式、工厂模式、抽象工厂模式、建造者模式和原型模式。
单例模式
单例模式确保一个类只有一个实例,并提供一个全局访问点。
示例:单例模式
package cn.juwatech; public class Singleton { private static Singleton instance; private Singleton() { // 私有构造函数,防止外部实例化 } public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
在这个示例中,Singleton类的构造函数是私有的,并且通过getInstance方法确保只有一个实例存在。使用synchronized关键字保证线程安全。
工厂模式
工厂模式定义一个创建对象的接口,但由子类决定实例化哪个类。
示例:工厂模式
package cn.juwatech; interface Product { void use(); } class ConcreteProductA implements Product { public void use() { System.out.println("使用产品A"); } } class ConcreteProductB implements Product { public void use() { System.out.println("使用产品B"); } } class ProductFactory { public static Product createProduct(String type) { if (type.equals("A")) { return new ConcreteProductA(); } else if (type.equals("B")) { return new ConcreteProductB(); } return null; } } public class FactoryPatternDemo { public static void main(String[] args) { Product productA = ProductFactory.createProduct("A"); productA.use(); Product productB = ProductFactory.createProduct("B"); productB.use(); } }
在这个示例中,ProductFactory通过传入的类型参数创建不同的产品对象,客户端代码不需要知道具体的创建细节。
结构型模式
结构型模式关注类和对象的组合,主要有适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式和享元模式。
适配器模式
适配器模式将一个类的接口转换成客户希望的另一个接口,使得原本不兼容的类可以一起工作。
示例:适配器模式
package cn.juwatech; interface Target { void request(); } class Adaptee { public void specificRequest() { System.out.println("特殊请求"); } } class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void request() { adaptee.specificRequest(); } } public class AdapterPatternDemo { public static void main(String[] args) { Adaptee adaptee = new Adaptee(); Target target = new Adapter(adaptee); target.request(); } }
在这个示例中,Adapter类将Adaptee类的接口转换为Target接口,使得Adaptee类可以与客户端代码兼容。
装饰器模式
装饰器模式动态地给对象添加一些额外的职责,就增加功能来说,装饰器模式比生成子类更为灵活。
示例:装饰器模式
package cn.juwatech; interface Component { void operation(); } class ConcreteComponent implements Component { public void operation() { System.out.println("基本操作"); } } class Decorator implements Component { protected Component component; public Decorator(Component component) { this.component = component; } public void operation() { component.operation(); } } class ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component component) { super(component); } public void operation() { super.operation(); addedBehavior(); } private void addedBehavior() { System.out.println("附加操作A"); } } class ConcreteDecoratorB extends Decorator { public ConcreteDecoratorB(Component component) { super(component); } public void operation() { super.operation(); addedBehavior(); } private void addedBehavior() { System.out.println("附加操作B"); } } public class DecoratorPatternDemo { public static void main(String[] args) { Component component = new ConcreteComponent(); Component decoratorA = new ConcreteDecoratorA(component); Component decoratorB = new ConcreteDecoratorB(component); decoratorA.operation(); decoratorB.operation(); } }
在这个示例中,ConcreteDecoratorA和ConcreteDecoratorB通过装饰模式动态地给ConcreteComponent对象添加了新的行为。
行为型模式
行为型模式关注对象之间的通信和职责分配,主要有观察者模式、策略模式、命令模式、责任链模式、中介者模式、备忘录模式、迭代器模式、模板方法模式和状态模式。
观察者模式
观察者模式定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。
示例:观察者模式
package cn.juwatech; import java.util.ArrayList; import java.util.List; interface Observer { void update(String 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 + " 收到消息: " + message); } } 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(String message) { for (Observer observer : observers) { observer.update(message); } } } public class ObserverPatternDemo { public static void main(String[] args) { Subject subject = new Subject(); Observer observer1 = new ConcreteObserver("观察者1"); Observer observer2 = new ConcreteObserver("观察者2"); subject.attach(observer1); subject.attach(observer2); subject.notifyObservers("新的消息来了!"); } }
在这个示例中,Subject类维护了一组观察者对象,当状态发生变化时,通知所有观察者进行更新。
总结
通过本文,我们详细介绍了Java中常见的设计模式及其应用场景,包括创建型模式、结构型模式和行为型模式。每种模式都有其独特的应用场景和优势,合理使用这些设计模式,可以提高代码的可维护性、可扩展性和灵活性。