structure型design模式tutorial
structure型design模式关注classes and objects 组合, throughinheritance and 组合来implementation new functions, 使得system更加flexible and 可scale. 本tutorial将详细介绍适配器, 装饰器, proxy, 组合etc.common structure型design模式.
structure型design模式overview
structure型design模式 主要目 is through组合classes and objects来构建更 big structure, from 而improvingsystem flexible性 and 可scale性. throughusingstructure型模式, 我们可以:
- 将不同 class组合 in 一起, 形成 new functions
- throughinheritance and 组合来implementationcode 重用
- 使system更加flexible and 可scale
- improvingcode 可maintenance性
1. adapter pattern (Adapter)
concepts
adapter pattern将一个class interface转换成客户希望 另一个interface, 使得原本由于interface不兼容而不能一起工作 那些class可以一起工作.
implementation
// 目标interface
interface Target {
void request();
}
// 适配者class
class Adaptee {
public void specificRequest() {
System.out.println("Adaptee's specific request");
}
}
// 适配器class
class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
// 客户端code
class Client {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter(adaptee);
target.request();
}
}
application场景
- 当你想using一个已经存 in class, 但它 interface不符合你 requirements时
- 当你想creation一个 reusable class, 该class可以 and other不相关 class or 不可预见 class (即interface可能不兼容 class) 协同工作时
- 当你想using一些已经存 in 子class, 但 is 不可能 for 每一个都for子class化以匹配它们 interface时
2. decorator pattern (Decorator)
concepts
decorator pattern动态地给一个object添加一些额 out 职责, 就增加functions来说, decorator pattern比生成子class更 for flexible.
implementation
// componentinterface
interface Component {
void operation();
}
// 具体component
class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("ConcreteComponent operation");
}
}
// 装饰器abstractionclass
abstract class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void operation() {
component.operation();
}
}
// 具体装饰器A
class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addBehavior();
}
private void addBehavior() {
System.out.println("ConcreteDecoratorA added behavior");
}
}
// 具体装饰器B
class ConcreteDecoratorB extends Decorator {
public ConcreteDecoratorB(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addBehavior();
}
private void addBehavior() {
System.out.println("ConcreteDecoratorB added behavior");
}
}
// 客户端code
class Client {
public static void main(String[] args) {
Component component = new ConcreteComponent();
Component decoratedComponent = new ConcreteDecoratorA(new ConcreteDecoratorB(component));
decoratedComponent.operation();
}
}
application场景
- 当你需要动态地给一个object添加额 out 职责时
- 当你需要给一个object添加职责, 但这些职责可能会 in 将来被移除时
- 当你需要through子class化来scalefunctions, 但这样会导致子class数量急剧增加时
3. Proxy Pattern
concepts
proxy模式 for otherobjectproviding一种proxy以控制 for 这个object 访问.
implementation
// 主题interface
interface Subject {
void request();
}
// 真实主题
class RealSubject implements Subject {
@Override
public void request() {
System.out.println("RealSubject request");
}
}
// proxy
class Proxy implements Subject {
private RealSubject realSubject;
@Override
public void request() {
if (realSubject == null) {
realSubject = new RealSubject();
}
preRequest();
realSubject.request();
postRequest();
}
private void preRequest() {
System.out.println("Proxy preRequest");
}
private void postRequest() {
System.out.println("Proxy postRequest");
}
}
// 客户端code
class Client {
public static void main(String[] args) {
Subject proxy = new Proxy();
proxy.request();
}
}
application场景
- 当你需要 in 访问一个object之 before or 之 after 执行一些额 out operation时
- 当你需要控制 for 一个object 访问时
- 当你需要latency加载一个object时
- 当你需要 for 一个远程objectproviding本地proxy时
4. Composite Pattern
concepts
Composite Pattern将object组合成tree形structure以表示"部分-整体" 层次structure, 使得user for 单个object and 组合object using具 has consistency.
implementation
// componentabstractionclass
abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract void add(Component component);
public abstract void remove(Component component);
public abstract void display(int depth);
}
// 叶子node
class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public void add(Component component) {
System.out.println("Cannot add to a leaf");
}
@Override
public void remove(Component component) {
System.out.println("Cannot remove from a leaf");
}
@Override
public void display(int depth) {
System.out.println("|".repeat(depth) + "-" + name);
}
}
// 组合node
class Composite extends Component {
private List children = new ArrayList<>();
public Composite(String name) {
super(name);
}
@Override
public void add(Component component) {
children.add(component);
}
@Override
public void remove(Component component) {
children.remove(component);
}
@Override
public void display(int depth) {
System.out.println("|".repeat(depth) + "+" + name);
for (Component child : children) {
child.display(depth + 1);
}
}
}
// 客户端code
class Client {
public static void main(String[] args) {
Composite root = new Composite("Root");
root.add(new Leaf("Leaf A"));
root.add(new Leaf("Leaf B"));
Composite composite = new Composite("Composite X");
composite.add(new Leaf("Leaf XA"));
composite.add(new Leaf("Leaf XB"));
root.add(composite);
root.add(new Leaf("Leaf C"));
root.display(1);
}
}
application场景
- 当你需要表示object 部分-整体层次structure时
- 当你希望userignore单个object and 组合object 区别, 统一地using组合structurein 所 has object时
structure型模式 选择guide
in 选择structure型模式时, 可以考虑以 under 因素:
- such as果需要将不同interface class组合 in 一起, usingadapter pattern
- such as果需要动态地给object添加额 out 职责, usingdecorator pattern
- such as果需要控制 for object 访问, usingproxy模式
- such as果需要表示object 部分-整体层次structure, usingComposite Pattern
实践case: structure型模式 application
case: graph形user界面 (GUI) system
fake设你需要design一个graph形user界面system, 需要考虑以 under requirements:
- system需要support不同class型 UIcomponent (按钮, 文本框, 面板etc.)
- 需要support给component添加额 out functions (such asedge框, 滚动条etc.)
- 需要supportcomponent 嵌套组合 (such as面板inpackage含按钮 and 文本框)
- 需要support远程访问UIcomponent
in 这个systemin, 你可以application以 under structure型模式:
- adapter pattern: 用于将不同平台 UIcomponent适配 to 统一 interface
- decorator pattern: 用于给component添加额 out functions, such asedge框, 滚动条etc.
- proxy模式: 用于远程访问UIcomponent
- Composite Pattern: 用于表示UIcomponent 层次structure
互动练习
练习1: adapter patternapplication
请design一个适配器, 将一个 old logsystem适配 to new loginterface. old logsystemusinglog(String message)method, new loginterfaceusinginfo(String message), error(String message)etc.method.
练习2: decorator patternapplication
请usingdecorator patterndesign一个咖啡订单system, support给咖啡添加不同 调料 (such as牛奶, 糖, 巧克力etc.) , 并且每种调料都 has 相应 价格.
练习3: Composite Patternapplication
请usingComposite Patterndesign一个filesystem, supportfile and Table of Contents 嵌套, 并able to计算整个filesystem big small .