欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > 常用设计模式总结

常用设计模式总结

2024/12/24 11:36:33 来源:https://blog.csdn.net/qq_38826019/article/details/143215534  浏览:    关键词:常用设计模式总结

1. 创建型模式

1.1 单例模式(Singleton Pattern)

单例模式确保一个类只有一个实例,并提供一个全局访问点。

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

1.2 工厂模式(Factory Pattern)

工厂模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。

public interface Product {void use();
}public class ConcreteProductA implements Product {@Overridepublic void use() {System.out.println("Using Product A");}
}public class ConcreteProductB implements Product {@Overridepublic void use() {System.out.println("Using Product B");}
}public class Factory {public Product createProduct(String type) {if (type.equals("A")) {return new ConcreteProductA();} else if (type.equals("B")) {return new ConcreteProductB();}return null;}
}

1.3 抽象工厂模式(Abstract Factory Pattern)

抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的具体类。

public interface GUIFactory {Button createButton();Checkbox createCheckbox();
}public class WinFactory implements GUIFactory {@Overridepublic Button createButton() {return new WinButton();}@Overridepublic Checkbox createCheckbox() {return new WinCheckbox();}
}public class MacFactory implements GUIFactory {@Overridepublic Button createButton() {return new MacButton();}@Overridepublic Checkbox createCheckbox() {return new MacCheckbox();}
}

2. 结构型模式

2.1 适配器模式(Adapter Pattern)

适配器模式将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。

public interface Target {void request();
}public class Adaptee {public void specificRequest() {System.out.println("Specific request");}
}public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {this.adaptee = adaptee;}@Overridepublic void request() {adaptee.specificRequest();}
}

2.2 装饰器模式(Decorator Pattern)

装饰器模式动态地给对象添加一些额外的职责,就增加功能来说,装饰器模式比生成子类更为灵活。

public interface Component {void operation();
}public class ConcreteComponent implements Component {@Overridepublic void operation() {System.out.println("ConcreteComponent operation");}
}public abstract class Decorator implements Component {protected Component component;public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {component.operation();}
}public class ConcreteDecoratorA extends Decorator {public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void operation() {super.operation();System.out.println("ConcreteDecoratorA operation");}
}

2.3 代理模式(Proxy Pattern)

代理模式为其他对象提供一种代理以控制对这个对象的访问。

public interface Subject {void request();
}public class RealSubject implements Subject {@Overridepublic void request() {System.out.println("RealSubject request");}
}public class Proxy implements Subject {private RealSubject realSubject;@Overridepublic void request() {if (realSubject == null) {realSubject = new RealSubject();}realSubject.request();}
}

3. 行为型模式

3.1 策略模式(Strategy Pattern)

策略模式定义了一系列算法,并将每一个算法封装起来,而且使它们可以互换。

public interface Strategy {int doOperation(int num1, int num2);
}public class OperationAdd implements Strategy {@Overridepublic int doOperation(int num1, int num2) {return num1 + num2;}
}public class OperationSubtract implements Strategy {@Overridepublic 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);}
}

3.2 观察者模式(Observer Pattern)

观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。

import java.util.ArrayList;
import java.util.List;public class Subject {private List<Observer> observers = new ArrayList<>();private int state;public int getState() {return state;}public void setState(int state) {this.state = state;notifyAllObservers();}public void attach(Observer observer) {observers.add(observer);}public void notifyAllObservers() {for (Observer observer : observers) {observer.update();}}
}public abstract class Observer {protected Subject subject;public abstract void update();
}public class ConcreteObserver extends Observer {public ConcreteObserver(Subject subject) {this.subject = subject;this.subject.attach(this);}@Overridepublic void update() {System.out.println("State changed to: " + subject.getState());}
}

3.3 责任链模式(Chain of Responsibility Pattern)

责任链模式为解除请求的发送者和接受者之间的耦合,而使多个对象都有机会处理这个请求。

public abstract class Handler {protected Handler nextHandler;public void setNextHandler(Handler nextHandler) {this.nextHandler = nextHandler;}public abstract void handleRequest(String request);
}public class ConcreteHandler1 extends Handler {@Overridepublic void handleRequest(String request) {if (request.equals("Handler1")) {System.out.println("Handled by Handler1");} else if (nextHandler != null) {nextHandler.handleRequest(request);}}
}public class ConcreteHandler2 extends Handler {@Overridepublic void handleRequest(String request) {if (request.equals("Handler2")) {System.out.println("Handled by Handler2");} else if (nextHandler != null) {nextHandler.handleRequest(request);}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com