2021-02-17

【大话设计模式】第6章 穿什么有这么重要?——装饰模式(Java)

装饰模式类图
学习模式要善于变通,如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。
同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,只保留一个ConcreteDecorator类即可。
在这里我们就没必要有Component类了,直接让服饰类Decorator继承人类ConcreteComponent就可。

简化的装饰模式类图

/** * @CreateTime: 2021/02/17 12:13 * @Description: 装饰模式:人穿衣服 */public class PersonDecorator { private String name; public static void main(String[] args) {  PersonDecorator pd = new PersonDecorator("小菜");  // 1、普通方式//  System.out.println("\n第一种装扮:");//  pd.wearTShirts();//  pd.wearTrouser();//  pd.wearSneakers();//  pd.show();////  System.out.println("\n第二种装扮:");//  pd.wearSuit();//  pd.wearTie();//  pd.wearLeatherShoes();//  pd.show();  // 2、继承//  System.out.println("\n第一种装扮:");//  Finery shirts = new TShirts();//  Finery trouser = new Trouser();//  Finery sneakers = new Sneakers();////  shirts.show();//  trouser.show();//  sneakers.show();//  pd.show();////  System.out.println("\n第二种装扮:");//  Finery suit = new Suit();//  Finery tie = new Tie();//  Finery shoes = new LeatherShoes();////  suit.show();//  tie.show();//  shoes.show();//  pd.show();  // 3、装饰模式  Person person = new Person("小菜");  System.out.println("\n第一种装扮:");  Finery shirts = new TShirts();  Finery trouser = new Trouser();  Finery sneakers = new Sneakers();  // 装饰过程  shirts.decorate(person);  trouser.decorate(shirts);  sneakers.decorate(trouser);  sneakers.show();  System.out.println("\n第二种装扮:");  Finery suit = new Suit();  Finery tie = new Tie();  Finery shoes = new LeatherShoes();  suit.decorate(person);  tie.decorate(suit);  shoes.decorate(tie);  shoes.show(); } public PersonDecorator(String name) {  this.name = name; } public void wearTShirts() {  System.out.print("T恤 "); } public void wearTrouser() {  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); }}/** ConcreteComponent */class Person { public Person() { } private String name = ""; public Person(String name) {  this.name = name; } public void show() {  System.out.println("装扮的" + name); }}/** Decorator */class Finery extends Person { private Person component = null; /** 打扮 */ public void decorate(Person component) {  this.component = component; } /** 服饰展示 */ @Override public void show() {  if (component != null) {   component.show();  } }}class TShirts extends Finery { @Override public void show() {  System.out.print("T恤 ");  super.show(); }}class Trouser extends Finery { @Override public void show() {  System.out.print("垮裤 ");  super.show(); }}class Sneakers extends Finery { @Override public void show() {  System.out.print("破球鞋 ");  super.show(); }}class Suit extends Finery { @Override public void show() {  System.out.print("西装 ");  super.show(); }}class Tie extends Finery { @Override public void show() {  System.out.print("领带 ");  super.show(); }}class LeatherShoes extends Finery { @Override public void show() {  System.out.print("皮鞋 ");  super.show(); }}








原文转载:http://www.shaoqun.com/a/563574.html

跨境电商:https://www.ikjzd.com/

邮乐:https://www.ikjzd.com/w/1776

aeo:https://www.ikjzd.com/w/2356


学习模式要善于变通,如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,只保留一个ConcreteDecorator类即可。在这里我们就没必要有Component类了,直接让服饰类De
慧聪商务网:慧聪商务网
邮乐网:邮乐网
适合shopify新手卖家的6个SEO策略:适合shopify新手卖家的6个SEO策略
为避免国内包裹积压 WishPost中邮渠道仅保留7个仓库:为避免国内包裹积压 WishPost中邮渠道仅保留7个仓库
韩国"亚马逊"Coupang再获软银20亿美元投资,估值跻身亚洲前十:韩国"亚马逊"Coupang再获软银20亿美元投资,估值跻身亚洲前十

No comments:

Post a Comment