I am curently working on an example using the decorator pattern.
My model looks something like this: Model
The abstract class ICharakter is the interface for the classes to decorate:
public abstract class ICharakter
{
public string Name { get; set; }
public int HealthPoints { get; set; }
public abstract AttackInformation attack();
}
The Component Knight inherites this class:
class Knight : ICharakter{
public int KnightArmourPoints { get; set; }
public Knight(string name, int healthpoints){
this.Name = name;
this.HealthPoints = healthpoints;
}
public override AttackInformation attack(){
//implementation
}
}
This is what the abstract Decorator-Class looks like:
abstract class CharakterDecorator : ICharakter
{
protected ICharakter Charakter;
public CharakterDecorator(ICharakter charakter)
{
this.Charakter = charakter;
}
public override AttackInformation attack()
{
//implementation
}
}
And my two Concrete-Decorator look something like this:
class Armour : CharakterDecorator
{
public int ArmourHealthPoints { get; set; }
public Armour(ICharakter charakter) : base(charakter)
{
ArmourHealthPoints = 20;
}
public override AttackInformation attack()
{
//implementaton
}
}
class Amplifier : CharakterDecorator
{
public int AmplifyValue { get; set; }
public Amplifier(ICharakter charakter) : base(charakter)
{
AmplifyValue = 10;
}
public override AttackInformation attack()
{
//implementaton
}
}
Now I create a Armour-Decorated, Amplifier-Decorated Knight:
ICharakter c = new Armour(new Amplifier(new Knight("John", 15)));
Now, when I call c.HealthPoints, I expect the object to return the given value 15. Instead, 0 is returned. Can anyone explain why, and how i get the programm to return the expected value 15? I am new to this topic and very thankful for every help I can get! If you need further information feel free to ask!
Aucun commentaire:
Enregistrer un commentaire