I. Strategic mode

Definition of policy pattern:

The policy pattern encapsulates the algorithm, separating the responsibility of using the algorithm from the algorithm itself and delegating it to different object management. Policy patterns typically wrap a set of algorithms into a set of policy classes as a subclass of an abstract policy class.

    

Two, what problem did I solve?

In a real project, there are many different ways to complete a task. Else if… The else… As shown in the code below, each platform has its own HuiTieTicketNumber

if (vendeeId= =1) {/ / ctrip
        TicketContext ticketContextCtrip=CtripTicketContext(reportDetail,orderDetail);
        if (ticketContextCtrip! =null){
            huitiePiaoHao=new CtripHuiTieTicketNumber();
            huitiePiaoHao.handle(ticketContextCtrip); }}else if (vendeeId= =2) {/ / go
        TicketContext ticketContextQunar=QunarTicketContext(reportDetail,orderDetail);
        if (ticketContextQunar! =null){
            huitiePiaoHao=new QunarHuiTieTicketNumber();
            huitiePiaoHao.handle(ticketContextQunar); }}else if (vendeeId= =3) {/ / ali.
        TicketContext ticketContextQua=QuaTicketContext(reportDetail,orderDetail);
        if (ticketContextQua! =null){
            huitiePiaoHao=new QuaHuiTieTicketNumber();
            huitiePiaoHao.handle(ticketContextQua); }}else if (vendeeId= =4) {/ / the same routine
        TicketContext ticketContextTongCheng=QunarTicketContext(reportDetail,orderDetail);
        if (ticketContextTongCheng! =null){
            huitiePiaoHao=new TongChengHuiTieTicketNumber();
            TicketContext ticketContext=huitiePiaoHao.handle(ticketContextTongCheng);
            if (ticketContext.getStatus()= =2){}}}}Copy the code

Of course, it is possible to do this just to complete the function, but it is also limited. If you add a new platform, you need to change the code again, which violates the principle of closed for internal modification and open for external extension. So this is where the policy pattern is applied. HuiTie of each platform is encapsulated as a strategy into an independent class for processing.

    

Three, code implementation

Policy factory classes, whose concrete policy classes are injected in Spring.

/** * Platform strategy pattern factory class * Created by Ling. Zhang on 2017/1/21. */
public class OrderIntegrateReadFactory {
    private final static Logger logger= LoggerFactory.getLogger(OrderIntegrateReadFactory.class);

    private Map<String,IVendeeContextStrategy> vendeeContextStrategyMap=new HashMap<String, IVendeeContextStrategy>();

    public Map<String, IVendeeContextStrategy> getVendeeContextStrategyMap() {
        return vendeeContextStrategyMap;
    }

    public void setVendeeContextStrategyMap(Map<String, IVendeeContextStrategy> vendeeContextStrategyMap) {
        this.vendeeContextStrategyMap = vendeeContextStrategyMap;
    }

    public boolean doAction(String strType, ReportDetail reportDetail, OrderDetail orderDetail){
        return this.vendeeContextStrategyMap.get(strType).getTicketContext(reportDetail,orderDetail); }}Copy the code

Spring configuration file

<? xmlversion="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">


    <bean id="orderIntegrateReadFactory" class="com.flight.inter.otaadapter.factory.OrderIntegrateReadFactory">
       <property name="vendeeContextStrategyMap">
           <map>
               <entry key="1" value-ref="ctripContextStrategy"/>
               <entry key="2" value-ref="qunarContextStrategy"/>
               <entry key="3" value-ref="aliquaContextStrategy"/>
               <entry key="4" value-ref="tongChengContextStrategy"/>
           </map>
       </property>
    </bean>

    <bean id="ctripContextStrategy" class="com.flight.inter.otaadapter.manage.cloudticket.CtripContextStrategy">
        <property name="huitiePiaoHao" ref="ctripHuiTieTicketNumber"/>
        <property name="redisManage" ref="policyRedis"/>
    </bean>

    <bean id="qunarContextStrategy" class="com.flight.inter.otaadapter.manage.cloudticket.QunarContextStrategy">
        <property name="huitiePiaoHao" ref="qunarHuiTieTicketNumber"/>
        <property name="redisManage" ref="policyRedis"/>
    </bean>
    <bean id="aliquaContextStrategy" class="com.flight.inter.otaadapter.manage.cloudticket.AliquaContextStrategy">
        <property name="huitiePiaoHao" ref="quaHuiTieTicketNumber"/>
        <property name="redisManage" ref="policyRedis"/>
    </bean>
    <bean id="tongChengContextStrategy" class="com.flight.inter.otaadapter.manage.cloudticket.TongChengContextStrategy">
        <property name="huitiePiaoHao" ref="TongChengHuiTieTicketNumber"/>
        <property name="redisManage" ref="policyRedis"/>
    </bean>
</beans>Copy the code

Interface class:

/** * Created by ling.zhang on 2017/1/21. */
public interface IVendeeContextStrategy {

    boolean getTicketContext(ReportDetail reportDetail, OrderDetail orderDetail);
}
Copy the code

Concrete implementation classes:

public class QunarContextStrategy implements IVendeeContextStrategy{
    private final static Logger logger= LoggerFactory.getLogger(QunarContextStrategy.class);
    HandleManage<TicketContext> huitiePiaoHao;
    RedisManager redisManage;
    public RedisManager getRedisManage() {
        return redisManage;
    }

    public void setRedisManage(RedisManager redisManage) {
        this.redisManage = redisManage;
    }
    public HandleManage<TicketContext> getHuitiePiaoHao() {
        return huitiePiaoHao;
    }

    public void setHuitiePiaoHao(HandleManage<TicketContext> huitiePiaoHao) {
        this.huitiePiaoHao = huitiePiaoHao;
    }
    @Override
    public boolean getTicketContext(ReportDetail reportDetail, OrderDetail orderDetail){
       try {
           TicketContext ticketContextQunar = QunarTicketContext(reportDetail, orderDetail);
           if(ticketContextQunar ! =null) {
               TicketContext ticketContext=huitiePiaoHao.handle(ticketContextQunar);
               if (ticketContext.isHuitieresult()){
                   redisManage.add("vendee-"+ticketContext.getTtsorderno(),"true");
                   redisManage.expire("vendee-"+ticketContext.getTtsorderno(),600);
                   logger.info("orderintegrate huitie success data {}",JSONObject.toJSONString(ticketContext));
                   return true; }}}catch (Exception e){
           logger.error("orerintegrate read ticketcontext error {}",e);
            return false;
       }
        return false; }}Copy the code

Each platform will have the Strategy class above, so I won’t list it all. That way, when you need a new platform, you just need to create a new class and inject it into Spring, and it’s ready to use. Also in line with design principles. When you have a lot of if else in your code, your code needs to be refactored, otherwise you’re stuck in this little space, and when you decouple it out, you have a different view and height, and you can do things many times more.