在Java系统架构中,设计模式是软件开发中解决常见问题的经验总结。掌握这些设计模式有助于提升代码的可维护性、可扩展性和可复用性。以下是对Java系统架构中不可或缺的10大设计模式的深度解析:
1. 单例模式(Singleton)
解析: 单例模式确保一个类只有一个实例,并提供一个全局访问点。
代码示例:
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
2. 工厂方法模式(Factory Method)
解析: 工厂方法模式定义一个接口用于创建对象,但让子类决定实例化哪一个类。
代码示例:
interface Product {
void use();
}
class ConcreteProductA implements Product {
public void use() {
System.out.println("Using Product A");
}
}
class ConcreteProductB implements Product {
public void use() {
System.out.println("Using Product B");
}
}
class Creator {
public Product factoryMethod() {
return new ConcreteProductA();
}
}
3. 抽象工厂模式(Abstract Factory)
解析: 抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
代码示例:
interface ProductA {
void use();
}
interface ProductB {
void use();
}
class ConcreteProductA implements ProductA {
public void use() {
System.out.println("Using Product A");
}
}
class ConcreteProductB implements ProductB {
public void use() {
System.out.println("Using Product B");
}
}
interface Factory {
ProductA createProductA();
ProductB createProductB();
}
class ConcreteFactory implements Factory {
public ProductA createProductA() {
return new ConcreteProductA();
}
public ProductB createProductB() {
return new ConcreteProductB();
}
}
4. 建造者模式(Builder)
解析: 建造者模式将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
代码示例:
class Product {
private String partA;
private String partB;
public void setPartA(String partA) {
this.partA = partA;
}
public void setPartB(String partB) {
this.partB = partB;
}
public void show() {
System.out.println("Product with partA: " + partA + ", partB: " + partB);
}
}
class Builder {
private Product product = new Product();
public Builder addPartA(String partA) {
product.setPartA(partA);
return this;
}
public Builder addPartB(String partB) {
product.setPartB(partB);
return this;
}
public Product build() {
return product;
}
}
5. 原型模式(Prototype)
解析: 原型模式通过复制现有的实例来创建新的实例,从而避免直接构造新对象。
代码示例:
class Prototype implements Cloneable {
private String field;
public void setField(String field) {
this.field = field;
}
public String getField() {
return field;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
6. 适配器模式(Adapter)
解析: 适配器模式允许将一个类的接口转换成客户期望的另一个接口,适配器让原本接口不兼容的类可以合作无间。
代码示例:
interface Target {
void request();
}
class Adaptee {
public void specificRequest() {
System.out.println("Specific Request");
}
}
class Adapter implements Target {
private Adaptee adaptee = new Adaptee();
public void request() {
adaptee.specificRequest();
}
}
7. 桥接模式(Bridge)
解析: 桥接模式将抽象部分与实现部分分离,使它们都可以独立地变化。
代码示例:
interface Abstraction {
void operation();
}
interface RefinedAbstraction extends Abstraction {
void operation();
}
class Implementor {
public void operationImpl() {
System.out.println("Operation Implementation");
}
}
class ConcreteImplementorA extends Implementor {
public void operationImpl() {
System.out.println("Concrete Implementor A Operation");
}
}
class RefinedAbstractionImpl extends RefinedAbstraction {
private Implementor implementor;
public RefinedAbstractionImpl(Implementor implementor) {
this.implementor = implementor;
}
public void operation() {
implementor.operationImpl();
}
}
8. 组合模式(Composite)
解析: 组合模式允许将对象组合成树形结构来表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。
代码示例:
interface Component {
void operation();
}
class Leaf implements Component {
public void operation() {
System.out.println("Leaf operation");
}
}
class Composite implements Component {
private List<Component> children = new ArrayList<>();
public void add(Component child) {
children.add(child);
}
public void operation() {
for (Component child : children) {
child.operation();
}
}
}
9. 装饰者模式(Decorator)
解析: 装饰者模式动态地给一个对象添加一些额外的职责,而不改变其接口。
代码示例:
interface Component {
void operation();
}
class ConcreteComponent implements Component {
public void operation() {
System.out.println("Concrete Component Operation");
}
}
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("Added Behavior");
}
}
10. 观察者模式(Observer)
解析: 观察者模式定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动更新。
代码示例:
interface Observer {
void update();
}
class ConcreteObserver implements Observer {
public void update() {
System.out.println("Observer updated");
}
}
interface Subject {
void attach(Observer observer);
void detach(Observer observer);
void notifyObservers();
}
class ConcreteSubject implements 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();
}
}
}
以上就是对Java系统架构中不可或缺的10大设计模式的深度解析。这些设计模式在Java开发中广泛应用,能够帮助开发者构建更加健壮和灵活的系统。
