大话设计模式读书笔记 代理模式

schoolgirl.java

package org.ustc.proxy;

public class SchoolGirl {
	String name;

	public String getName() {
		return name;
	}

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

givegift.java

package org.ustc.proxy;

public interface GiveGift {
	public void giveFlower();
	public void giveDoll();
	public void giveChocalate();
}

package org.ustc.proxy;

public class Pursuit implements GiveGift{
	private SchoolGirl girl;
	
	public Pursuit(SchoolGirl girl) {
		this.girl = girl;
	}

	public SchoolGirl getGirl() {
		return girl;
	}

	public void setGirl(SchoolGirl girl) {
		this.girl = girl;
	}

	@Override
	public void giveFlower() {
		// TODO Auto-generated method stub
		System.out.println("送花");
	}

	@Override
	public void giveDoll() {
		// TODO Auto-generated method stub
		System.out.println("送娃娃");
	}

	@Override
	public void giveChocalate() {
		// TODO Auto-generated method stub
		System.out.println("送巧克力");
	}
	
}

package org.ustc.proxy;

public class Proxy implements GiveGift{
	
	private Pursuit pur;
	public Proxy(SchoolGirl girl) {
		// TODO Auto-generated constructor stub
		pur = new Pursuit(girl);
	}

	@Override
	public void giveFlower() {
		// TODO Auto-generated method stub
		pur.giveFlower();
	}

	@Override
	public void giveDoll() {
		// TODO Auto-generated method stub
		pur.giveDoll();
	}

	@Override
	public void giveChocalate() {
		// TODO Auto-generated method stub
		pur.giveChocalate();
	}
	
}


package org.ustc.proxy;

public class Main {
	public static void main(String[] args) {
		SchoolGirl sg = new SchoolGirl();
		sg.setName("娇娇");
		Proxy proxy = new Proxy(sg);
		proxy.giveChocalate();
		proxy.giveDoll();
		proxy.giveFlower();
	}
}



你可能感兴趣的:(设计模式)