结构型模式: 用于处理类和对象的组合,关注对象之间的关系。其中包括:
- 适配器模式(Adapter Pattern): 将一个类的接口转换为客户希望的另一个接口(XmlAdapter)。
ps:适配器模式不是软件设计阶段考虑的设计模式,而是随着软件的维护,由于不同产品、不同厂家造成功能类似而接口不相同情况下的解决方案。
// 目标接口
interface Target {
void request();
}
// 被适配者类
class Adaptee {
void specificRequest() {
System.out.println("Adaptee's specificRequest");
}
}
// 适配器类
class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
- 桥接模式(Bridge Pattern): 将抽象部分与它的实现部分分离,使它们可以独立地变化。
// 实现部分接口
interface Implementor {
void operationImpl();
}
// 抽象部分类
abstract class Abstraction {
protected Implementor implementor;
public Abstraction(Implementor implementor) {
this.implementor = implementor;
}
abstract void operation();
}
// 具体实现部分类
class ConcreteImplementorA implements Implementor {
@Override
public void operationImpl() {
System.out.println("ConcreteImplementorA operation");
}
}
// 修正抽象部分类
class RefinedAbstraction extends Abstraction {
public RefinedAbstraction(Implementor implementor) {
super(implementor);
}
@Override
void operation() {
System.out.println("RefinedAbstraction operation");
implementor.operationImpl();
}
}
- 组合模式(Composite Pattern): 将对象组合成树形结构以表示“部分-整体”的层次结构(ArrayList.addAll(Collection<>)、)。
// 组件接口
interface Component {
void operation();
}
// 叶子组件类
class Leaf implements Component {
@Override
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);
}
@Override
public void operation() {
System.out.println("Composite operation");
for (Component component : components) {
component.operation();
}
}
}
- 装饰器模式(Decorator Pattern): 动态地给对象添加额外的职责,是继承关系的一个替代方案(springframework.cache.transaction.TrasactionAwareCacheDecorator、prg.apache.ibatis.cache.*)。
ps:提供了比继承更灵活的替代方案。符合开闭原则
// 组件接口
interface Component {
void operation();
}
// 具体组件类
class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("ConcreteComponent operation");
}
}
// 装饰器类
class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void operation() {
component.operation();
}
}
// 具体装饰器类
class ConcreteDecorator extends Decorator {
public ConcreteDecorator(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
System.out.println("ConcreteDecorator operation");
}
}
- 外观模式(Facade Pattern): 提供了一个统一的接口,用于访问子系统中的一组接口(JdbcUtil.、ibatis.session.Configuration、tomcat.RequestFacade、RequestFacade*)。
ps:符合迪米特法则(最少知道原则)、不符合开闭原则。
// 子系统A
class SubsystemA {
void operationA() {
System.out.println("SubsystemA operation");
}
}
// 子系统B
class SubsystemB {
void operationB() {
System.out.println("SubsystemB operation");
}
}
// 外观类
class Facade {
private SubsystemA subsystemA = new SubsystemA();
private SubsystemB subsystemB = new SubsystemB();
void operation() {
subsystemA.operationA();
subsystemB.operationB();
}
}
- 享元模式(Flyweight Pattern): 使用共享对象,有效地支持大量细粒度的对象(Integer、xx连接池的拿/放的方法(双端队列))。
ps:连接池/String的原理。若复用度低、无需使用。需关注线程安全问题。
// 享元工厂类
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);
}
}
// 享元接口
interface Flyweight {
void operation();
}
// 具体享元类
class ConcreteFlyweight implements Flyweight {
private String key;
public ConcreteFlyweight(String key) {
this.key = key;
}
@Override
public void operation() {
System.out.println("ConcreteFlyweight " + key + " operation");
}
}
public class FlyweightPatternExample {
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
Flyweight flyweight1 = factory.getFlyweight("A");
Flyweight flyweight2 = factory.getFlyweight("B");
Flyweight flyweight3 = factory.getFlyweight("A"); // 这里使用了已有的实例
flyweight1.operation();
flyweight2.operation();
flyweight3.operation();
}
}
- 代理模式(Proxy Pattern): 通过代理对象访问目标对象,在不改变目标对象的情况下,控制访问并拓展功能。
// 目标接口
interface Subject {
void request();
}
// 目标类
class RealSubject implements Subject {
@Override
public void request() {
System.out.println("RealSubject's request");
}
}
// 代理类
class Proxy implements Subject {
private RealSubject realSubject;
@Override
public void request() {
if (realSubject == null) {
realSubject = new RealSubject();
}
System.out.println("Proxy's request");
realSubject.request();
}
}