欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java設計模式學習之裝飾模式

 更新時間:2017年10月12日 11:05:36   作者:南國木棉  
這篇文章主要為大家詳細介紹了java設計模式學習之裝飾模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

裝飾模式:動態(tài)的給一個對象添加一些額外的職責,就增加功能來說,裝飾模式比生成子類更加靈活。

優(yōu)點:裝飾類和被裝飾類可以獨立發(fā)展,不會相互耦合,裝飾模式是繼承的一個替代模式,裝飾模式可以動態(tài)擴展一個實現(xiàn)類的功能。

缺點:多層裝飾比較復雜。

實例:給一個人配置穿衣

1:代碼結(jié)構(gòu)圖

2:創(chuàng)建一個person類(  ConcreteComponent)

package DecoratorModel;

/**
 * 2017-10-9 10:39:09
 * 裝飾器設計模式
 * Person 類 ConcreteComponent
 * @author 我不是張英俊
 *
 */
public class Person {

  public Person(){}
  
  private String name;
  public Person(String name){
    this.name=name;
  }
  
  public void Show(){
    System.out.println("裝扮的"+name);
  }
}

3:服飾類

package DecoratorModel;

/**
 *服飾類(Decorator)
 * @author 我不是張英俊
 *
 */
public class Finery extends Person{

  protected Person component;
  //打扮
  public void Decorate(Person component){
    this.component=component;
  }
  
  public void Show(){
    if(component!=null){
      component.Show();
    }
  }
}

4:具體服飾類

public class Tshirts extends Finery {
  public void Show(){
    System.out.println("大T恤");
    super.Show();
    }
}

public class BigTrouser extends Finery {
  public void Show(){
    System.out.println("垮褲");
    super.Show();
  }
}

public class Sneakers extends Finery {
  public void Show(){
    System.out.println("破球鞋");
    super.Show();
    }
}

public class Suit extends Finery {
  public void Show(){
    System.out.println("西裝");
    super.Show();
  }
}

public class Tie extends Finery {
  public void Show(){
    System.out.println("領帶");
    super.Show();
  }
}

public class LeatherShoes extends Finery {
  public void Show(){
    System.out.println("皮鞋");
    super.Show();
  }
}

5:測試類

public class test {

  public static void main(String[] args) {
    Person xc=new Person("旺財");    
    Sneakers pqx=new Sneakers();
    BigTrouser kk=new BigTrouser();
    Tshirts dtx=new Tshirts();
    pqx.Decorate(xc);
    kk.Decorate(pqx);
    dtx.Decorate(kk);
    dtx.Show();
  }

}

6:控制臺

大T恤
垮褲
破球鞋
裝扮的旺財

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論