使用组合的方式来创建新类, 并可以重新定制某些类的特定行为
class Action { private OnEnterListener enterListener; private OnLeaveListener leaveListener; public Action(OnEnterListener enterListener) { this.enterListener = enterListener; } public Action(OnLeaveListener leaveListener) { this.leaveListener = leaveListener } public interface OnEnterListener { void onEnter(); } public interface OnLeaveListener { void onLeave(); } } class Persone implements Action.OnEnterListener, Action.OnLeaveListener { /* 使用组合的方式定义一个新类,而且可以对特定的对象的特定行为进行重写 */ private Action action; public Persone() { action = new Action(this); } public void onEnter() { } public void onLeave() { } }
上面是在代码中看到的设计模式