Design Patterns - Strategy Pattern and factory pattern combination

  • How the strategy pattern and factory pattern in combination

If everyone on the strategy pattern and factory pattern is not very understanding, then you can look at the previous articles
the strategy pattern: https://www.jianshu.com/p/958281936901
factory pattern: https://www.jianshu.com/p/9078481e00c6

They may have used micro-channel pay, pay for the use of micro-channel pay when:

1, when our payment amount is greater than our balance, we will use the bank card payment,
2, plenty of time to balance our priorities will balance the money inside
debit Strategy One:
Balance (blance)> = payment amount ( tradeAmout) balance
debit strategy II:
balance (blance) <payment amount (tradeAmout) using a bank card
obviously this is the practical application of a policy mode, but you remember the pattern of defect policies do? It must be exposed to specific strategies, but also initialized by the upper module, which does not fit, does not match with the Law of Demeter (Demeter (Law of Demeter) also called the principle of least knowledge (Least Knowledge Principle abbreviation LKP), that there should be an object to other objects as little as possible to understand, do not talk to strangers Note: Excerpt from Baidu Encyclopedia, the law of Demeter next time you go into detail) conflict. High-level module level modules in contact with the upper level only, and should not be coupled relationship. The problem is, we should try to solve, just the factory pattern can help us solve this problem. But the introduction of the factory model also has a problem, the factory method to specify a class, it can produce objects, we enumerate to complete.
First we built two entity classes WxBlance and WxTrade

import java.math.BigDecimal;

/**
 * @author shuliangzhao * @Title: WxBlance * @ProjectName design-parent * @Description: TODO * @date 2019/5/28 23:50 */ public class WxBlance { //余额 private BigDecimal blance; public BigDecimal getBlance() { return blance; } public void setBlance(BigDecimal blance) { this.blance = blance; } } 
import java.math.BigDecimal;

/**
 * @author shuliangzhao * @Title: Trade * @ProjectName design-parent * @Description: TODO * @date 2019/5/28 23:51 */ public class WxTrade { private BigDecimal tradeAmout; private String tradeNo; private BigDecimal userAmout; public BigDecimal getUserAmout() { return userAmout; } public void setUserAmout(BigDecimal userAmout) { this.userAmout = userAmout; } public String getTradeNo() { return tradeNo; } public void setTradeNo(String tradeNo) { this.tradeNo = tradeNo; } public BigDecimal getTradeAmout() { return tradeAmout; } public void setTradeAmout(BigDecimal tradeAmout) { this.tradeAmout = tradeAmout; } } 

Debit strategy interfaces

/**
 * @author shuliangzhao * @Title: Deduction * @ProjectName design-parent * @Description: TODO * @date 2019/5/28 23:53 */ public interface Deduction { public boolean exec(WxBlance wxBlance,WxTrade wxTrade); } 

A debit strategy

/**
 * @author shuliangzhao * @Title: BlanceDeduction * @ProjectName design-parent * @Description: TODO * @date 2019/5/28 23:54 */ public class BlanceDeduction implements Deduction { @Override public boolean exec(WxBlance wxBlance, WxTrade wxTrade) { if (wxBlance.getBlance().compareTo(wxTrade.getTradeAmout()) >= 0) { wxTrade.setUserAmout(wxBlance.getBlance()); } return true; } } 

Debit Strategy II

/**
 * @author shuliangzhao * @Title: IdCardDeduction * @ProjectName design-parent * @Description: TODO * @date 2019/5/28 23:54 */ public class IdCardDeduction implements Deduction { @Override public boolean exec(WxBlance wxBlance, WxTrade wxTrade) { if (wxBlance.getBlance().compareTo(wxTrade.getTradeAmout()) < 0) { wxTrade.setUserAmout(wxTrade.getTradeAmout()); } return true; } } 

Chargeback policy package

/**
 * @author shuliangzhao * @Title: DedutionContext * @ProjectName design-parent * @Description: TODO * @date 2019/5/28 23:58 */ public class DedutionContext { private Deduction deduction; public DedutionContext(Deduction deduction) { this.deduction = deduction; } public boolean exec(WxBlance wxBlance,WxTrade wxTrade) { return deduction.exec(wxBlance,wxTrade); } } 

A typical policy context. Defect strategy pattern of all policy class are exposed to, how to modify it? Policy object using the factory pattern generated according to the mapping

Enumeration strategy

/**
 * @author shuliangzhao * @Title: StrategyEnum * @ProjectName design-parent * @Description: TODO * @date 2019/5/29 0:00 */ public enum StrategyEnum { BlanceDeduction("com.sl.factorystrategy.BlanceDeduction"), IdCardDeduction("com.sl.factorystrategy.IdCardDeduction"); String value = ""; private StrategyEnum(String value) { this.value = value; } public String getValue() { return value; } } 

Strategy factory

/**
 * @author shuliangzhao * @Title: StrategyFactory * @ProjectName design-parent * @Description: TODO * @date 2019/5/29 0:03 */ public class StrategyFactory { public static Deduction getDeduction(StrategyEnum strategyEnum) { Deduction deduction = null; try { deduction = (Deduction)Class.forName(strategyEnum.getValue()).newInstance(); } catch (Exception e) { e.printStackTrace(); } return deduction; } } 

Debit call the class

/**
 * @author shuliangzhao * @Title: DeductionFacade * @ProjectName design-parent * @Description: TODO * @date 2019/5/29 0:06 */ public class DeductionFacade { //扣款 public static void deduct(WxBlance wxBlance,WxTrade wxTrade) { StrategyEnum strate = getStrate(wxBlance, wxTrade); Deduction deduction = StrategyFactory.getDeduction(strate); deduction.exec(wxBlance,wxTrade); } //获取扣款策略 private static StrategyEnum getStrate(WxBlance wxBlance,WxTrade wxTrade) { if (wxBlance.getBlance().compareTo(wxTrade.getTradeAmout()) < 0) { return StrategyEnum.IdCardDeduction; }else { return StrategyEnum.BlanceDeduction; } } } 

Call the client client

/**
 * @author shuliangzhao * @Title: Client * @ProjectName design-parent * @Description: TODO * @date 2019/5/29 0:10 */ public class Client { public static void main(String[] args) { WxTrade wxTrade = new WxTrade(); wxTrade.setTradeAmout(new BigDecimal("1000")); WxBlance wxBlance = new WxBlance(); wxBlance.setBlance(new BigDecimal("999")); DeductionFacade.deduct(wxBlance,wxTrade); System.out.println(wxTrade.getUserAmout()); } } 

Results of the

 

 

Summary:
Strategy Mode: Responsible for debit package, to ensure that both policies are free to switch, is also very easy to add a policy in the future

Factory Mode: Strategy Mode must be corrected the problem of external exposure, generate a specific policy object factory mode

Guess you like

Origin www.cnblogs.com/treeshu/p/10959615.html