码迷,mamicode.com
首页 > 其他好文 > 详细

装饰模式(Decorator)

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

标签:blog   http   2014   new   app   log   

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)

标签:blog   http   2014   new   app   log   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!