zoukankan      html  css  js  c++  java
  • 《大话设计模式》——第一、二章 简单工厂模式、策略模式

    第一章 简单工厂模式

    工厂:用一个单独的类来创造实例的过程。

    Main
     1 package{
     2     import flash.display.Sprite;
     3     
     4     public class Main extends Sprite{
     5         
     6         public function Main(){
     7             var numA:Number = 100;
     8             var numB:Number = 10;
     9             var operate:String = "/";
    10             var result:Number;
    11             var oper:Operation;
    12             oper = OperationFactoy.createOperate(operate);
    13             oper.numA = numA;
    14             oper.numB = numB;
    15             result = oper.GetResult();
    16             trace(result);
    17         }
    18     }
    19 }
    Operation
     1 package{
     2     public class Operation{
     3         private var _numA:Number = 0;
     4         private var _numB:Number = 0;
     5         
     6         public function get numA():Number{
     7             return _numA;
     8         }
     9         
    10         public function set numA(value:Number):void{
    11             _numA = value;
    12         }
    13         
    14         public function get numB():Number{
    15             return _numB;
    16         }
    17         
    18         public function set numB(value:Number):void{
    19             _numB = value;
    20         }
    21         
    22         public function GetResult():Number{
    23             var result:Number = 0;
    24             return result;
    25         }
    26     }
    27 }
    OperationAdd
     1 package{
     2     public class OperationAdd extends Operation{
     3         
     4         public override function GetResult():Number{
     5             var result:Number = 0;
     6             result = numA + numB;
     7             return result;
     8         }
     9     }
    10 }
    OperationSub
     1 package{
     2     public class OperationSub extends Operation{
     3         
     4         public override function GetResult():Number{
     5             var result:Number = 0;
     6             result = numA - numB;
     7             return result;
     8         }
     9     }
    10 }
    OperationMul
     1 package{
     2     public class OperationMul extends Operation{
     3         
     4         public override function GetResult():Number{
     5             var result:Number = 0;
     6             result = numA * numB;
     7             return result;
     8         }
     9     }
    10 }
    OperationDiv
     1 package{
     2     public class OperationDiv extends Operation{
     3         
     4         public override function GetResult():Number{
     5             var result:Number = 0;
     6             //as3,若除数为0,商为Infinity;
     7             if(numB == 0){
     8 //                throw new Error("除数不能为0!");
     9             }
    10             result = numA / numB;
    11             return result;
    12         }
    13     }
    14 }
    OperationFactoy
     1 package{
     2     public class OperationFactoy{
     3         
     4         public static function createOperate(operate:String):Operation{
     5             var oper:Operation = null;
     6             switch(operate){
     7                 case "+":
     8                     oper = new OperationAdd();
     9                     break;
    10                 case "-":
    11                     oper = new OperationSub();
    12                     break;
    13                 case "*":
    14                     oper = new OperationMul();
    15                     break;
    16                 case "/":
    17                     oper = new OperationDiv();
    18                     break;
    19             }
    20             return oper;
    21         }
    22     }
    23 }

    第二章 策略模式

     策略模式:策略模式是一种定义一系列算法的方法,从概念上来看,所以这些算法完成的都是相同的工作,只是实现不同,它可以以相同方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。

    优点:减低耦合;简化单元测试,可以通过自己的接口单独测试。

    使用:需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性。

    Main
     1 package{
     2     import flash.display.Sprite;
     3     
     4     public class Main extends Sprite{
     5         public function Main(){
     6             var total:Number;
     7             var type:String = "1"
     8             var money:Number = 500;
     9             var cc:CashContext = new CashContext(type);
    10             total = cc.GetResult(money);
    11             trace("total:" + total);
    12         }
    13     }
    14 }
    CashContext
     1 package{
     2     public class CashContext{
     3         private var cs:CashSuper;
     4         public function CashContext(type:String){
     5             switch(type){
     6                 case "0" :
     7                     cs = new CashNormal();
     8                     break;
     9                 case "1":
    10                     cs = new CashRebate(0.9);
    11                     break;
    12                 case "2":
    13                     cs = new CashReturn(400,100);
    14                     break;
    15             }
    16             this.cs = cs;    
    17         }
    18         
    19         public function GetResult(money:Number):Number{
    20             return cs.acceptCash(money);
    21         }
    22     
    23     }
    24 }
    CashSuper
    1 package{
    2     public class CashSuper{
    3         public function acceptCash(money:Number):Number{
    4             return money;            
    5         }
    6     }
    7 }
    CashNormal
    1 package{
    2     public class CashNormal extends CashSuper{
    3         public override function acceptCash(money:Number):Number{
    4             return money;
    5         }
    6     }
    7 }
    CashRebate
     1 package{
     2     public class CashRebate extends CashSuper{
     3         
     4         private var moneyRebate:Number = 0.8;
     5         public function CashRebate(moneyRebate:Number){
     6             this.moneyRebate = moneyRebate;
     7         }
     8         public override function acceptCash(money:Number):Number{
     9             return money*moneyRebate;
    10         }
    11     }
    12 }
    CashReturn
     1 package{
     2     public class CashReturn extends CashSuper{
     3         private var moneyCondition:Number;
     4         private var moneyReturn:Number;
     5         public function CashReturn(moneyCondition:Number, moneyReturn:Number){
     6             this.moneyCondition = moneyCondition;
     7             this.moneyReturn = moneyReturn;
     8         }
     9         
    10         public override function acceptCash(money:Number):Number{
    11             var reulst:Number;
    12             if(money >= moneyCondition){
    13                 reulst = money - Math.floor(money/moneyCondition) * moneyCondition;
    14             }
    15             return reulst;
    16         }
    17     }
    18 }
  • 相关阅读:
    Again Prime? No Time.(uva10870+数论)
    Heaps(Contest2080
    Terminator快捷键
    Chrome快捷键
    STL函数static void (* set_malloc_handler(void (*f)()))()与函数指针解析
    C++中的new,operator new与placement new
    Qt5 中对 C++11 一些新特性的封装
    BFS提高效率的一点建议
    《C++ Primer》读书笔记 第三章
    《C++ Primer》读书笔记 第二章
  • 原文地址:https://www.cnblogs.com/iwhk/p/2727981.html
Copyright © 2011-2022 走看看