1
Reply

Code error in Decorator design pattern..

kobi kobi

kobi kobi

May 20 2017 7:41 PM
356
Hello everyone,
 
It's the first time I publish something in the forum, so I apologize if the code is not presented in the appropriate font for a code.. :/
 
I tried to implement the decorator design pattern, but for some reason something goes wrong during the process and the compiler throws NullReferenceException for a reason I can't figure out.
I'd be grateful for your help.
 
I have this as my basic item:
  1. public class Hambruger  
  2.    {  
  3.    public virtual int PattySizeInGrams { getset; }  
  4.    public virtual string Bun { getset; }   
  5.    public Hambruger()  
  6.       {  
  7.          PattySizeInGrams = 200;  
  8.          Bun = "Ordinary bun";  
  9.       }  
  10.    }

  11. public virtual void GetDescription()
    {
       Console.WriteLine("Hamburger:\n {0}, {1} grams", Bun,PattySizeInGrams );
    }


this is the abstract decorator class:
 
  1. public abstract class HamburgerDecorator : Hambruger  
  2.    {  
  3.        protected Hambruger hamburgerToBeDecorated;  
  4.   
  5.        public HamburgerDecorator(Hambruger theHamburger)   
  6.        {  
  7.            hamburgerToBeDecorated = theHamburger;  
  8.        }  
  9.       public override string Bun  
  10.        {  
  11.            get  
  12.            {  
  13.                return hamburgerToBeDecorated.Bun;  
  14.            }  
  15.   
  16.            set  
  17.            {  
  18.                hamburgerToBeDecorated.Bun = value;  
  19.            }  
  20.        }  
  21.        public override int PattySizeInGrams  
  22.        {  
  23.            get  
  24.            {  
  25.                return hamburgerToBeDecorated.PattySizeInGrams;  
  26.            }  
  27.   
  28.            set  
  29.            {  
  30.                hamburgerToBeDecorated.PattySizeInGrams = value;  
  31.            }  
  32.        } 
  33.    }

This is the concrete decorator class:

  1. public class ExtraMeatDecorator : HamburgerDecorator  
  2.     {  
  3.         public ExtraMeatDecorator(Hambruger hamburgerToDecorate) : base(hamburgerToDecorate) { }  
  4.         public override void AddExtraMeat()  
  5.         {  
  6.             hamburgerToBeDecorated.PattySizeInGrams += 100;  
  7.         }  

 And this is my Main:
 
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Hambruger ham = new Hambruger();  
  6.             Hambruger decorated = new ExtraMeatDecorator(ham);  
  7.         }  
  8.     } 
 
 The exception happens when "new ExtraMeatDecorator(ham) is called. Before it calls the ExtraMeatDecorator constructor, it has to call its base constructor - HamburgerDecorator, which in turn, has to call first to the Hamburger constructor. The PattySizeInGrams
and Bun properties are null and thus, cannot make the assignment in the Hamburger constructor.
 
I just can't seem to figure out the reason. I've made a simpler example in which this problem didn't happen:
  1. public class Parent  
  2.     {  
  3.         public  int Age { getset; }  
  4.         public Parent()  
  5.         {  
  6.             Age = 25;  
  7.         }  
  8.     }  
  9.   
  10.     public class Child : Parent  
  11.     {  
  12.         public Child(Parent par) : base() { }  
  13.     } 
 

Answers (1)