装饰图案实现 [英] Decorator pattern implementation
本文介绍了装饰图案实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始使用C#中的装饰器模式,在头像设计模式书中C#,因此仍然是新的语法,所以我不知道为什么我无法获得下面的代码行的注释。
这是第一个抽象-base类及其派生类在Decorator模式中:
using System;
public abstract class Beverage
{
private String m_description;
//获取饮料的描述
public virtual String Description {get {return m_description;
//计算饮料的成本
public abstract double Cost();
}
// HouseBlend咖啡器具饮料
public class HouseBlend:饮料
{
//构造函数
public HouseBlend(){m_description =House Blend;
//计算House Blend的基本费用
public override double Cost(){return 0.89;
}
// DarkRoast咖啡器具饮料
public class DarkRoast:饮料
{
//构造函数
public DarkRoast(){ m_description =黑暗烤
//计算Dark Roast的基本费用
public override double Cost(){return 1.00;
}
// Espresso咖啡器具饮料
public class Espresso:饮料
{
//构造函数
public Espresso(){ m_description =Espresso;
//计算Espresso的基本费用
public override double Cost(){return 1.99;
}
违规代码位于Mocha类的Cost()方法中:
using System;
//抽象基类CondimentDecorator is-a Beverage
public abstract class CondimentDecorator:Beverage {}
// Mocha实现CondimentDecorater
public class Mocha:CondimentDecorator
{
//调味品装饰器有一个饮料(递归!)
私人饮料m_beverage;
//构造函数绑定传递给成员的对象var
public Mocha(饮料饮料)
{
this.m_beverage = drinks;
}
// getter实现抽象类描述
public override String描述
{
get
{
return m_beverage。说明+,摩卡;
}
}
//获取调味品的成本加上原始饮料的基本费用
//
public new double成本( )// ERROR:'Mocha.Cost()'隐藏继承
{// member'Beverage.Cost()'
return 0.20 + m_beverage.Cost();
}
}
解决方案
更改 new
to override
。另外, m_description
应该是 protected
。
Trying to implement the decorator pattern in C# from the code in the "Head First Design Patterns" book (written in Java).
I am just starting out with C# and am therefore still new to the syntax, so I am not sure why I can't get the commented line of code below to work.
Here is the first abstract-base class and its derived classes in the Decorator pattern:
using System;
public abstract class Beverage
{
private String m_description;
// get a description of the beverage
public virtual String Description { get { return m_description; } }
// calculate cost of the beverage
public abstract double Cost();
}
// HouseBlend coffee implements Beverage
public class HouseBlend : Beverage
{
// Constructor
public HouseBlend() { m_description = "House Blend"; }
// calculate base cost of House Blend
public override double Cost() { return 0.89; }
}
// DarkRoast coffee implements Beverage
public class DarkRoast : Beverage
{
// Constructor
public DarkRoast() { m_description = "Dark Roast"; }
// calculate base cost of Dark Roast
public override double Cost() { return 1.00; }
}
// Espresso coffee implements Beverage
public class Espresso : Beverage
{
// Constructor
public Espresso() { m_description = "Espresso"; }
// calculate base cost of Espresso
public override double Cost() { return 1.99; }
}
The offending code is in the Cost() method of the Mocha class:
using System;
// abstract base class CondimentDecorator is-a Beverage
public abstract class CondimentDecorator : Beverage {}
// Mocha implements the CondimentDecorater
public class Mocha : CondimentDecorator
{
// Condiment decorator has-a Beverage (recursion!)
private Beverage m_beverage;
// Constructor binds the object passed to member var
public Mocha(Beverage beverage)
{
this.m_beverage = beverage;
}
// getter implements abstract class Description
public override String Description
{
get
{
return m_beverage.Description + ", Mocha";
}
}
// get the Cost of the condiment plus the base-cost
// of the original beverage
public new double Cost() // ERROR: 'Mocha.Cost()' hides inherited
{ // member 'Beverage.Cost()'
return 0.20 + m_beverage.Cost();
}
}
解决方案
Change new
to override
. Also, m_description
should be protected
.
这篇关于装饰图案实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文