How to write like poetry as service code (using a single annotation + + Example plant remove a large wave is determined if and else)

1. Orders controller, a calculation based on the commodity price discount merchandise id id banking channels and interfaces:

Import org.springframework.web.bind.annotation.GetMapping;
 Import org.springframework.web.bind.annotation.RequestMapping;
 Import org.springframework.web.bind.annotation.ResponseBody;
 Import org.springframework.web.bind.annotation. RestController; 

Import java.math.BigDecimal; 

@RestController 
@ RequestMapping ( "/ the Order" )
 public  class {OrderController 

    / ** 
     amount * after the discount is calculated according to the commodity and banking channels id id 
     * 
     * @param goodsId product id 
     * @param channelId Bank channel the above mentioned id 
     * @return 
     * /
    @GetMapping("/calc")
    @ResponseBody
    public String calcAmount(Integer goodsId, Integer channelId) {
        Context context = new Context();
        BigDecimal bigDecimal;
        try {
            bigDecimal = context.calRecharge(goodsId, channelId);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return bigDecimal.setScale(2) + "";
    }
}

 

2. Context:

Import java.math.BigDecimal; 

public  class {Context 

    / ** 
     Amount * after the discount is calculated according to the commodity and banking channels id id 
     * 
     * @param goodsId product id 
     * @param channelId banking channels id 
     * @return 
     * @throws Exception
      * / 
    public the BigDecimal calRecharge (goodsId Integer, Integer channelId is) throws Exception { 
        strategyFactory strategyFactory = StrategyFactory.getInstance ();
         // the channel id query specific implementation class bank 
        Strategy Strategy = strategyFactory.create (channelId is);
        // call to a specific category is calculated 
        return strategy.calRecharge (goodsId, channelId is); 
    } 
}

 

Example 3. Discount calculation unit factory class, the internal implementation of the class with the mapping between a channel bank to store Map and specific bank id, bank to facilitate access to the corresponding channel according to a specific category id reflection:

import org.reflections.Reflections;

import java.util.HashMap;
import java.util.Set;

public class StrategyFactory {
    private static StrategyFactory strategyFactory = new StrategyFactory();

    private StrategyFactory() {
    }

    public static StrategyFactory getInstance() {
        return strategyFactory;
    }

    private static HashMap<Integer, String> source_map = new HashMap<>();

    static {
        Reflections reflections = newReflections ( "ICBCBankImpl" ); 
        the Set <Class <>> classSet = reflections.getTypesAnnotatedWith (the Pay.? Class );
         for (Class <>? Clazz: classSet) { 
            the Pay the Pay = clazz.getAnnotation (. The Pay class ); 
            source_map. PUT (pay.channelId (), clazz.getCanonicalName ()); 
        } 
    } 

    / ** 
     * get specific implementation class bank the bank channels from the Map ID 
     * 
     * @param channelId is 
     * @return 
     * @throws Exception
      * / 
    public Strategy the Create ( int channelId)throws Exception {
        String clazz = source_map.get(channelId);
        Class<?> clazz_ = Class.forName(clazz);
        return (Strategy) clazz_.newInstance();
    }
}

 

4. After calculating the discount price of the interface:

import java.math.BigDecimal;

public interface Strategy {
    BigDecimal calRecharge(Integer goodsId, Integer channelId);
}

 

5. ICBC implementation class, class notes on the plus @Pay channel id specified ICBC corresponding database:

Import javax.annotation.Resource;
 Import java.math.BigDecimal; 

/ ** 
 * Commercial Bank implementation class, channel id database corresponding to. 1 
 * / 
@Pay (channelId is =. 1 )
 public  class ICBCBankImpl the implements Strategy { 

    @Resource 
    Private goodsMapper goodsMapper; 

    @Resource 
    Private channelMapper channelMapper; 

    / ** 
     * price after discount is calculated according to the commodity and banking channels id id 
     * 
     * @param goodsId product id 
     * @param channelId banking channels id 
     * @return 
     * / 
    @Override 
    public BigDecimal calRecharge(Integer goodsId, Integer channelId) {
        BigDecimal goodsPrice = goodsMapper.getGoodsPriceById(goodsId);
        BigDecimal discountPrice = channelMapper.getDiscountPriceById(channelId);
        if (goodsPrice == null || discountPrice == null) {
            return null;
        }
        return goodsPrice.multiply(discountPrice);
    }
}

 

6. Bank mark for the annotation of implementation class defines a banking channels id attribute:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Pay {
    int channelId();
}

 

7. simulation check prices of Dao:

import java.math.BigDecimal;

public class GoodsMapper {
    public BigDecimal getGoodsPriceById(Integer goodsId) {
        return BigDecimal.valueOf(599);
    }
}

 

8. Analog query query channel discount of Dao:

import java.math.BigDecimal;

public class ChannelMapper {
    public BigDecimal getDiscountPriceById(Integer channelId) {
        return BigDecimal.valueOf(0.5);
    }
}

 

Guess you like

Origin www.cnblogs.com/jun1019/p/11795515.html