大话设计模式_代理模式

卓贾易让戴励帮助自己追娇娇为场景,卓贾易不好意思直接送礼物给娇娇,让戴励代替自己送.

package com.wzs.design;

/**
 * 大话设计模式--page62 代理模式
 * 
 * @author Administrator
 * 
 */
public class ProxyPattern {
	public static void main(String[] args) {
		SchoolGirl jiaojiao = new SchoolGirl("李娇娇");
		Pursuit pursuit = new Pursuit(jiaojiao, "卓贾易");
		Proxy proxy = new Proxy(pursuit);

		proxy.giveDolls();
		proxy.giveFlowers();
		proxy.giveChocolate();
	}
}

/*
 * 女孩
 */
class SchoolGirl {
	private String name;

	public SchoolGirl(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

/*
 * 送礼物接口
 */
interface IGiveGift {
	public abstract void giveDolls();

	public abstract void giveFlowers();

	public abstract void giveChocolate();
}

/*
 * 追求者
 */
class Pursuit implements IGiveGift {
	private SchoolGirl schoolGirl;
	private String PursuitName;

	public Pursuit(SchoolGirl schoolGirl, String pursuitName) {
		this.schoolGirl = schoolGirl;
		PursuitName = pursuitName;
	}

	@Override
	public void giveChocolate() {
		System.out.println(PursuitName + " 送给  " + schoolGirl.getName() + " 巧克力.");
	}

	@Override
	public void giveDolls() {
		System.out.println(PursuitName + " 送给  " + schoolGirl.getName() + " 洋娃娃.");
	}

	@Override
	public void giveFlowers() {
		System.out.println(PursuitName + " 送给  " + schoolGirl.getName() + " 鲜花.");
	}
}

/*
 * 送礼物的代理
 */
class Proxy implements IGiveGift {
	Pursuit pursuit;

	public Proxy(Pursuit pursuit) {
		this.pursuit = pursuit;
	}

	@Override
	public void giveChocolate() {
		pursuit.giveChocolate();
	}

	@Override
	public void giveDolls() {
		pursuit.giveDolls();
	}

	@Override
	public void giveFlowers() {
		pursuit.giveFlowers();
	}
}


你可能感兴趣的:(代理模式,大话设计模式,追女生)