适配器模式是一种结构型设计模式,它用于将不兼容的接口转换为可兼容的接口。适配器模式允许两个不兼容的类能够协同工作,通过将一个类的接口转换为另一个类所期望的接口形式。这样就能够在不修改现有代码的情况下,使两个不兼容的类能够相互协作。
适配器模式通常在以下场景中使用:
// 目标接口
interface Target {
void request();
}
// 需要适配的类
class Adaptee {
void specificRequest() {
System.out.println("Adaptee's specific request");
}
}
// 适配器类
class Adapter implements Target {
private Adaptee adaptee;
Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
// 使用适配器
public class Main {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter(adaptee);
target.request();
}
}
桥接模式是一种结构型设计模式,它将抽象部分和实现部分分离,使它们可以独立地变化。通过将抽象和实现分离,桥接模式可以使两者能够独立地进行扩展。这种模式的核心目标是通过解耦抽象和实现,来使它们能够相互独立地变化。
桥接模式通常在以下场景中使用:
// 实现部分接口
interface Implementor {
void operationImp();
}
// 具体实现部分A
class ConcreteImplementorA implements Implementor {
@Override
public void operationImp() {
System.out.println("ConcreteImplementorA operationImp");
}
}
// 具体实现部分B
class ConcreteImplementorB implements Implementor {
@Override
public void operationImp() {
System.out.println("ConcreteImplementorB operationImp");
}
}
// 抽象部分接口
abstract class Abstraction {
protected Implementor implementor;
Abstraction(Implementor implementor) {
this.implementor = implementor;
}
abstract void operation();
}
// 具体抽象部分
class ConcreteAbstraction extends Abstraction {
ConcreteAbstraction(Implementor implementor) {
super(implementor);
}
@Override
void operation() {
implementor.operationImp();
}
}
// 使用桥接模式
public class Main {
public static void main(String[] args) {
Implementor implementorA = new ConcreteImplementorA();
Implementor implementorB = new ConcreteImplementorB();
Abstraction abstractionA = new ConcreteAbstraction(implementorA);
abstractionA.operation();
Abstraction abstractionB = new ConcreteAbstraction(implementorB);
abstractionB.operation();
}
}
组合模式是一种结构型设计模式,它允许将对象组合成树形结构,以表示整体-部分的层次结构。通过使用组合模式,可以以统一的方式处理对象组合和单个对象。
// 抽象组件
interface Component {
void operation();
}
// 叶子组件
class Leaf implements Component {
public void operation() {
System.out.println("Leaf operation");
}
}
// 复合组件
class Composite implements Component {
private List<Component> components = new ArrayList<>();
public void add(Component component) {
components.add(component);
}
public void remove(Component component) {
components.remove(component);
}
public void operation() {
System.out.println("Composite operation");
for (Component component : components) {
component.operation();
}
}
}
// 使用示例
public class Main {
public static void main(String[] args) {
Component leaf1 = new Leaf();
Component leaf2 = new Leaf();
Component composite1 = new Composite();
Component composite2 = new Composite();
composite1.add(leaf1);
composite1.add(leaf2);
composite2.add(composite1);
composite2.operation();
}
}
优点:
缺点:
装饰器模式是一种结构型设计模式,它允许动态地给对象添加额外的功能。装饰器模式通过使用额外的对象来包装原始对象,并在调用原始对象的方法前后执行额外的操作。
# 抽象组件
class Component:
def operation(self):
pass
# 具体组件
class ConcreteComponent(Component):
def operation(self):
print("ConcreteComponent operation")
# 装饰器基类
class Decorator(Component):
def __init__(self, component):
self._component = component
def operation(self):
self._component.operation()
# 具体装饰器
class ConcreteDecoratorA(Decorator):
def operation(self):
super().operation()
print("ConcreteDecoratorA operation")
class ConcreteDecoratorB(Decorator):
def operation(self):
print("ConcreteDecoratorB operation")
super().operation()
# 使用示例
component = ConcreteComponent()
decoratorA = ConcreteDecoratorA(component)
decoratorB = ConcreteDecoratorB(decoratorA)
decoratorB.operation()
优点:
缺点:
外观模式是一种结构型设计模式,它提供了一个统一的接口,用于访问子系统的一组接口。外观模式通过封装一组复杂的子系统接口,简化了客户端与子系统之间的交互。
# 子系统A
class SubsystemA:
def methodA(self):
print("SubsystemA methodA")
# 子系统B
class SubsystemB:
def methodB(self):
print("SubsystemB methodB")
# 外观类
class Facade:
def __init__(self):
self._subsystemA = SubsystemA()
self._subsystemB = SubsystemB()
def operation(self):
self._subsystemA.methodA()
self._subsystemB.methodB()
# 使用示例
facade = Facade()
facade.operation()
优点:
缺点:
享元模式是一种结构型设计模式,它通过共享对象来减少内存使用,以支持大量细粒度的对象。享元模式将对象的状态分为内部状态和外部状态,内部状态可以共享,外部状态可以由客户端传入。
import java.util.HashMap;
import java.util.Map;
// 具体享元类
class ConcreteFlyweight implements Flyweight {
private String intrinsicState;
public ConcreteFlyweight(String intrinsicState) {
this.intrinsicState = intrinsicState;
}
public void operation(String extrinsicState) {
System.out.println("Intrinsic state: " + intrinsicState);
System.out.println("Extrinsic state: " + extrinsicState);
}
}
// 享元工厂
class FlyweightFactory {
private Map<String, Flyweight> flyweights = new HashMap<>();
public Flyweight getFlyweight(String key) {
if (!flyweights.containsKey(key)) {
flyweights.put(key, new ConcreteFlyweight(key));
}
return flyweights.get(key);
}
}
// 使用示例
public class Main {
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
Flyweight flyweight1 = factory.getFlyweight("key1");
flyweight1.operation("state1");
Flyweight flyweight2 = factory.getFlyweight("key2");
flyweight2.operation("state2");
}
}
优点:
缺点:
代理模式是一种结构型设计模式,用于控制对象的访问,并在访问对象时添加额外的处理。代理模式通过创建一个代理对象来代替原始对象,客户端使用代理对象进行操作,代理对象在执行操作前后可以添加一些额外的逻辑。
// 抽象主题
interface Subject {
void operation();
}
// 具体主题
class RealSubject implements Subject {
public void operation() {
System.out.println("RealSubject operation");
}
}
// 代理类
class Proxy implements Subject {
private RealSubject realSubject;
public void operation() {
beforeOperation();
if (realSubject == null) {
realSubject = new RealSubject();
}
realSubject.operation();
afterOperation();
}
private void beforeOperation() {
System.out.println("Proxy before operation");
}
private void afterOperation() {
System.out.println("Proxy after operation");
}
}
// 使用示例
public class Main {
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.operation();
}
}
优点:
缺点:
装饰器模式是一种结构型设计模式,允许动态地给对象添加额外的功能。装饰器模式通过使用额外的对象来包装原始对象,并在调用原始对象的方法前后执行额外的操作。
# 抽象组件
class Component:
def operation(self):
pass
# 具体组件
class ConcreteComponent(Component):
def operation(self):
print("ConcreteComponent operation")
# 装饰器基类
class Decorator(Component):
def __init__(self, component):
self._component = component
def operation(self):
self._component.operation()
# 具体装饰器
class ConcreteDecoratorA(Decorator):
def operation(self):
print("ConcreteDecoratorA operation")
super().operation()
class ConcreteDecoratorB(Decorator):
def operation(self):
super().operation()
print("ConcreteDecoratorB operation")
# 使用示例
component = ConcreteComponent()
decoratorA = ConcreteDecoratorA(component)
decoratorB = ConcreteDecoratorB(decoratorA)
decoratorB.operation()
优点:
缺点: