package com.djk.design.struct.proxy;
public interface Action
{
void buy();
}
package com.djk.design.struct.proxy;
public class Father implements Action
{
private Action action;
public Father(Action action) {
super();
this.action = action;
}
public void buy()
{
if (null == action)
{
System.out.println("儿子不在家,自己去买");
}else
{
System.out.println("儿子在家,委托儿子去买");
action.buy();
}
}
}
package com.djk.design.struct.proxy;
public class Son implements Action
{
@Override
public void buy()
{
System.out.println("老爸叫我去买烟");
}
}
package com.djk.design.struct.proxy;
public class Client
{
public static void main(String[] args)
{
Action father = new Father(new Son());
father.buy();
}
}