Advertisement
Slash18

Decorator

Jun 30th, 2016
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Decorator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             IItem newItem = new Sword();
  10.             Console.WriteLine(newItem.GetName() + " Value: " + newItem.GetValue().ToString());
  11.  
  12.             newItem = new Magic(newItem);
  13.             Console.WriteLine(newItem.GetName() + " Value: " + newItem.GetValue().ToString());
  14.  
  15.             Console.ReadLine();
  16.         }
  17.     }
  18.  
  19.     public interface IItem
  20.     {
  21.        string GetName();
  22.        int GetValue();
  23.     }
  24.     class Sword : IItem
  25.     {
  26.         public string GetName()
  27.         {
  28.             return "Iron Sword";
  29.         }
  30.  
  31.         public int GetValue()
  32.         {
  33.             return 20;
  34.         }
  35.     }
  36.  
  37.    class Armor : IItem
  38.     {
  39.         public string GetName()
  40.         {
  41.             return "Iron Armor";
  42.         }
  43.  
  44.         public int GetValue()
  45.         {
  46.             return 50;
  47.         }
  48.     }
  49.  
  50.     abstract class Enchantment : IItem
  51.     {
  52.         IItem _item = null;
  53.  
  54.         protected int _Value = 0;
  55.  
  56.         protected Enchantment(IItem baseItem)
  57.         {
  58.             _item = baseItem;
  59.         }
  60.  
  61.         public string GetName()
  62.         {
  63.             return (_item.GetName() +" +1 ");
  64.         }
  65.  
  66.        public int GetValue()
  67.         {
  68.             return (_item.GetValue() + _Value);
  69.         }
  70.     }
  71.      class Magic : Enchantment
  72.     {
  73.         public Magic(IItem baseComponent): base(baseComponent)
  74.         {
  75.             this._Value = 30;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement