可以给人搭配嘻哈服或白领装的程序。
/**
* 人类
* Created by callmeDevil on 2019/6/23.
*/
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void wearTShirts(){
System.out.print("大T恤 ");
}
public void wearBigTrouser(){
System.out.print("垮裤 ");
}
public void wearSneakers(){
System.out.print("破球鞋 ");
}
public void wearSuit(){
System.out.print("西装 ");
}
public void wearTie(){
System.out.print("领带 ");
}
public void wearLeatherShoes(){
System.out.print("皮鞋 ");
}
public void show(){
System.out.println("装扮的" + name);
}
}
/**
* 装扮测试类
* Created by callmeDevil on 2019/6/23.
*/
public class Test {
public static void main(String[] args) {
Person devil = new Person("Devil");
System.out.println("第一种装扮:");
devil.wearTShirts();
devil.wearBigTrouser();
devil.wearSneakers();
devil.show();
System.out.println("\n第二种装扮:");
devil.wearSuit();
devil.wearTie();
devil.wearLeatherShoes();
devil.show();
}
}
第一种装扮:
大T恤 垮裤 破球鞋 装扮的Devil
第二种装扮:
西装 领带 皮鞋 装扮的Devil
如果需要增加“超人”装扮,会导致需要修改“Person”类,违背了开放-封闭原则
/**
* 人类
* Created by callmeDevil on 2019/6/23.
*/
public class Person {
private String name;
public Person(String name){
this.name = name;
}
public void show(){
System.out.print("装扮的" + name);
}
}
/**
* 服装抽象类
* Created by callmeDevil on 2019/6/23.
*/
public abstract class Finery {
public abstract void show();
}
/**
* T恤 类
* Created by callmeDevil on 2019/6/23.
*/
public class TShirts extends Finery {
@Override
public void show() {
System.out.print("大T恤 ");
}
}
/**
* 垮裤 类
* Created by callmeDevil on 2019/6/23.
*/
public class BigTrouser extends Finery {
@Override
public void show() {
System.out.print("垮裤 ");
}
}
// 其余子类相似,此处省略
/**
* 装饰升级版测试
* Created by callmeDevil on 2019/6/23.
*/
public class Test {
public static void main(String[] args) {
Person devil = new Person("Devil");
System.out.println("第一种装扮:");
Finery tShirts = new TShirts();
Finery bigTrouser = new BigTrouser();
Finery sneakers = new Sneakers();
tShirts.show();
bigTrouser.show();
sneakers.show();
devil.show();
System.out.println("\n第二种装扮:");
Finery suit = new Suit();
Finery tie = new Tie();
Finery leatherShoes = new LeatherShoes();
suit.show();
tie.show();
leatherShoes.show();
devil.show();
}
}
第一种装扮:
大T恤 垮裤 破球鞋 装扮的Devil
第二种装扮:
西装 领带 皮鞋 装扮的Devil
现在如果要加超人装扮,只要增加子类就可以了,但是这么做虽然把“服装”类和“人”类分离开了,仍然是存在问题的。把“大T恤”、“垮裤”、“破球鞋”和“装扮的Devil”一个词一个词显示出来,就好比:你光着身子,当着大家的面,先穿T恤,再穿裤子,再穿鞋,仿佛在跳穿衣舞。。。因此需要一个房间(组合类)来换衣服,同时这个穿的顺序对每个人来说是不固定的,有的人喜欢先穿裤子,再穿鞋,最后穿T恤。。只需要把所需的功能按正确的顺序串联起来进行控制即可。
动态地给一个对象添加一些额外的职责,就增加来说,装饰模式比生成子类更为灵活
/**
* 究极进化人类(ConcreteComponent)
* Created by callmeDevil on 2019/6/23.
*/
public class Person {
private String name;
public Person(){}
public Person(String name){
this.name = name;
}
public void show(){
System.out.println("装扮的" + name);
}
}
/**
* 究极进化 服饰类(Decorator)
* Created by callmeDevil on 2019/6/23.
*/
public abstract class Finery extends Person{
protected Person component;
/**
* 装扮
* @param component
*/
public void decorate(Person component) {
this.component = component;
}
@Override
public void show() {
if (component != null) {
component.show();
}
}
}
/**
* 究极进化 T恤(ConcreteDecorator)
* Created by callmeDevil on 2019/6/23.
*/
public class TShirts extends Finery{
@Override
public void show() {
System.out.print("大T恤 ");
super.show();
}
}
/**
* 究极进化 垮裤(ConcreteDecorator)
* Created by callmeDevil on 2019/6/23.
*/
public class BigTrouser extends Finery {
@Override
public void show() {
System.out.print("垮裤 ");
super.show();
}
}
// 其余子类相似,此处省略
/**
* 装饰模式测试
* Created by callmeDevil on 2019/6/23.
*/
public class Test {
public static void main(String[] args) {
Person devil = new Person("Devil");
System.out.println("第一种装扮:");
Sneakers sneakers = new Sneakers();
BigTrouser bigTrouser = new BigTrouser();
TShirts tShirts = new TShirts();
// 装饰
sneakers.decorate(devil);
bigTrouser.decorate(sneakers);
tShirts.decorate(bigTrouser);
tShirts.show();
System.out.println("\n第二种装扮:");
LeatherShoes leatherShoes = new LeatherShoes();
Tie tie = new Tie();
Suit suit = new Suit();
// 装饰
leatherShoes.decorate(devil);
tie.decorate(leatherShoes);
suit.decorate(tie);
suit.show();
}
}
第一种装扮:
大T恤 垮裤 破球鞋 装扮的Devil
第二种装扮:
西装 领带 皮鞋 装扮的Devil
原文:https://www.cnblogs.com/call-me-devil/p/11073500.html