zoukankan      html  css  js  c++  java
  • 开闭原则, 对扩展开放、对修改关闭

    开闭原则, 对扩展开放、对修改关闭

    新需求的实现是通过新增加代码来完成的,不是通过修改现有代码完成的 例如要打折了,原有的代码不修改,曾加一个派生类

    只对应用程序中频繁变化的部分进行抽象 比如Book这个抽象类中的

    class Book(object):
        def __init__(self, name, price, author):
            self.name = name
            self.price = price
            self.author = author
    
        def get_name(self):
            pass
    
        def get_price(self):
            pass
    
        def get_author(self):
            pass
    
        def get_book_info(self):
            pass
    
    class NovelBook(Book):
        def __init__(self, name, price, author):
            super(NovelBook, self).__init__(name, price, author)
    
        def get_name(self):
            return self.name
    
        def get_price(self):
            return self.price
    
        def get_author(self):
            return self.author
    
        def get_book_info(self):
            return "Book name: " + self.get_name() + "  Book author: " + self.get_author() + "  Book price: " + str(self.get_price()/100.0) + "元"
    
    class OffNovelBook(NovelBook): ### 新增子类用于扩展 
        def __init__(self, name, price, author):
            super(OffNovelBook, self).__init__(name, price, author)
    
        # def get_name(self):
        #     return self.name
    
        def get_price(self):
            origin_price = super(OffNovelBook, self).get_price()
            off_price = 0
            if origin_price >= 5000:
                off_price = origin_price * 0.8
            else:
                off_price = origin_price * 0.9
    
            return off_price
    
        # def get_author(self):
        #     return self.author
    
        def get_book_info(self):
            off_book_info = super(OffNovelBook, self).get_book_info()
    
            return off_book_info
    
    # 此处初始化区属于高层次模块因为业务需求变更也需要相应修改,但修改越少越好
    class BookStore(object):
        def __init__(self):
            self.book_list = []
            self.book_list.append(OffNovelBook("西游记", 3000, "吴承恩"))         
            self.book_list.append(OffNovelBook("三国演义", 6000, "罗贯中"))
            self.book_list.append(OffNovelBook("红楼梦", 8000, "曹雪芹"))
    
        def sell(self, book):
            print ("Sell Info: {}".format(book.get_book_info()))
    
            
    if __name__ == '__main__':
        book_store = BookStore()
        for book in book_store.book_list:
            book_store.sell(book)
    

    执行结果

    Sell Info: Book name: 西游记 Book author: 吴承恩 Book price: 27.0元

    Sell Info: Book name: 三国演义 Book author: 罗贯中 Book price: 48.0元

    Sell Info: Book name: 红楼梦 Book author: 曹雪芹 Book price: 64.0元

  • 相关阅读:
    IntelliJ IDEA 使用总结[zz]
    “vmware tools 只能虚拟机中安装”的解决方法
    Git 少用 Pull 多用 Fetch 和 Merge
    hypervisor与VMware共存方法
    rsa && sha1 js code
    PHP导出数据库方法
    Git中的AutoCRLF与SafeCRLF换行符问题
    DOTA2参数收集
    HelloGitHub.com 网站开源了
    第 10 篇 评论接口
  • 原文地址:https://www.cnblogs.com/ShawSpring/p/10646499.html
Copyright © 2011-2022 走看看