zoukankan      html  css  js  c++  java
  • 设计模式-行为型模式,命令模式(11)

    命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。

    现在多数应用都有撤销操作。虽然难以想象,但在很多年里,任何软件中确实都不存在撤销
    操作。撤销操作是在1974年引入的(请参考网页[t.cn/Rqr3N22]),但Fortran和Lisp分别早在1957
    年和1958年就已创建了撤销操作(请参考网页[t.cn/Rqr3067]),这两门语言仍在被人广泛使用。
    在那些年里,我真心不想使用应用软件。犯了一个错误,用户也没什么便捷方式能修正它。

    import os
    
    verbose = True
    
    
    class RenameFile:
    
        def __init__(self, path_src, path_dest):
            self.src, self.dest = path_src, path_dest
    
        def execute(self):
            if verbose:
                print("[renaming '{}' to '{}']".format(self.src, self.dest))
            os.rename(self.src, self.dest)
    
        def undo(self):
            if verbose:
                print("[renaming '{}' back to '{}']".format(self.dest, self.src))
            os.rename(self.dest, self.src)
    
    
    class CreateFile:
    
        def __init__(self, path, txt='hello world
    '):
            self.path, self.txt = path, txt
    
        def execute(self):
            if verbose:
                print("[creating file '{}']".format(self.path))
            with open(self.path, mode='w', encoding='utf-8') as out_file:
                out_file.write(self.txt)
    
        def undo(self):
            delete_file(self.path)
    
    
    class ReadFile:
    
        def __init__(self, path):
            self.path = path
    
        def execute(self):
            if verbose:
                print("[reading file '{}']".format(self.path))
            with open(self.path, mode='r', encoding='utf-8') as in_file:
                print(in_file.read(), end='')
    
    
    def delete_file(path):
        if verbose:
            print("deleting file '{}'".format(path))
        os.remove(path)
    
    
    def main():
        orig_name, new_name = 'file1', 'file2'
    
        commands = []
        for cmd in CreateFile(orig_name), ReadFile(orig_name), RenameFile(orig_name, new_name):
            commands.append(cmd)
    
        [c.execute() for c in commands]
    
        answer = input('reverse the executed commands? [y/n] ')
    
        if answer not in 'yY':
            print("the result is {}".format(new_name))
            exit()
    
        for c in reversed(commands):
            try:
                c.undo()
            except AttributeError as e:
                pass
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    SpringCloud+MyBatis+Redis整合—— 超详细实例(一)
    SpringCloud开发学习总结(五)—— 服务容错保护Hystrix
    SpringCloud开发学习总结(三)—— 服务治理Eureka
    SpringCloud开发学习总结(二)—— 简单的微服务构建
    SpringCloud开发学习总结(一)—— 基础知识
    Activiti入门
    kafka入门(3)- SpringBoot集成Kafka
    kafka入门(2)- 环境部署
    kafka入门(1)- 基本概念
    微服务深入浅出(11)-- SpringBoot整合Docker
  • 原文地址:https://www.cnblogs.com/ydf0509/p/8526012.html
Copyright © 2011-2022 走看看