日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

裝飾(Decorator)模式

系統(tǒng) 2005 0

裝飾(Decorator)模式又名包裝(Wrapper)模式。裝飾模式以對(duì)客戶端透明的方式擴(kuò)展對(duì)象的功能,是繼承關(guān)系的一個(gè)替代方案。

一,結(jié)構(gòu)

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

抽象構(gòu)件(Component)角色: 給出一個(gè)抽象接口,以規(guī)范準(zhǔn)備接收附加責(zé)任的對(duì)象。

具體構(gòu)件(Concrete Component)角色: 定義一個(gè)將要接收附加責(zé)任的類。

裝飾(Decorator)角色: 持有一個(gè)構(gòu)件(Component)對(duì)象的實(shí)例,并定義一個(gè)與抽象構(gòu)件接口一致的接口。

具體裝飾(Concrete Decorator)角色: 負(fù)責(zé)給構(gòu)件對(duì)象"貼上"附加的責(zé)任。

二,示例代碼

    /**
 * 抽象構(gòu)件
 * @author Salmon
 *
 */
public interface Component {
	public void operation();
}

/**
 * 具體構(gòu)件
 * @author Salmon
 *
 */
public class ConcreteComponent implements Component {
	public void operation() {
		System.out.println("ConcreteComponent.Operation()");
	}
}

/**
 * 裝飾(即組合又繼承)
 * @author Salmon
 *
 */
public class Decorator implements Component {
	protected Component component;

	public void setComponent(Component component) {
		this.component = component;
	}

	public void operation() {
		if (component != null)
			component.operation();
	}
}

/**
 * 具體裝飾
 * @author Salmon
 *
 */
public class ConcreteDecoratorA extends Decorator {
	private String addedState;

	public void operation() {
		super.operation();
		addedState = "new state";
		System.out.println("ConcreteDecoratorA.Operation()");
	}
}

/**
 * 具體裝飾
 * @author Salmon
 *
 */
public class ConcreteDecoratorB extends Decorator {
	public void operation() {
		super.operation();
		AddedBehavior();
		System.out.println("ConcreteDecoratorB.Operation()");
	}

	private void AddedBehavior() {
		System.out.println("new state");
	}
}

/**
 * 客戶代碼
 * @author Salmon
 *
 */
public class Client {
	public static void main(String[] args) {
		ConcreteComponent c = new ConcreteComponent();
		ConcreteDecoratorA d1 = new ConcreteDecoratorA();
		ConcreteDecoratorB d2 = new ConcreteDecoratorB();
		d1.setComponent(c);
		d2.setComponent(d1);
		d2.operation();
	}
}
  

?

裝飾(Decorator)模式


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 乌拉特中旗| 临夏县| 筠连县| 永德县| 南皮县| 商南县| 稻城县| 哈尔滨市| 崇文区| 新闻| 陕西省| 南陵县| 大厂| 壤塘县| 淄博市| 枞阳县| 麟游县| 遵义市| 南京市| 通江县| 阿瓦提县| 新津县| 阿克陶县| 阳春市| 岑溪市| 秀山| 呼图壁县| 千阳县| 加查县| 萨迦县| 三门峡市| 聂荣县| 灵山县| 阿城市| 苍山县| 凌源市| 凤山县| 昌图县| 蕉岭县| 肇东市| 内江市|