门面模式(Facade Pattern)是面向对象设计模式中的一种,它提供了一个统一的接口,用来访问子系统中的一群接口,从而简化了客户端与子系统之间的交互。本文将深入探讨门面模式,并介绍20种经典设计模式,通过实战案例分析,帮助你轻松入门,成为编程高手。
门面模式概述
门面模式的核心思想是“封装复杂性,提供简单接口”。在复杂的系统中,子系统之间可能会有很多交互,这会增加客户端的复杂性。门面模式通过提供一个统一的接口,将客户端与子系统解耦,简化了客户端的使用。
门面模式的特点
- 简化客户端使用:客户端只需要与门面类交互,而不需要了解子系统中的具体实现。
- 降低系统复杂性:将子系统之间的复杂关系封装起来,简化了系统结构。
- 提高系统可维护性:当子系统发生变化时,只需修改门面类,而不需要修改客户端代码。
20种经典设计模式
下面将介绍20种经典设计模式,并通过案例进行分析。
1. 单例模式(Singleton Pattern)
单例模式确保一个类只有一个实例,并提供一个全局访问点。
代码示例:
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
2. 建造者模式(Builder Pattern)
建造者模式将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
代码示例:
public class CarBuilder {
public Car build() {
Car car = new Car();
car.setEngine(new Engine());
car.setTransmission(new Transmission());
return car;
}
}
public class Car {
private Engine engine;
private Transmission transmission;
// getter and setter
}
3. 工厂方法模式(Factory Method Pattern)
工厂方法模式定义了一个用于创建对象的接口,让子类决定实例化哪一个类。
代码示例:
public interface Factory {
Product create();
}
public class ConcreteFactory implements Factory {
public Product create() {
return new ConcreteProduct();
}
}
public class Product {
// 产品类
}
4. 抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
代码示例:
public interface Factory {
ProductA createProductA();
ProductB createProductB();
}
public class ConcreteFactory implements Factory {
public ProductA createProductA() {
return new ConcreteProductA();
}
public ProductB createProductB() {
return new ConcreteProductB();
}
}
5. 适配器模式(Adapter Pattern)
适配器模式将一个类的接口转换成客户期望的另一个接口,从而使原本接口不兼容的类可以一起工作。
代码示例:
public interface Target {
void request();
}
public class Adaptee {
public void specificRequest() {
// ...
}
}
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
public void request() {
adaptee.specificRequest();
}
}
6. 桥接模式(Bridge Pattern)
桥接模式将抽象部分与实现部分分离,使它们可以独立地变化。
代码示例:
public interface Abstraction {
void operation();
}
public class RefinedAbstraction extends Abstraction {
private Implementor implementor;
public RefinedAbstraction(Implementor implementor) {
this.implementor = implementor;
}
public void operation() {
implementor.operationImpl();
}
}
public interface Implementor {
void operationImpl();
}
public class ConcreteImplementorA implements Implementor {
public void operationImpl() {
// ...
}
}
7. 组合模式(Composite Pattern)
组合模式将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。
代码示例:
public abstract class Component {
public abstract void operation();
}
public class Leaf extends Component {
public void operation() {
// ...
}
}
public class Composite extends Component {
private List<Component> children = new ArrayList<>();
public void add(Component child) {
children.add(child);
}
public void operation() {
for (Component child : children) {
child.operation();
}
}
}
8. 装饰者模式(Decorator Pattern)
装饰者模式动态地给一个对象添加一些额外的职责,而不改变其接口。
代码示例:
public interface Component {
void operation();
}
public class ConcreteComponent implements Component {
public void operation() {
// ...
}
}
public class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
// ...
}
}
9. 外观模式(Facade Pattern)
外观模式提供了一个统一的接口,用来访问子系统中的一群接口。
代码示例:
public interface SubSystemA {
void methodA();
}
public interface SubSystemB {
void methodB();
}
public class Facade {
private SubSystemA subSystemA;
private SubSystemB subSystemB;
public Facade() {
subSystemA = new SubSystemA();
subSystemB = new SubSystemB();
}
public void method() {
subSystemA.methodA();
subSystemB.methodB();
}
}
10. 享元模式(Flyweight Pattern)
享元模式通过共享尽可能多的相似对象来减少内存使用。
代码示例:
public class FlyweightFactory {
private Map<String, Flyweight> flyweights = new HashMap<>();
public Flyweight getFlyweight(String key) {
Flyweight result = flyweights.get(key);
if (result == null) {
result = new ConcreteFlyweight(key);
flyweights.put(key, result);
}
return result;
}
}
public class ConcreteFlyweight implements Flyweight {
private String intrinsic;
public ConcreteFlyweight(String intrinsic) {
this.intrinsic = intrinsic;
}
public void operation(String extrinsic) {
// ...
}
}
11. 代理模式(Proxy Pattern)
代理模式为其他对象提供一种代理以控制对这个对象的访问。
代码示例:
public interface Image {
void display();
}
public class RealImage implements Image {
private String fileName;
public RealImage(String fileName) {
// ...
}
public void display() {
// ...
}
}
public class ProxyImage implements Image {
private RealImage realImage;
private String fileName;
public ProxyImage(String fileName) {
this.fileName = fileName;
}
public void display() {
if (realImage == null) {
realImage = new RealImage(fileName);
}
realImage.display();
}
}
12. 适配器模式(Adapter Pattern)
适配器模式将一个类的接口转换成客户期望的另一个接口,从而使原本接口不兼容的类可以一起工作。
代码示例:
public interface Target {
void request();
}
public class Adaptee {
public void specificRequest() {
// ...
}
}
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
public void request() {
adaptee.specificRequest();
}
}
13. 桥接模式(Bridge Pattern)
桥接模式将抽象部分与实现部分分离,使它们可以独立地变化。
代码示例:
public interface Abstraction {
void operation();
}
public class RefinedAbstraction extends Abstraction {
private Implementor implementor;
public RefinedAbstraction(Implementor implementor) {
this.implementor = implementor;
}
public void operation() {
implementor.operationImpl();
}
}
public interface Implementor {
void operationImpl();
}
public class ConcreteImplementorA implements Implementor {
public void operationImpl() {
// ...
}
}
14. 组合模式(Composite Pattern)
组合模式将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。
代码示例:
public abstract class Component {
public abstract void operation();
}
public class Leaf extends Component {
public void operation() {
// ...
}
}
public class Composite extends Component {
private List<Component> children = new ArrayList<>();
public void add(Component child) {
children.add(child);
}
public void operation() {
for (Component child : children) {
child.operation();
}
}
}
15. 装饰者模式(Decorator Pattern)
装饰者模式动态地给一个对象添加一些额外的职责,而不改变其接口。
代码示例:
public interface Component {
void operation();
}
public class ConcreteComponent implements Component {
public void operation() {
// ...
}
}
public class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
// ...
}
}
16. 外观模式(Facade Pattern)
外观模式提供了一个统一的接口,用来访问子系统中的一群接口。
代码示例:
public interface SubSystemA {
void methodA();
}
public interface SubSystemB {
void methodB();
}
public class Facade {
private SubSystemA subSystemA;
private SubSystemB subSystemB;
public Facade() {
subSystemA = new SubSystemA();
subSystemB = new SubSystemB();
}
public void method() {
subSystemA.methodA();
subSystemB.methodB();
}
}
17. 享元模式(Flyweight Pattern)
享元模式通过共享尽可能多的相似对象来减少内存使用。
代码示例:
public class FlyweightFactory {
private Map<String, Flyweight> flyweights = new HashMap<>();
public Flyweight getFlyweight(String key) {
Flyweight result = flyweights.get(key);
if (result == null) {
result = new ConcreteFlyweight(key);
flyweights.put(key, result);
}
return result;
}
}
public class ConcreteFlyweight implements Flyweight {
private String intrinsic;
public ConcreteFlyweight(String intrinsic) {
this.intrinsic = intrinsic;
}
public void operation(String extrinsic) {
// ...
}
}
18. 代理模式(Proxy Pattern)
代理模式为其他对象提供一种代理以控制对这个对象的访问。
代码示例:
public interface Image {
void display();
}
public class RealImage implements Image {
private String fileName;
public RealImage(String fileName) {
// ...
}
public void display() {
// ...
}
}
public class ProxyImage implements Image {
private RealImage realImage;
private String fileName;
public ProxyImage(String fileName) {
this.fileName = fileName;
}
public void display() {
if (realImage == null) {
realImage = new RealImage(fileName);
}
realImage.display();
}
}
19. 适配器模式(Adapter Pattern)
适配器模式将一个类的接口转换成客户期望的另一个接口,从而使原本接口不兼容的类可以一起工作。
代码示例:
public interface Target {
void request();
}
public class Adaptee {
public void specificRequest() {
// ...
}
}
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
public void request() {
adaptee.specificRequest();
}
}
20. 桥接模式(Bridge Pattern)
桥接模式将抽象部分与实现部分分离,使它们可以独立地变化。
代码示例:
public interface Abstraction {
void operation();
}
public class RefinedAbstraction extends Abstraction {
private Implementor implementor;
public RefinedAbstraction(Implementor implementor) {
this.implementor = implementor;
}
public void operation() {
implementor.operationImpl();
}
}
public interface Implementor {
void operationImpl();
}
public class ConcreteImplementorA implements Implementor {
public void operationImpl() {
// ...
}
}
实战案例分析
以下是一些实战案例分析,帮助你更好地理解这些设计模式。
案例一:设计一个简单的银行系统
在这个案例中,我们可以使用单例模式来确保银行只有一个实例。使用工厂方法模式来创建不同的账户类型,如储蓄账户和支票账户。使用装饰者模式来为账户添加额外的功能,如转账和还款。
案例二:设计一个电商平台
在这个案例中,我们可以使用门面模式来简化与电商平台各个模块的交互。使用工厂方法模式来创建不同的商品类型,如电子产品和服装。使用适配器模式来适配不同的支付方式,如支付宝和微信支付。
案例三:设计一个在线教育平台
在这个案例中,我们可以使用组合模式来表示课程和章节的层次结构。使用装饰者模式来为课程添加额外的功能,如视频和文档。使用代理模式来控制对课程内容的访问,如付费和免费课程。
通过以上案例,你可以看到这些设计模式在实际开发中的应用。掌握这些设计模式,将有助于你提高代码质量,提高开发效率。
总结
本文介绍了门面模式和20种经典设计模式,并通过案例分析了这些模式在实际开发中的应用。希望这篇文章能帮助你轻松入门,成为编程高手。
