在软件开发过程中,设计模式是一种解决特定问题的经验总结和最佳实践。设计模式可以被分为三大类:创建型模式、结构型模式、行为型模式。接下来我们将从不同的角度详细介绍这三大类设计模式。
一、创建型模式
创建型模式主要关注对象的创建过程,在对象的创建过程中封装复杂性,并提供灵活的方式创建对象。创建型模式包括以下几种模式:
1. 工厂方法模式
工厂方法模式定义一个用于创建对象的接口,让子类决定将哪一个类实例化。可以将需要创建的对象类型从具体工厂的实现中解耦出来,使得代码更加灵活。以下是工厂方法模式的示例代码:
interface Car { void run(); } class Benz implements Car { @Override public void run() { System.out.println("Benz is running"); } } class BMW implements Car { @Override public void run() { System.out.println("BMW is running"); } } interface CarFactory { Car createCar(); } class BenzFactory implements CarFactory { @Override public Car createCar() { return new Benz(); } } class BMWFactory implements CarFactory { @Override public Car createCar() { return new BMW(); } } public class Main { public static void main(String[] args) { CarFactory factory = new BenzFactory(); Car car = factory.createCar(); car.run(); } }
2. 抽象工厂模式
抽象工厂模式提供一个用于创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。抽象工厂可以为用户提供一组对象,这组对象的实现可以有多种选择,基于用户的需求而变化。以下是抽象工厂模式的示例代码:
interface Engine { void start(); } interface Wheel { void roll(); } interface CarFactory { Engine createEngine(); Wheel createWheel(); } class BenzEngine implements Engine { @Override public void start() { System.out.println("Benz engine start"); } } class BMWEngine implements Engine { @Override public void start() { System.out.println("BMW engine start"); } } class BenzWheel implements Wheel { @Override public void roll() { System.out.println("Benz wheel roll"); } } class BMWWheel implements Wheel { @Override public void roll() { System.out.println("BMW wheel roll"); } } class BenzFactory implements CarFactory { @Override public Engine createEngine() { return new BenzEngine(); } @Override public Wheel createWheel() { return new BenzWheel(); } } class BMWFactory implements CarFactory { @Override public Engine createEngine() { return new BMWEngine(); } @Override public Wheel createWheel() { return new BMWWheel(); } } public class Main { public static void main(String[] args) { CarFactory factory = new BenzFactory(); Engine engine = factory.createEngine(); Wheel wheel = factory.createWheel(); engine.start(); wheel.roll(); } }
二、结构型模式
结构型模式主要关注对象的组合方式,通过对象间的类组合形成更大的结构,使得系统更加灵活和易于维护。结构型模式包括以下几种模式:
1. 适配器模式
适配器模式将一个类的接口转换为客户希望的另外一个接口,适配器模式可以让原本不兼容的类在一起工作。以下是适配器模式的示例代码:
interface Electronic220V { void powerOn(); } class Outlet { public void powerOn(Electronic220V electronic220V) { electronic220V.powerOn(); } } class Electronic110V { public void powerOn() { System.out.println("Electronic110V is powered on"); } } class ElectronicAdapter implements Electronic220V { private Electronic110V electronic110V; public ElectronicAdapter(Electronic110V electronic110V) { this.electronic110V = electronic110V; } @Override public void powerOn() { electronic110V.powerOn(); } } public class Main { public static void main(String[] args) { Electronic110V electronic110V = new Electronic110V(); ElectronicAdapter adapter = new ElectronicAdapter(electronic110V); Outlet outlet = new Outlet(); outlet.powerOn(adapter); } }
2. 装饰器模式
装饰器模式可以动态地给一个对象添加一些额外的职责,同时又不改变其结构。装饰器模式可以实现设计要求开放闭合原则(Open–Closed Principle),即软件实体应该对扩展开放,对修改关闭。以下是装饰器模式的示例代码:
interface Shape { void draw(); } class Circle implements Shape { @Override public void draw() { System.out.println("Draw a circle"); } } class Square implements Shape { @Override public void draw() { System.out.println("Draw a square"); } } abstract class ShapeDecorator implements Shape { protected Shape decoratedShape; public ShapeDecorator(Shape decoratedShape) { this.decoratedShape = decoratedShape; } @Override public void draw() { decoratedShape.draw(); } } class RedBorderDecorator extends ShapeDecorator { public RedBorderDecorator(Shape decoratedShape) { super(decoratedShape); } @Override public void draw() { decoratedShape.draw(); System.out.println("Add a red border"); } } public class Main { public static void main(String[] args) { Shape circle = new Circle(); Shape redCircle = new RedBorderDecorator(circle); Shape redSquare = new RedBorderDecorator(new Square()); circle.draw(); redCircle.draw(); redSquare.draw(); } }
三、行为型模式
行为型模式主要关注对象间的通信方式和通信协议,通过定义对象间的交互进行协同工作,从而完成特定的功能。行为型模式包括以下几种模式:
1. 观察者模式
观察者模式定义了一个一对多的依赖关系,当对象状态发生改变时,所有依赖它的对象都会收到通知并自动更新。以下是观察者模式的示例代码:
import java.util.ArrayList; import java.util.List; interface Observer { void update(); } class Subject { private List 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(); } } } class ConcreteObserver implements Observer { private String name; private Subject subject; public ConcreteObserver(String name, Subject subject) { this.name = name; this.subject = subject; } @Override public void update() { System.out.println(name + " received notification"); } } public class Main { public static void main(String[] args) { Subject subject = new Subject(); Observer observer1 = new ConcreteObserver("Observer1", subject); Observer observer2 = new ConcreteObserver("Observer2", subject); Observer observer3 = new ConcreteObserver("Observer3", subject); subject.attach(observer1); subject.attach(observer2); subject.attach(observer3); subject.notifyObservers(); } }
2. 命令模式
命令模式将请求封装成对象,以便解耦发送者和接收者。命令模式中包含的对象有:请求者、接收者、命令对象和客户端。以下是命令模式的示例代码:
interface Command { void execute(); } class Receiver { public void action() { System.out.println("Action"); } } class ConcreteCommand implements Command { private Receiver receiver; public ConcreteCommand(Receiver receiver) { this.receiver = receiver; } @Override public void execute() { receiver.action(); } } class Invoker { private Command command; public void setCommand(Command command) { this.command = command; } public void executeCommand() { command.execute(); } } public class Main { public static void main(String[] args) { Receiver receiver = new Receiver(); ConcreteCommand command = new ConcreteCommand(receiver); Invoker invoker = new Invoker(); invoker.setCommand(command); invoker.executeCommand(); } }
以上就是对设计模式分为哪三大类的讲解,通过示例代码加深了对每个模式的具体理解,希望对读者有所帮助。
原创文章,作者:ETIMX,如若转载,请注明出处:https://www.506064.com/n/349414.html