Delegation mode and policy mode of design mode

1, Delegation mode

In the delegate mode, the delegate mode is similar to the delegate mode. In the delegate mode, the delegate mode can be regarded as a result of the delegate mode.


give an example:

The Boss assigns tasks to the project manager. The project manager will distribute work tasks to each employee according to the actual situation. After the employee completes the work tasks, the project manager will report the work progress and results to the Boss.

 

Create employee IEmployee interface

public interface IEmployee {​    public void doing(String command);​}

Create employee class EmployeeA

public class EmployeeA implements IEmployee {    @Override    public void doing(String command) {        System.out.println("I'm an employee A,I'll start now" + command + "work");    }}

 

Create employee EmployeeB class

public class EmployeeB implements IEmployee {    @Override    public void doing(String command) {        System.out.println("I'm an employee B,I'll start now" + command + "work");    }}

 

Create a project manager's class

public class Leader implements IEmployee {​    private Map<String,IEmployee> targets = new HashMap<String,IEmployee>();​    public Leader() {        targets.put("encryption",new EmployeeA());        targets.put("Sign in",new EmployeeB());    }​    //Public void doing (string command) {targets. Get (command). Doing (command);} ​}

 

Create a Boss class and issue a command

public class Boss {​    public void command(String command,Leader leader){        leader.doing(command);    }​}

 

Test code class

new Boss().command("Sign in",new Leader());

 

The above code can well reflect the scene of the project manager's work assignment, and it is also the embodiment of the delegation mode.

 

2, Strategy mode

Strategy Pattern refers to defining algorithm families and encapsulating them separately so that they can be replaced with each other. This mode makes the change of algorithm not affect the users who use the algorithm.

Application scenario

  • If there are many classes in the system, and their difference only lies in their behavior

  • A system needs to dynamically choose one of several algorithms

Advantages:

 

  • The strategy mode conforms to the opening and closing principle.

  • Avoid using multiple conditional transition statements, such as if else... Statement, switch statement

  • Using policy mode can improve the confidentiality and security of the algorithm

Disadvantages:

 

  • The client must know all the policies and decide which policy class to use.

  • Many policy classes will be generated in the code, which increases the difficulty of maintenance

give an example

Jingdong Mall sometimes has many preferential activities, such as receiving coupon deduction, cash back promotion and group discount

 

Create promotion policy class

public interface PromotionStrategy {    void doPromotion();}

Then create CouponStrategy class of coupon deduction strategy, cashback promotion strategy class, GroupbuyStrategy class of group discount strategy and EmptyStrategy class of no discount strategy respectively:

public class CashbackStrategy implements PromotionStrategy {​    public void doPromotion() {        System.out.println("Cash back promotion,Transfer the returned amount to Alipay account");    }}
public class CouponStrategy implements PromotionStrategy {​    public void doPromotion() {        System.out.println("Receive coupons,The price of the course is deducted directly from the face value of the coupon");    }}
public class EmptyStrategy implements PromotionStrategy {    public void doPromotion() {        System.out.println("No promotions");    }}
public class GroupbuyStrategy implements PromotionStrategy{​    public void doPromotion() {        System.out.println("The whole group can enjoy the group purchase price");    }}

 

Create promotion activity class

public class PromotionActivity {    private PromotionStrategy promotionStrategy;​    public PromotionActivity(PromotionStrategy promotionStrategy) {        this.promotionStrategy = promotionStrategy;    }​    public void execute(){        promotionStrategy.doPromotion();    }​}

Test class

 PromotionActivity activity618 = new PromotionActivity(new CouponStrategy());        PromotionActivity activity1111 = new PromotionActivity(new CashbackStrategy());​        activity618.execute();        activity1111.execute();

There is no problem with the above code verification, but in the actual situation, if there are many activities, we need to change the verification, so we can optimize and refactor in combination with singleton and factory mode

Create factory class

public class PromotionStrategyFactory {    private static Map<String,PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<String, PromotionStrategy>();    static {        PROMOTION_STRATEGY_MAP.put(PromotionKey.COUPON,new CouponStrategy());        PROMOTION_STRATEGY_MAP.put(PromotionKey.CASHBACK,new CashbackStrategy());        PROMOTION_STRATEGY_MAP.put(PromotionKey.GROUPBUY,new GroupbuyStrategy());    }​    private static final PromotionStrategy NON_PROMOTION = new EmptyStrategy();​    private PromotionStrategyFactory(){}​    public static PromotionStrategy getPromotionStrategy(String promotionKey){        PromotionStrategy promotionStrategy = PROMOTION_STRATEGY_MAP.get(promotionKey);        return promotionStrategy == null ? NON_PROMOTION : promotionStrategy;    }​    private interface PromotionKey{        String COUPON = "COUPON";        String CASHBACK = "CASHBACK";        String GROUPBUY = "GROUPBUY";    }

 

The client code can be written like this

String promotionKey = "GROUPBUY";        PromotionActivity promotionActivity = new PromotionActivity(PromotionStrategyFactory.getPromotionStrategy(promotionKey));        promotionActivity.execute();

Which activity is the parameter passed in from the front end? In this way, the original processing logic will not be affected!

Tags: Design Pattern

Posted by Unseeeen on Wed, 11 May 2022 21:44:57 +0300