装饰者中的一个问题?

buptxf 2009-04-29 08:34:08
我是在HeadFirst上学到的装饰者模式,他是用Java编的,我改成了C#代码,代码如下:
using System;
using System.Collections.Generic;
using System.Text;

namespace DesignModel
{
class DecoratorPattern
{
public void TestDecorator()
{
Beverage be = new Espresso();
be = new Mocha(be);
be = new Mocha(be);
be = new Whip(be);
Console.WriteLine(be.getDescription()+" $"+be.cost());
}
}

//****************************************************
public abstract class Beverage
{
protected string description = "Unknown Beverage";

public string getDescription()
{
return description;
}

public abstract double cost();
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++

public abstract class CondimentDecorator : Beverage
{
public abstract string getDescription();
}

//*****************************************************

public class Espresso : Beverage
{
public Espresso()
{
description = "Espresso";
}

public override double cost()
{
return 1.99;
}
}


public class Mocha : CondimentDecorator
{
Beverage beverage;

public Mocha(Beverage beverage)
{
this.beverage = beverage;
}

public override string getDescription()
{
return beverage.getDescription() + ", Mocha";
}

public override double cost()
{
return beverage.cost() + 0.20;
}
}

public class Whip : CondimentDecorator
{
Beverage beverage;

public Whip(Beverage beverage)
{
this.beverage = beverage;
}

public override string getDescription()
{
return beverage.getDescription() + ", Whip";
}

public override double cost()
{
return beverage.cost() + 0.70;
}
}

}

为啥输出的结果是Unknown Beverage
而不是:Espresso, Mocha, Mocha, Whip
...全文
70 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
birdlonger 2009-04-30
  • 打赏
  • 举报
回复
整理了下,希望能对你有帮助.个人认为如下写比较规范清晰明了.
using System;
using System.Collections.Generic;
using System.Text;

namespace _009._4._0
{
class Program
{
static void Main(string[] args)
{
Espresso espresso = new Espresso();
CondimentDecorator decorator1 = new MochaDecorator(espresso);
CondimentDecorator decorator2 = new MochaDecorator(decorator1);
CondimentDecorator decorator3 = new WhipDecorator(decorator2);
CondimentDecorator decorator4 = new EspressoDecorator(decorator3);
Console.WriteLine(decorator4.getDescription() + "\tCost :" + decorator4.cost());
Console.ReadLine();
}
}
public abstract class Beverage
{
protected string description = "Unknown Beverage";

public abstract string getDescription();

public abstract double cost();
}

public abstract class CondimentDecorator : Beverage
{
protected Beverage _beverage;
}

public class Espresso : Beverage
{
public Espresso()
{
description = "Espresso";
}
public override string getDescription()
{
return this.description;
}

public override double cost()
{
return 1.99;
}
}
public class Mocha : Beverage
{
public Mocha()
{
description = "Mocha";
}
public override string getDescription()
{
return this.description;
}

public override double cost()
{
return 0.20;
}
}
public class Whip : Beverage
{
public Whip()
{
description = "Whip";
}
public override string getDescription()
{
return this.description;
}

public override double cost()
{
return 0.70;
}
}

public class MochaDecorator : CondimentDecorator
{

public MochaDecorator(Beverage beverage)
{
this._beverage = beverage;
}

public override string getDescription()
{
return _beverage.getDescription() + ", Mocha";

}

public override double cost()
{
return _beverage.cost() + 0.20;

}
}
public class WhipDecorator : CondimentDecorator
{

public WhipDecorator(Beverage beverage)
{
this._beverage = beverage;
}

public override string getDescription()
{
return _beverage.getDescription() + ", Whip";

}

public override double cost()
{
return _beverage.cost() + 0.70;

}
}
public class EspressoDecorator : CondimentDecorator
{
public EspressoDecorator(Beverage beverage)
{
this._beverage = beverage;
}

public override string getDescription()
{
return _beverage.getDescription() + ", Espresso";

}

public override double cost()
{
return _beverage.cost() + 1.99;

}
}

}
birdlonger 2009-04-30
  • 打赏
  • 举报
回复
这个模式如果要写的标准的话,应该建立每个派生类,然后建立一个装饰类就可以了.
birdlonger 2009-04-30
  • 打赏
  • 举报
回复

你还是自己在好好把装饰者模式看看吧.装饰者的核心是,继承被装饰者类的框架并且添加被装饰者的引用,来增加操作.你建立Beverage 和CondimentDecorator的时候有点问题,粗略把你的程序修改了下.(测试通过)..
using System;
using System.Collections.Generic;
using System.Text;

namespace csdn_2009._4._0
{
class DecoratorPattern
{
public void TestDecorator()
{
Beverage be = new Espresso();
be = new Mocha(be);
be = new Mocha(be);
be = new Whip(be);
Console.WriteLine(be.getDescription() + " $" + be.cost());
}
static void Main(string[] args)
{
DecoratorPattern dp = new DecoratorPattern();
dp.TestDecorator();
Console.ReadLine();
}
}

//****************************************************
public abstract class Beverage
{
protected string description = "Unknown Beverage";

public abstract string getDescription();
//{
// return description;
//}

public abstract double cost();
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++

public abstract class CondimentDecorator : Beverage
{
protected Beverage _beverage ;
//public abstract string getDescription();
}

//*****************************************************

public class Espresso : CondimentDecorator //Beverage
{
public Espresso()
{
description = "Espresso";
}
public Espresso(Beverage b)
{
this._beverage = b;
}
public override string getDescription()
{
if (_beverage != null)
return _beverage.getDescription() + this.description;
else
return this.description;
}

public override double cost()
{
if (_beverage != null)
return _beverage.cost() + 1.99;
else
return 1.99;
}
}


public class Mocha : CondimentDecorator
{
//Beverage beverage;

public Mocha(Beverage beverage)
{
this._beverage = beverage;
}

public override string getDescription()
{
if (_beverage != null)
return _beverage.getDescription() + ", Mocha";
else
return "Mocha";
}

public override double cost()
{
if (_beverage != null)

return _beverage.cost() + 0.20;
else
return 0.20;
}
}

public class Whip : CondimentDecorator
{
//Beverage beverage;

public Whip(Beverage beverage)
{
this._beverage = beverage;
}

public override string getDescription()
{
if (_beverage != null)
return _beverage.getDescription() + ", Whip";
else
return "Whip";
}

public override double cost()
{
if (_beverage != null)
return _beverage.cost() + 0.70;
else
return 0.70;
}
}
}
CompilerZy 2009-04-30
  • 打赏
  • 举报
回复
be = new Mocha(be);
be = new Mocha(be);
be = new Whip(be);
~~~~~~~~~~~~~~~~~~~~~~~
这边确定没有写错?
xufzu123 2009-04-30
  • 打赏
  • 举报
回复
学习~~
buptxf 2009-04-29
  • 打赏
  • 举报
回复
大家帮忙呀!我加分

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧