Created
May 15, 2013 11:43
-
-
Save kashifmunir/5583398 to your computer and use it in GitHub Desktop.
Decorator Pattern Example in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class WindowAbstract | |
| { | |
| /// <summary> | |
| /// a dictionary that will store all method names and their delegate/function pointer information | |
| /// </summary> | |
| protected Dictionary<string, Delegate> callbacks = new Dictionary<string, Delegate>(); | |
| /// <summary> | |
| /// Dynamically execute a function from the Dictionary/Array of callbacks | |
| /// </summary> | |
| /// <param name="methodName"></param> | |
| public void ExecuteMethod(string methodName) | |
| { | |
| if (callbacks.ContainsKey(methodName)) | |
| { | |
| callbacks[methodName].DynamicInvoke(null); | |
| } | |
| else | |
| { | |
| Logger.LogMessage("No such method found"); | |
| } | |
| } | |
| /// <summary> | |
| /// Adds a decorator | |
| /// </summary> | |
| /// <param name="name"></param> | |
| /// <param name="callback"></param> | |
| public void addDecorator(string name, Delegate callback) | |
| { | |
| callbacks.Add(name, callback); | |
| } | |
| /// <summary> | |
| /// Removes a decorator | |
| /// </summary> | |
| /// <param name="name"></param> | |
| public void removeDecorator(string name) | |
| { | |
| if (callbacks.ContainsKey(name)) | |
| { | |
| callbacks.Remove(name); | |
| } | |
| } | |
| } | |
| /// <summary> | |
| /// Interface for decorators | |
| /// </summary> | |
| interface IDecorator | |
| { | |
| void add(WindowAbstract window); | |
| void remove(WindowAbstract window); | |
| } | |
| /// <summary> | |
| /// Adds a maximise methof to object WindowsAbstract | |
| /// </summary> | |
| class MaximiseDecorator : IDecorator | |
| { | |
| /// <summary> | |
| /// Adds a decorator to WindowsAbstract | |
| /// </summary> | |
| /// <param name="window"></param> | |
| public void add(WindowAbstract window) | |
| { | |
| window.addDecorator("maximise", this.max); | |
| } | |
| /// <summary> | |
| /// Removes a decorator from WindowsAbstract | |
| /// </summary> | |
| /// <param name="window"></param> | |
| public void remove(WindowAbstract window) | |
| { | |
| window.removeDecorator("maximise"); | |
| } | |
| public delegate void maximiseDelegate(); | |
| public maximiseDelegate max = maximiseWindow; | |
| public static void maximiseWindow() | |
| { | |
| Logger.LogMessage("About to maximise myself"); | |
| } | |
| } | |
| /// <summary> | |
| /// Implemenation of a Window class | |
| /// </summary> | |
| class Window : WindowAbstract | |
| { | |
| public void show() | |
| { | |
| } | |
| public void hide() | |
| { | |
| } | |
| } | |
| /// <summary> | |
| /// Used to print a message on console | |
| /// </summary> | |
| class Logger | |
| { | |
| public static void LogMessage(string message) | |
| { | |
| Console.WriteLine(message); | |
| } | |
| } | |
| //Tester program | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Logger.LogMessage("Start"); | |
| Window window = new Window(); | |
| Logger.LogMessage("Creating decorator"); | |
| MaximiseDecorator decorator = new MaximiseDecorator(); | |
| decorator.add(window); | |
| Logger.LogMessage("Calling decorator"); | |
| window.ExecuteMethod("maximise"); | |
| Logger.LogMessage("Removing decorator"); | |
| decorator.remove(window); | |
| Logger.LogMessage("Calling maximise with no decorator"); | |
| window.ExecuteMethod("maximise"); | |
| Logger.LogMessage("End"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment