装饰模式的基本结构
classDiagram class Component{ <<interface>> + operation(); } class ConcreateComponent{ + operation(); } class Decrator{ - Component component; + operation(); } class ConcreateDecrator1{ + operation(); } class ConcreateDecrator2{ + operation(); } ConcreateComponent ..|> Component Decrator..|> Component Decrator <|-- ConcreateDecrator1 Decrator <|-- ConcreateDecrator2以实现不同的穿搭风格为例
classDiagram class client{ main(String[] args) } class ICharacter{ <<interface>> + show(); } class Finery{ - Component component; + operation(); } class Persion{ - String name; + operation(); } class Suit{ + operation(); } class Tie{ + operation(); } Persion ..|> ICharacter Finery..|> ICharacter Finery <|-- Suit Finery <|-- Tie client o-- Persion Tie o-- Persion Suit o-- Persion个人形象抽象类
public interface ICharacter {
public void show();
}
人员实体类
public class Person implements ICharacter {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void show() {
System.out.println("装扮的" + name);
}
}
装饰类
public class Finery implements ICharacter {
protected ICharacter component;
public void decorate(ICharacter component) {
this.component = component;
}
public void show() {
if (this.component != null) {
this.component.show();
}
}
}
服装类
西装
public class Suit extends Finery {
public void show() {
System.out.print(" 西装");
super.show();
}
}
领带
public class Tie extends Finery {
public void show() {
System.out.print(" 领带");
super.show();
}
}
调用
public static void main(String[] args) {
System.out.println("**********************************************");
System.out.println("《大话设计模式》代码样例");
System.out.println();
Person xc = new Person();
xc.setName("小菜");
System.out.println(" 第一种装扮:");
Tie ld = new Tie(); // 生成领带实例
ld.decorate(xc); // 领带装饰“有领带装饰的小菜”
Suit xz = new Suit(); // 生成西装实例
xz.decorate(ld); // 西装装饰“有领带西装装饰的小菜”
xz.show(); // 执行形象展示
System.out.println("**********************************************");
}
标签:show,大话,System,笔记,public,ICharacter,operation,设计模式,class
From: https://www.cnblogs.com/lixiuming521125/p/17800833.html