首页 > 其他 > 详细

装饰模式(Decorator)

时间:2014-07-01 12:49:39      阅读:386      评论:0      收藏:0      [点我收藏+]

bubuko.com,布布扣bubuko.com,布布扣

 bubuko.com,布布扣

动态地给一个对象增加一些额外的职责。就增加功能而言,Decorator模式比生成子类更为灵活。

 

public class Tank

{

public abstract void Shot();

public abstract void Run();

}

public class T50 extends Tank

{

public override void Shot(){};

public override void Run(){};

}

public class T75 extends Tank

{

public override void Shot(){};

public override void Run(){};

}

public  abstract class Decorator extends Tank

{

private Tank tank;

public Decorator(Tank tank)

{

this.tank = tank;

}

void Shot()

{

tank.Shot();

}

void Run()

{

tank.Run();

}

}

public class DecoratorA extends Decorator 

{

public DecoratorA(Tank tank)

{

super(tank);

}

public override void Shot()

{

  红外

  base.shot();

}

public override void Run()

{

功能扩展 红外

base.Run();

}

}

public class DecoratorB extends Decorator 

{

public DecoratorB(Tank tank)

{

super(tank);

}

public override void Shot()

{

  功能扩展 两栖

  base.shot();

}

public override void Run()

{

功能扩展 两栖

base.Run();

}

}

class App

{

public static void Main()

{

Tank tank = new T50();

DecoratorA da = new Decorator(tank);红外功能

DecoratorB db = new Decorator(da); 红外,两栖

}

}

 bubuko.com,布布扣

装饰模式(Decorator),布布扣,bubuko.com

装饰模式(Decorator)

原文:http://www.cnblogs.com/hyp5490-/p/3817255.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!