SHARE
TWEET

Composite

Slash18 Jun 25th, 2016 (edited) 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. //    Full Tutorial on indiedevart.wordpress.com
  4.  
  5.  
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12.  
  13. namespace ConsoleApplication108
  14. {
  15.     public interface IInventory
  16.     {
  17.         string Name { get; set; }
  18.         void PrintName();
  19.     }
  20.  
  21.     public class Backpack : IInventory, IEnumerable<IInventory>
  22.     {
  23.         private List<IInventory> _subordinates = new List<IInventory>();
  24.  
  25.         public string Name { get; set; }
  26.  
  27.         public void AddSubordinate(IInventory subordinate)
  28.         {
  29.             _subordinates.Add(subordinate);
  30.         }
  31.  
  32.         public void RemoveSubordinate(IInventory subordinate)
  33.         {
  34.             _subordinates.Remove(subordinate);
  35.         }
  36.  
  37.         public IInventory GetSubordinate(int index)
  38.         {
  39.             return _subordinates[index];
  40.         }
  41.  
  42.         public void PrintName()
  43.         {
  44.             Console.WriteLine(Name);
  45.         }
  46.         public IEnumerator<IInventory> GetEnumerator()
  47.         {
  48.             foreach (IInventory subordinate in _subordinates)
  49.             {
  50.                 yield return subordinate;
  51.             }
  52.         }
  53.  
  54.         IEnumerator IEnumerable.GetEnumerator()
  55.         {
  56.             return GetEnumerator();
  57.         }
  58.     }
  59.  
  60.     public class Item : IInventory
  61.     {
  62.         public string Name { get; set; }
  63.  
  64.         public void PrintName()
  65.         {
  66.             Console.WriteLine(Name);
  67.         }
  68.     }
  69.     class Program
  70.     {
  71.         static void Main(string[] args)
  72.         {
  73.             Backpack inventory= new Backpack{  Name = "Inventory" };
  74.             Backpack backpack = new Backpack { Name = "Backpack" };
  75.  
  76.             inventory.AddSubordinate(backpack);
  77.             inventory.AddSubordinate(new Item { Name = "Sword" });
  78.             inventory.AddSubordinate(new Item { Name = "Armor" });
  79.  
  80.             backpack.AddSubordinate(new Item { Name = "Shield" });
  81.  
  82.             Console.WriteLine("List of items in inventory");
  83.             foreach (IInventory element in inventory)
  84.             {
  85.                 element.PrintName();
  86.             }
  87.             Console.ReadKey();
  88.         }
  89.     }
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top