1日1つの設計モデルの1つ---ポリシーモデル


一.ポリシー・モード定義
  • ポリシーモードは、3つのタイプの設計モードの動作モード(他の2つは作成モードと構造モード)に属する
  • である.
  • ポリシー・モードは、アルゴリズム・ファミリーを定義し、それぞれカプセル化して、アルゴリズムの変化がアルゴリズムを使用する顧客に影響を与えないようにします.
  • このモードでは、一般に、ビジネスを実装するための抽象的なメソッド(計算結果)を定義する親クラス(アルゴリズムクラス)を定義し、Nサブクラスを定義してメソッドを実装し、コンテキストクラス(context)を介して親クラスのメンバーを宣言し、上下文を作成する際に、入力stringに基づいて生成された子クラスによって親クラスメンバーに付与する必要があります.コンテキストクラスにメソッドを宣言し、メソッドでビジネスメソッドを呼び出せばよい.
  • 適用シーン:1.ファイルを異なるフォーマットで保存します.2.異なるアルゴリズムでファイルを圧縮します.3.異なるアルゴリズムで画像をキャプチャする.4.曲線やブロック図など、同じデータを異なる形式で出力する図形
  • .
  • 欠点:以下のコードがクライアントに暴露されたのはContext,celve,celveのサブクラスであり、結合性が高い.ポリシーを1つ追加するたびに、サブクラスを1つ多く書きます.
  • の利点:開放-閉鎖の原則、リス置換の原則と単一の職責の原則を完璧に支持する.

  • 二.テストコード
    package com.me.celvemoshi.test;
    
    /**
     *          new    Celve              ,             Celve,        Celve   ,
     *          
     * 
     * @author liujun E-mail:
     * @version     :2016 2 21    5:22:23
     */
    public class CelveTest {
        public static void main(String[] args) {
            //      
            Context context;
            context = new Context(new DazheCelve(0.8));
            System.out.println(context.getResult(110d));
            context = new Context(new FanliCelve(300d, 100d));
            System.out.println(context.getResult(400d));
            context = new Context(new FanliCelve(300d, 100d));
    
        }
    }
    
    class Context {
        Celve celve;
    
        public Context(Celve celve) {
            this.celve = celve;
        }
    
        public double getResult(Double money) {
            return celve.getResult(money);
        }
    }
    
    abstract class Celve {
        public abstract double getResult(Double money);
    }
    
    /**
     *   
     * 
     * @author 58
     *
     */
    class DazheCelve extends Celve {
        private double rate;
    
        public DazheCelve(double rate) {
            this.rate = rate;
        }
    
        public double getRate() {
            return rate;
        }
    
        public void setRate(double rate) {
            this.rate = rate;
        }
    
        @Override
        public double getResult(Double money) {
            return money * rate;
        }
    
    }
    
    /**
     *      
     * 
     * @author
     */
    class DazheAndFanli extends Celve {
        private double rate;
        private double moneyLimit;
        private double moneyReturn;
    
        public DazheAndFanli(double rate, double moneyLimit, double moneyReturn) {
            this.rate = rate;
            this.moneyLimit = moneyLimit;
            this.moneyReturn = moneyReturn;
        }
    
        @Override
        public double getResult(Double money) {
            double tempResult = money * rate;
            if (tempResult >= moneyLimit) {
                tempResult -= moneyReturn;
            }
            return tempResult;
        }
    
        public double getRate() {
            return rate;
        }
    
        public void setRate(double rate) {
            this.rate = rate;
        }
    
        public double getMoneyLimit() {
            return moneyLimit;
        }
    
        public void setMoneyLimit(double moneyLimit) {
            this.moneyLimit = moneyLimit;
        }
    
        public double getMoneyReturn() {
            return moneyReturn;
        }
    
        public void setMoneyReturn(double moneyReturn) {
            this.moneyReturn = moneyReturn;
        }
    
    }
    
    /**
     *   
     * 
     * @author 58
     *
     */
    class FanliCelve extends Celve {
        private double moneyLimit;
        private double moneyReturn;
    
        public FanliCelve(double moneyLimit, double moneyReturn) {
            this.moneyLimit = moneyLimit;
            this.moneyReturn = moneyReturn;
        }
    
        public double getMoneyLimit() {
            return moneyLimit;
        }
    
        public void setMoneyLimit(double moneyLimit) {
            this.moneyLimit = moneyLimit;
        }
    
        public double getMoneyReturn() {
            return moneyReturn;
        }
    
        public void setMoneyReturn(double moneyReturn) {
            this.moneyReturn = moneyReturn;
        }
    
        @Override
        public double getResult(Double money) {
            if (money > moneyLimit) {
                money = money - moneyReturn;
            }
            return money;
        }
    
    }