1,被追求者类:
package yx.designpattern.proxy;
public class SchoolGril {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
2,代理接口
package yx.designpattern.proxy;
interface IGiveGift {
public void giveDolls();
public void giveFlowers();
public void giveChocolate();
}
3,追求者类:
package yx.designpattern.proxy;
public class Pursuit implements IGiveGift {
SchoolGril mm;
public Pursuit(SchoolGril mm) {
this.mm = mm;
}
@Override
public void giveDolls() {
System.out.println(mm.getName()+"::送你玩具");
}
@Override
public void giveFlowers() {
System.out.println(mm.getName()+"::送你花");
}
@Override
public void giveChocolate() {
System.out.println(mm.getName()+"::送你巧克力");
}
}
4,代理类
package yx.designpattern.proxy;
public class Proxy implements IGiveGift {
Pursuit gg;
public Proxy(SchoolGril mm) {
gg = new Pursuit(mm);
}
@Override
public void giveDolls() {
gg.giveDolls();
}
@Override
public void giveFlowers() {
gg.giveFlowers();
}
@Override
public void giveChocolate() {
gg.giveChocolate();
}
}
5,客户端类
package yx.designpattern.proxy;
public class Test {
public static void main(String[] args) {
SchoolGril jiaojiao=new SchoolGril();
jiaojiao.setName("李娇娇");
Proxy proxy=new Proxy(jiaojiao);
proxy.giveDolls();
proxy.giveFlowers();
proxy.giveChocolate();
}
}