Command 设计模式

 public interface ICommand
    {
        void Show();
        void Undo();
        void Redo();
    }
 public class Document
    {        
        public void ShowText()
        {
        }
    }
 public class Graphics
    {
        public void ShowGraphics()
        {
        }
    }
 public class DocumentCommand:ICommand
    {
        Document document;

        public DocumentCommand(Document document)
        {
            this.document = document;
        }

        public void Show()
        {
            document.ShowText();
        }

        public void Undo()
        {
        }

        public void Redo()
        {
        }
    }
 public class GraphicsCommand:ICommand
    {
        Graphics graphics;
        
        public GraphicsCommand(Graphics graphics)
        {
            this.graphics = graphics;
        }

        public void Show()
        {
            graphics.ShowGraphics();
        }

        public void Undo()
        {
        }

        public void Redo()
        {
        }
    }
 Stack<ICommand> stack;
        IList<ICommand> undoList;
        IList<ICommand> redoList;

        public Application( Stack<ICommand> stack )
        {
            this.stack = stack;
        }

        public void Show()
        {
            foreach( ICommand c in stack )
            {
                c.Show();
            }
        }

        public void Redo()
        {
            ICommand command = stack.Pop();
            if( undoList.Count > 0 )
            {
                command.Redo();
                redoList.Add( command );
            }
        }

        public void Undo()
        {
            if( redoList.Count > 0 )
            {
                ICommand command = stack.Pop();
                command.Undo();

                undoList.Add( command );
            }
        }

依赖抽象而非具体,解除耦合,依赖倒置

原文地址:https://www.cnblogs.com/wangchuang/p/3010818.html