java代理模式

代理模式:为其他对象提供一种代理以控制对这个对象的访问

package proxy; public interface IGiveGift { void GiveDolls(); void GiveFlowers(); void GiveChocolate(); } public class SchoolGirl { private String name; String getName() { return name; } void setName(String name) { this.name = name; } } public class Pursuit implements IGiveGift{ SchoolGirl mm; public Pursuit(SchoolGirl mm) { this.mm = mm; } public void GiveChocolate() { // TODO Auto-generated method stub System.out.println(mm.getName() + " 送你巧克力"); } public void GiveDolls() { // TODO Auto-generated method stub System.out.println(mm.getName() + " 送你洋娃娃"); } public void GiveFlowers() { // TODO Auto-generated method stub System.out.println(mm.getName() + " 送你鲜花"); } } public class Proxy implements IGiveGift{ Pursuit gg; public Proxy(SchoolGirl mm) { gg = new Pursuit(mm); } public void GiveChocolate() { // TODO Auto-generated method stub System.out.println("嘻嘻,我是代理-------给巧克力前"); gg.GiveChocolate(); System.out.println("嘻嘻,我是代理-------给巧克力后"); } public void GiveDolls() { // TODO Auto-generated method stub gg.GiveDolls(); } public void GiveFlowers() { // TODO Auto-generated method stub gg.GiveFlowers(); } } public class TestProxy { public static void main(String[] args) { SchoolGirl jiaojiao = new SchoolGirl(); jiaojiao.setName("李娇娇"); Proxy daili = new Proxy(jiaojiao); daili.GiveChocolate(); daili.GiveDolls(); daili.GiveFlowers(); } } 

 

你可能感兴趣的:(java代理模式)