Design Patterns Study Notes ---- + simple factory pattern strategy pattern

background

Shopping malls cash register system receives a renewal, the customer says there needs to be a cash register software, to calculate how much money should be collected according to the customer purchased the unit price and quantity. After doing a version of the customer request for additional discount function, and also reduce the need to support full functionality.
At first I relate to this idea is to create a common interface according to type determines what kind of payment mode currently in use, and then select the corresponding function to calculate the required cash received.

Simple factory pattern

Because before learning the simple factory pattern, that such a scenario is relatively simple factory pattern applies, so have the following code; create a parent class fee.

public abstract class CashSuper {
    private int nums;
    private double price;
    protected abstract double acceptCash(double money);
}

Then define normal payment mode, receivable discounts Save mode and full mode three classes

class NormalCash extends CashSuper {

    @Override
    protected double acceptCash(double money) {
        return money;
    }
}

class RebateCash extends CashSuper {

    private double rebate;

    public RebateCash(double rebate) {
        this.rebate = rebate;
    }

    @Override
    protected double acceptCash(double money) {
        return money * rebate;
    }
}

class ReturnCash extends CashSuper {

    private double moneyCondition = 0.0;
    private double moneyReturn = 0.0;

    public ReturnCash(double moneyCondition, double moneyReturn) {
        this.moneyCondition = moneyCondition;
        this.moneyReturn = moneyReturn;
    }

    @Override
    protected double acceptCash(double money) {
        return money - Math.floor(money / moneyCondition) * moneyReturn;
    }
}

Then create a collection facility on it

class CashFactory {
    public static CashSuper createAcceptCasg(int type) {
        CashSuper cashSuper;
        switch (type) {
            case 0:
                cashSuper = new NormalCash();
                break;
            case 1:
                cashSuper = new RebateCash(0.7);        // 打七折
                break;
            case 2:
                cashSuper = new ReturnCash(100, 50);        // 满100减50
                break;
            default:
                cashSuper = new NormalCash();
        }
        return cashSuper;
    }

}

In this mode, although it can address this demand, but this mode is only to solve the problems created object, and because the plant itself also includes all charging methods, the mall is likely to be frequent changes to the amount of the discount and rebate scale, each maintenance need to change or expand the factory, so that the code needs to be recompiled to deploy, it really is a very bad approach, so it's not the best way.

Strategy Mode

The face of frequent changes in algorithms, you should use the strategy pattern.
** Strategy pattern defines a family of algorithms, encapsulate them, so that they can replace each other, this mode allows changes in the algorithm u when the incident will not affect customers **
code is as follows: the client only needs to call this code to complete, but needs the client to determine which algorithm to use

class CashContexts {
    private CashSuper cashSuper;

    public CashContexts(CashSuper cashSuper) {
        this.cashSuper = cashSuper;
    }

    public double getReesult(double money) {
        return cashSuper.acceptCash(money);
    }
}

Client:

public static void main(String[] args) {
        CashContexts cashContexts;
        double money = 100;
        int type = 0;
        switch (type) {
            case 0:
                cashContexts = new CashContexts(new NormalCash());
                break;
            case 1:
                cashContexts = new CashContexts(new RebateCash(0.7));
                break;
            case 2:
                cashContexts = new CashContexts(new ReturnCash(100,50));
                break;
            default:
                cashContexts = new CashContexts(new NormalCash());;
        }

        double reesult = cashContexts.getReesult(money);
        System.out.println(reesult);
    }

Strategy pattern combined with a simple factory pattern

class CashContextt {
    private CashSuper cashSuper;

    public CashContextt(Integer type) {
        switch (type) {
            case 0:
                cashSuper = new NormalCash();
                break;
            case 1:
                cashSuper = new RebateCash(0.7);
                break;
            case 2:
                cashSuper = new ReturnCash(100,50);
                break;
            default:
                cashSuper = new NormalCash();
        }
    }
    
    public double getResult(double money) {
        return cashSuper.acceptCash(money);
    }
}

Think back, in fact Strategy Mode is a way to define a series of algorithms, from the conceptual point of view, these are the same methods to complete the work, but do not achieve the same amount it can use the same method calls all the algorithms, algorithm to reduce the coupling between the various classes and utility classes

Guess you like

Origin www.cnblogs.com/joimages/p/11977652.html