设计模式之总结与回顾(设计模式全面总结与回顾指南)
原创
一、设计模式概述
设计模式是软件工程中的一种重要概念,它提供了一套经过验证的、针对特定问题的解决方案。设计模式可以帮助我们节约代码的可维护性、可扩展性和复用性。本文将全面总结和回顾常见的设计模式,以便于开发者更好地领会和应用。
二、设计模式的分类
设计模式通常分为三类:创建型、结构型和行为型。下面将分别对这三类设计模式进行总结和回顾。
三、创建型设计模式
创建型设计模式关键关注对象的创建过程,它们提供了创建对象的新方法,使对象的创建过程更加灵活、可控。
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)
工厂方法模式定义一个接口用于创建对象,但让子类决定实例化哪个类。工厂方法让一个类的实例化延迟到其子类。
public abstract class Product {
}
public class ConcreteProductA extends Product {
}
public class ConcreteProductB extends Product {
}
public abstract class Creator {
public abstract Product factoryMethod();
}
public class ConcreteCreatorA extends Creator {
public Product factoryMethod() {
return new ConcreteProductA();
}
}
public class ConcreteCreatorB extends Creator {
public Product factoryMethod() {
return new ConcreteProductB();
}
}
3. 抽象工厂模式(Abstract Factory)
抽象工厂模式提供了一个接口,用于创建相关或依存对象的家族,而不需要明确指定具体类。
public abstract class AbstractFactory {
public abstract ProductA createProductA();
public abstract ProductB createProductB();
}
public class ConcreteFactoryA extends AbstractFactory {
public ProductA createProductA() {
return new ConcreteProductA();
}
public ProductB createProductB() {
return new ConcreteProductB();
}
}
public class ConcreteFactoryB extends AbstractFactory {
public ProductA createProductA() {
return new ConcreteProductC();
}
public ProductB createProductB() {
return new ConcreteProductD();
}
}
4. 建造者模式(Builder)
建造者模式将一个繁复对象的构建与其描述分离,使同样的构建过程可以创建不同的描述。
public abstract class Builder {
public abstract void buildPartA();
public abstract void buildPartB();
public abstract Product getResult();
}
public class ConcreteBuilderA extends Builder {
private Product product = new Product();
public void buildPartA() {
product.addPart("PartA");
}
public void buildPartB() {
product.addPart("PartB");
}
public Product getResult() {
return product;
}
}
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void construct() {
builder.buildPartA();
builder.buildPartB();
}
}
四、结构型设计模式
结构型设计模式关键关注类和对象之间的组合,它们通过组合已有类来创建新的功能。
1. 适配器模式(Adapter)
适配器模式允许将一个类的接口转换成客户期望的另一个接口,使原本接口不兼容的类可以一起工作。
public interface Target {
void request();
}
public class Adaptee {
public void specificRequest() {
System.out.println("Adaptee specificRequest");
}
}
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
2. 桥接模式(Bridge)
桥接模式将抽象部分与实现部分分离,使它们可以自由地变化。
public abstract class Abstraction {
protected Implementor implementor;
public Abstraction(Implementor implementor) {
this.implementor = implementor;
}
public abstract void operation();
}
public abstract class Implementor {
public abstract void operationImpl();
}
public class ConcreteAbstractionA extends Abstraction {
public ConcreteAbstractionA(Implementor implementor) {
super(implementor);
}
@Override
public void operation() {
implementor.operationImpl();
}
}
public class ConcreteImplementorA extends Implementor {
@Override
public void operationImpl() {
System.out.println("ConcreteImplementorA operationImpl");
}
}
3. 组合模式(Composite)
组合模式允许将对象组合成树形结构,以描述部分-整体的层次结构。组合模式使用户对单个对象和组合对象的使用具有一致性。
public abstract class Component {
public abstract void operation();
}
public class Leaf extends Component {
@Override
public void operation() {
System.out.println("Leaf operation");
}
}
public class Composite extends Component {
private List
components = new ArrayList<>(); public void add(Component component) {
components.add(component);
}
public void remove(Component component) {
components.remove(component);
}
@Override
public void operation() {
for (Component component : components) {
component.operation();
}
}
}
4. 装饰器模式(Decorator)
装饰器模式允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有类的一个包装。
public abstract class Component {
public abstract void operation();
}
public class ConcreteComponent extends Component {
@Override
public void operation() {
System.out.println("ConcreteComponent operation");
}
}
public abstract class Decorator extends Component {
private Component decoratedComponent;
public Decorator(Component decoratedComponent) {
this.decoratedComponent = decoratedComponent;
}
@Override
public void operation() {
decoratedComponent.operation();
}
}
public class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component decoratedComponent) {
super(decoratedComponent);
}
@Override
public void operation() {
super.operation();
System.out.println("ConcreteDecoratorA operation");
}
}
五、行为型设计模式
行为型设计模式关键关注对象之间的通信,它们通过定义对象之间的通信模式来节约系统的灵活性。
1. 策略模式(Strategy)
策略模式定义一系列算法,将每一个算法封装起来,并使它们可以互换。策略模式让算法的变化自由于使用算法的客户。
public interface Strategy {
void execute();
}
public class ConcreteStrategyA implements Strategy {
@Override
public void execute() {
System.out.println("ConcreteStrategyA execute");
}
}
public class ConcreteStrategyB implements Strategy {
@Override
public void execute() {
System.out.println("ConcreteStrategyB execute");
}
}
public class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public void executeStrategy() {
strategy.execute();
}
}
2. 观察者模式(Observer)
观察者模式定义了一种一对多的依存关系,当一个对象改变状态时,所有依存于它的对象都会得到通知并自动更新。
public interface Observer {
void update();
}
public class ConcreteObserverA implements Observer {
private int state;
public void update() {
System.out.println("ConcreteObserverA update");
}
public void setState(int state) {
this.state = state;
}
public int getState() {
return state;
}
}
public class ConcreteObserverB implements Observer {
private int state;
public void update() {
System.out.println("ConcreteObserverB update");
}
public void setState(int state) {
this.state = state;
}
public int getState() {
return state;
}
}
public class Subject {
private List
observers = new ArrayList<>(); public void addObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
}
3. 状态模式(State)
状态模式允许一个对象在其内部状态改变时改变其行为。对象看起来似乎修改了其类。
public abstract class State {
public abstract void handleRequest();
}
public class ConcreteStateA extends State {
@Override
public void handleRequest() {
System.out.println("ConcreteStateA handleRequest");
}
}
public class ConcreteStateB extends State {
@Override
public void handleRequest() {
System.out.println("ConcreteStateB handleRequest");
}
}
public class Context {
private State state;
public void setState(State state) {
this.state = state;
}
public void request() {
state.handleRequest();
}
}
4. 命令模式(Command)
命令模式是一种行为型设计模式,它将请求封装成一个对象,从而允许用户对请求进行参数化、排队或记录,以及赞成可撤销的操作。
public interface Command {
void execute();
}
public class ConcreteCommandA implements Command {
private Receiver receiver;
public ConcreteCommandA(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.actionA();
}
}
public class ConcreteCommandB implements Command {
private Receiver receiver;
public ConcreteCommandB(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.actionB();
}
}
public class Receiver {
public void actionA() {
System.out.println("Receiver actionA");
}
public void actionB() {
System.out.println("Receiver actionB");
}
}
public class Invoker {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void executeCommand() {
command.execute();
}
}
六、总结
设计模式是软件开发中的一种重要工具,它们可以帮助我们解决特定问题,节约代码的可维护性、可扩展性和复用性。通过本文的总结和回顾,我们期望开发者能够更好地领会和应用设计模式,从而编写出更高质量的代码。