结构型模式主要用于处理对象组合的问题,通过将对象组合成树形结构来表示具有层次的对象组合。以下是几种常见的结构型模式的简要说明和Java代码示例:
适配器模式(Adapter Pattern)
适配器模式将一个类的接口转换成客户期望的另一个接口,使原本由于接口不兼容而不能一起工作的类可以一起工作。
// 目标接口
interface Target {
void request();
}
// 适配者类
class Adaptee {
public void specificRequest() {
System.out.println(“适配者类的特殊方法被调用”);
}
}
// 适配器类
class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
// 客户端代码
class Client {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter(adaptee);
target.request();
}
}
装饰器模式(Decorator Pattern)
装饰器模式动态地给一个对象添加额外的职责,同时不改变其结构。
// 抽象构件
interface Component {
void operate();
}
// 具体构件
class ConcreteComponent implements Component {
@Override
public void operate() {
System.out.println(“具体构件的方法”);
}
}
// 抽象装饰者
abstract class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void operate() {
component.operate();
}
}
// 具体装饰者
class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void operate() {
super.operate();
// 添加额外的职责
System.out.println("为具体构件添加额外的功能A");
}
}
// 客户端代码
class Client {
public static void main(String[] args) {
Component component = new ConcreteComponent();
component = new ConcreteDecoratorA(component);
component.operate();
}
}
代理模式(Proxy Pattern)
代理模式为其他对象提供一个代替或占位符,以控制对它的访问。
// 真实主题
interface Subject {
void request();
}
// 真实实现
class RealSubject implements Subject {
@Override
public void request() {
System.out.println(“真实主题的请求”);
}
}
// 代理
class Proxy implements Subject {
private RealSubject realSubject;
public Proxy() {
this.realSubject = null;
}
public void request() {
if (realSubject == null) {
realSubject = new RealSubject();
}
realSubject.request();
}
}
// 客户端代码
class Client {
public static void main(String[] args) {
Subject proxy = new Proxy();
proxy.request();
}
}
外观模式(Facade Pattern)
外观模式提供了一个统一的接口,用来访问子系统中的一群接口,从而让子系统更容易使用。
// 子系统接口
interface SubSystem {
void operation();
}
// 子系统实现
class SubSystemImplA implements SubSystem {
@Override
public void operation() {
System.out.println(“子系统A的操作”);
}
}
class SubSystemImplB implements SubSystem {
@Override
public void operation() {
System.out.println(“子系统B的操作”);
}
}
// 外观
class Facade {
private SubSystem subSystemA;
private SubSystem subSystemB;
public Facade() {
subSystemA = new SubSystemImplA();
subSystemB = new SubSystemImplB();
}
public void operation() {
subSystemA.operation();
subSystemB.operation();
}
}
// 客户端代码
class Client {
public static void main(String[] args) {
Facade facade = new Facade();
facade.operation();
}
}
桥接模式(Bridge Pattern)
桥接模式将抽象部分与其实现部分分离,使它们可以独立地变化。
// 抽象部分
abstract class Abstraction {
protected Implementor implementor;
public Abstraction(Implementor implementor) {
this.implementor = implementor;
}
public void operation() {
implementor.operation();
}
}
// 具体实现部分
interface Implementor {
void operation();
}
class ConcreteImplementorA implements Implementor {
@Override
public void operation() {
System.out.println(“具体实现A的操作”);
}
}
// 具体抽象部分
class RefinedAbstraction extends Abstraction {
public RefinedAbstraction(Implementor implementor) {
super(implementor);
}
@Override
public void operation() {
super.operation();
// 额外的功能
System.out.println("额外的功能");
}
}
// 客户端代码
class Client {
public static void main(String[] args) {
Implementor implementor = new ConcreteImplementorA();
Abstraction abstraction = new RefinedAbstraction(implementor);
abstraction.operation();
}
}
组合模式(Composite Pattern)
组合模式允许将对象组合成树形结构,以表示“部分-整体”的层次结构。
// 组件接口
interface Component {
void operation();
}
// 叶节点
class Leaf implements Component {
@Override
public void operation() {
System.out.println(“叶节点的操作”);
}
}
// 组合节点
class Composite implements Component {
private List children = new ArrayList<>();
public void add(Component component) {
children.add(component);
}
public void remove(Component component) {
children.remove(component);
}
@Override
public void operation() {
for (Component child : children) {
child.operation();
}
}
}
// 客户端代码
class Client {
public static void main(String[] args) {
Composite root = new Composite();
root.add(new Leaf());
root.add(new Leaf());
root.add(new Composite());
((Composite) root.get(2)).add(new Leaf());
root.operation();
}
}
享元模式(Flyweight Pattern)
享元模式通过共享来高效地支持大量细粒度的对象。
// 享元接口
interface Flyweight {
void operation(String extrinsicState);
}
// 具体享元
class ConcreteFlyweight implements Flyweight {
private String intrinsicState;
public ConcreteFlyweight(String intrinsicState) {
this.intrinsicState = intrinsicState;
}
@Override
public void operation(String extrinsicState) {
System.out.println("内蕴状态:" + intrinsicState + ",外蕴状态:" + extrinsicState);
}
}
// 享元工厂
class FlyweightFactory {
private HashMap<String, Flyweight> flyweights = new HashMap<>();
public Flyweight getFlyweight(String key) {
if (!flyweights.containsKey(key)) {
flyweights.put(key, new ConcreteFlyweight(key));
}
return flyweights.get(key);
}
}
// 客户端代码
class Client {
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
Flyweight flyweight1 = factory.getFlyweight(“状态1”);
flyweight1.operation(“外蕴状态1”);
Flyweight flyweight2 = factory.getFlyweight(“状态2”);
flyweight2.operation(“外蕴状态2”);
}
}
每种结构型模式都有助于解决特定的设计问题,提高代码的灵活性和可维护性。