zoukankan      html  css  js  c++  java
  • 8.6 GOF设计模式四: 策略模式… Strategy Pattern

    策略模式… Strategy Pattern
       在POS系统中,有时需要实行价格优惠, 该如何处理?

         对普通客户或新客户报全价

         对老客户统一折扣5%

         对大客户统一折扣10%
       注:课件来自Head-First OOAD课程资料
    1.1 假设背景

       Joe是一个OO程序员,所在的公司正在开发一款仿真游戏软件 “SimUDuck”,他的任务是完成游戏的重要功能

        Joe works at a company that produces a simulation game called SimUDuck. He is an OO Programmer and his duty is to implement

        the necessary functionality for the game

       游戏具备以下的需求规格说明

        The game should have the following specifications:

         存在多种不同类型的鸭子

          A variety of different ducks should be integrated into the game

         鸭子会游泳 The ducks should swim

         鸭子会叫“quack、quack”The duck should quack
    1.2 初步的设计:简单

       A First Design for the Duck Simulator Game

      

      All ducks quack() and swim(). The superclass takes care of the implementation 父类实施了共同的功能

      The display()method is abstract since all the duck subtypes look different 因为不同的鸭子有不同的外形,子类覆盖
      Each duck subtype is responsible for implementing its own display() behavior for how it looks on the screen
      Lots of other types of ducks inherit from the Duck type
    1.3 需要鸭子飞起来…
      


    1.4 出了一点小问题:橡皮鸭子也飞起来了..


    1.5 继承机制起作用了


    1.6 再增加一只鸭子:诱导鸭

      需要修改的函数总量 = 每增加一个特殊功能*2 * 每增加一类鸭子*2, 好辛苦啊!好容易出哦!
    1.7 设计原则

       软件行业“公理”

         软件项目中,唯一不变的事情就是“变化”

       解决方法

         拥抱变化,让“变化”成为你的设计的一部分

         标识变化点,把它们与系统其余部分隔离开来

           比较:GRASP原则: Protected Variations 隔离变化

       “SimUDucK”游戏的需求总在变化,刚才的方案有点问题!

       解决方法

         把变化的部分,封装起来
    1.8 尝试利用接口的威力
      方案:把fly()从父类Duck中剥离,放 到新定义的一个接口 Flyable, 每个希 望能够飞起来的鸭子就实现这个接口
    1.9 Duck游戏哪些是会变化的?

       Fly() 和 quack() 行为在变化,所以,为每一种行为创建新类
    复习:面向对象设计原则
       1、把变化的部分,封装起来
       2、面向接口设计(编程),而不是面向实现设计(编程) Program to an interface,not to an implementation

  • 相关阅读:
    Delphi CxGrid 用法详解
    Delphi数据库字段
    如何使DevExpress的cxGrid内容只读?
    win10系统没有Windows照片查看器怎么办 win10系统下如何找回Windows照片查看器
    Delphi XE10 dxLayoutControl 控件应用指南
    在 docker 中查看容器日志
    处理 SQL SERVER 数据库的连接查询相关问题
    nginx 禁止 ip 地址访问
    查看 SQL SERVER 的连接情况
    Linux 下同步时间,另附 NTP 服务器地址
  • 原文地址:https://www.cnblogs.com/mayZhou/p/10556325.html
Copyright © 2011-2022 走看看