大話設計模式_裝飾模式

來源:互聯網
上載者:User

小菜要去見漂亮女孩,不知道穿什麼衣服好,就開始一件一件的開始搭配,實驗出最好效果的搭配...

package com.wzs.design;/** * 大話設計模式--page53 裝飾模式 *  * @author Administrator *  */public class DecorativePattern {public static void main(String[] args) {Person xc = new Person("小菜");System.out.println("第一種裝扮:");WearSneakers pqx = new WearSneakers();// 破球鞋BigTrouser kk = new BigTrouser();// 垮褲Tshirts dtx = new Tshirts(); // 大體恤pqx.decorate(xc);kk.decorate(pqx);dtx.decorate(kk);dtx.show();System.out.println("\n\n第二種裝扮:");WearLeatherShoes px = new WearLeatherShoes();// 皮鞋WearSuit xz = new WearSuit();// 西服WearTie ld = new WearTie();// 領帶px.decorate(xc);ld.decorate(px);xz.decorate(ld);xz.show();}}/* * 人 */class Person {private String name;public Person() {}public Person(String name) {this.name = name;}public void show() {System.out.print(name + "的裝扮.");}}/* * 服飾 */class Finery extends Person {private Person person;/* * 裝扮 */public void decorate(Person person) {this.person = person;}@Overridepublic void show() {if (null != person) {person.show();}}}/* * 大T恤 */class Tshirts extends Finery {@Overridepublic void show() {System.out.print("大T恤  ");super.show();}}/* * 垮褲 */class BigTrouser extends Finery {@Overridepublic void show() {System.out.print("垮褲  ");super.show();}}/* * 破球鞋 */class WearSneakers extends Finery {@Overridepublic void show() {System.out.print("破球鞋  ");super.show();}}/* * 西裝 */class WearSuit extends Finery {@Overridepublic void show() {System.out.print("西裝  ");super.show();}}/* * 領帶 */class WearTie extends Finery {@Overridepublic void show() {System.out.print("領帶  ");super.show();}}/* * 皮鞋 */class WearLeatherShoes extends Finery {@Overridepublic void show() {System.out.print("皮鞋  ");super.show();}}

輸出結果:

第一種裝扮:大T恤  垮褲  破球鞋  小菜的裝扮.第二種裝扮:西裝  領帶  皮鞋  小菜的裝扮.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.