04_ProxyPattern 为别人做嫁衣

GiveGift赠送礼物接口(由追求者和代理同时实现)
package cn;

public interface GiveGift {
	public void GiveDolls();
	public void GiveFlowers();
	public void GiveChocolate();
}



Pursuit追求者类
package cn.beans;


import cn.GiveGift;


public class Pursuit implements GiveGift {

	SchoolGirl mm;
	
	public Pursuit(SchoolGirl mm){
		this.mm=mm;
	}
	
	@Override
	public void GiveChocolate() {
		System.out.println(mm.getName()+",送你巧克力。");		
	}

	@Override
	public void GiveDolls() {
		System.out.println(mm.getName()+",送你洋娃娃。");
	}

	@Override
	public void GiveFlowers() {
		System.out.println(mm.getName()+",送你鲜花。");
	}

}



Proxy代理追求者类
package cn.beans;

import cn.GiveGift;

public class Proxy implements GiveGift {

	Pursuit gg;
	
	public Proxy(SchoolGirl mm){
		//衔接处
		this.gg=new Pursuit(mm);
	}
	
	@Override
	public void GiveChocolate() {
		gg.GiveChocolate();
	}

	@Override
	public void GiveDolls() {
		gg.GiveDolls();
	}

	@Override
	public void GiveFlowers() {
		gg.GiveFlowers();		
	}

}



SchoolGirl被追求者类
package cn.beans;

public class SchoolGirl {
	private String name;

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

	public String getName() {
		return name;
	}
	
}



测试类
package junit.test;

import org.junit.Test;

import cn.beans.Proxy;
import cn.beans.SchoolGirl;


public class Test_Proxy {

	@Test public void test(){
		SchoolGirl mm = new SchoolGirl("李娇娇");
		Proxy proxy = new Proxy(mm);
		proxy.GiveChocolate();
		proxy.GiveDolls();
		proxy.GiveFlowers();
	}
}

你可能感兴趣的:(java,JUnit)