public static void Main(string[] args) { // Create some chests: ItemChest swordChest = new ItemChest("Sword Chest"); ItemChest armorChest = new ItemChest("Armor Chest"); ItemChest potionChest = new ItemChest("Potions"); ItemChest otherItems = new ItemChest("Misc."); // Create the chain of responsibility: ChestSorter swords = new ChestSorter(swordChest, ItemType.Sword); ChestSorter armor = new ChestSorter(armorChest, ItemType.Armor); ChestSorter potions = new ChestSorter(potionChest, ItemType.Potion); // Null sorter for item's that don't have an explicit chest: ChestSorter other = new NullSorter(otherItems); // Link the chains: swords.SetNext(armor); armor.SetNext(potions); potions.SetNext(other); // Pointer to the head of the list: // Implementation note: You can create another class to maintain all the sorting items! ChestSorter sortingMachine = swords; // Insert a few items into the sorting machine: sortingMachine.Handle(new EquipmentItem("Mighty Sword", ItemType.Sword)); sortingMachine.Handle(new EquipmentItem("Potion of life", ItemType.Potion)); sortingMachine.Handle(new EquipmentItem("Blade of infinity", ItemType.Sword)); sortingMachine.Handle(new EquipmentItem("Hockey Pads", ItemType.Armor)); sortingMachine.Handle(new EquipmentItem("Scroll of a thousand truths", ItemType.QuestItem)); sortingMachine.Handle(new EquipmentItem("Isildur's Bane", ItemType.Ring)); // Display all the chests' contents: sortingMachine.PrintChain(); /* * Output: * Items in Sword Chest: * Mighty Sword * Blade of infinity * Items in Armor Chest: * Hockey Pads * Items in Potions: * Potion of life * Items in Misc.: * Scroll of a thousand truths * Isildur's Bane */ }
public void SetNext(ChestSorter sorter) { this.next = sorter; }