Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#

Design Patterns - nuff said !

Rate me:
Please Sign up or sign in to vote.
4.85/5 (18 votes)
2 Sep 2010CPOL16 min read 45.2K   633   74  
Principles of Design Patterns to answer why they exist, who should actually use them and their learning roadmap for beginners (using C#)
using System.Collections.Generic;
namespace Design_Pattern_Command2_1
{
    //Command hooked class - the invoker class
    class CommandProcessor
    {
        List<Command> allHookedCommandsList = new List<Command>();
        Stack<UndoableCommand> undoCommandsStack = new Stack<UndoableCommand>();
        
        private Command newCmd;
        public Command NewCmd
        {
            get { return newCmd; }
            set 
            { 
                newCmd = value;
                if (newCmd != null)
                {
                    allHookedCommandsList.Add(newCmd);
                }
            }
        }

        private Command openCmd;
        public Command OpenCmd
        {
            get { return openCmd; }
            set
            {
                openCmd = value;
                if (openCmd != null)
                    allHookedCommandsList.Add(openCmd);
            }
        }

        private Command saveCmd;
        public Command SaveCmd
        {
            get { return saveCmd; }
            set
            {
                saveCmd = value;
                if (saveCmd != null)
                    allHookedCommandsList.Add(saveCmd);
            }
        }

        private Command printCmd;
        public Command PrintCmd
        {
            get { return printCmd; }
            set
            {
                printCmd = value;
                if (printCmd != null)
                    allHookedCommandsList.Add(printCmd);
            }
        }       
        
        public void RunCommands()
        {
            foreach (Command hookedCommand in allHookedCommandsList)
            {
                if (hookedCommand is UndoableCommand)
                    undoCommandsStack.Push((UndoableCommand)hookedCommand);
                hookedCommand.Execute();
            }            
        }

        public void Undo()
        {
            UndoableCommand lastUndoableCmd;
            if (undoCommandsStack.Count > 0)
            {
                lastUndoableCmd = undoCommandsStack.Pop();
                lastUndoableCmd.UnExecute();
                allHookedCommandsList.Remove(lastUndoableCmd);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Working as Senior Developer in US based MNC. Interested in R&D, writing/reading techinical articles, sci-fi movies, playing synthesizer.

Comments and Discussions