静态代理
模拟经纪人与明星开演唱会
public interface Star {
/**
* 面谈
*/
void confer();
/**
* 签合同
*/
void signContract();
/**
* 唱歌
*/
void sing();
/**
* 收尾款
*/
void coleectMoney();
}
public class RealStar implements Star {
private String name;
public RealStar(String name) {
this.name = name;
}
private String name;
public RealStar(String name) {
this.name = name;
}
@Override
public void confer() {
System.out.println("RealStar.confer()");
}
public void confer() {
System.out.println("RealStar.confer()");
}
@Override
public void signContract() {
System.out.println("RealStar.signContract()");
}
public void signContract() {
System.out.println("RealStar.signContract()");
}
@Override
public void sing() {
System.out.println(this.name +"唱歌");
}
public void sing() {
System.out.println(this.name +"唱歌");
}
@Override
public void coleectMoney() {
System.out.println("RealStar.coleectMoney()");
}
public void coleectMoney() {
System.out.println("RealStar.coleectMoney()");
}
public String getName() {
return name;
}
return name;
}
public void setName(String name) {
this.name = name;
}
this.name = name;
}
}
public class ProxyStar implements Star{
private Star s;
@Override
public void confer() {
System.out.println("ProxyStar.confer()");;
}
private Star s;
@Override
public void confer() {
System.out.println("ProxyStar.confer()");;
}
@Override
public void signContract() {
System.out.println("ProxyStar.signContract()");
}
public void signContract() {
System.out.println("ProxyStar.signContract()");
}
@Override
public void sing() {
if(s instanceof RealStar)
{
RealStar star=(RealStar)s;
star.sing();
}else {
s.sing();
}
}
public void sing() {
if(s instanceof RealStar)
{
RealStar star=(RealStar)s;
star.sing();
}else {
s.sing();
}
}
@Override
public void coleectMoney() {
System.out.println("ProxyStar.coleectMoney()");
}
public void coleectMoney() {
System.out.println("ProxyStar.coleectMoney()");
}
public ProxyStar(Star ps) {
this.s = ps;
}
this.s = ps;
}
}
/**
* 客户端
* @author 小帆敲代码
*
*/
public class Client {
public static void main(String[] args) {
Star real=new RealStar("Jay");
Star proxy=new ProxyStar(real);
proxy.confer();
proxy.signContract();
proxy.sing();
proxy.coleectMoney();
}
* 客户端
* @author 小帆敲代码
*
*/
public class Client {
public static void main(String[] args) {
Star real=new RealStar("Jay");
Star proxy=new ProxyStar(real);
proxy.confer();
proxy.signContract();
proxy.sing();
proxy.coleectMoney();
}
}