java - remove conditional statements when dealing with inheritance -


I want to avoid conditions and apply some techniques to improve codes and make extensible easily when Working with heritage classes.

We have in this example.

Both MarketOrder and LimitOrder expand with the base class order.

TradeServiceImpl with the Trade get GetUnitPrice method that will be 2 orders traded

In this method, there are several conditions for selecting the right value for different types of orders. In addition, it is being converted into derivative classes.

The problem with this method is that if I want to add 5 new orders, then I have to give 7 * 7 = 49 statement, in some of them, convert me into a derived class which I I can confuse that, which seems to be very easy to write a buggy code and gives me the smell I should redesign

Can I get some ideas?

  // Domains Public Class Order {Private order type reward order type; // PUBLIC ORDER TYPES EONOM can buy or sell getOrderType () {return orderType; } Public Zero Set Order Type (Order Typ Order Order) {this.orderType = orderType; }} Public Class Limit Order Order Enhances {Private Long Unit Price; Public long mill units () {return unit value; } Public Zero Set Unit Price (Long Unit Value) {this.unitPrice = unitPrice; }} In order to public market order order {} / expansion of service public sector TradeServiceImpl applies TradeService {Private TradeRepository tradeRepository; Public Long Mill Units (Order Buy Order, Order Sale Order) {If order order of market order and order order of market order} {Return Trade Repository. } If (MarketOrder & amp; amp; & amp; Sales Sales Order of Limit Order) {Return ((Limited Order) Sales Order) .getUnitPrice (); } (Purchase order example for MarketOrder's limit order & sales order;) (return order) purchase order) .getUnitPrice (); } If (LimitOrd And Lift Order Of Limit Order) (Buy (Order Limit Orders)) .getUnitPrice () + ((LimitOrder) sellOrder) .getUnitPrice () / 2;} Return 0L;}}  < To begin with / pre>  

, two cases can be processed in one: when the order is different If: (MarketOrder & Sales Sales Order of Limit Order) {Return ((Limited order) Sales order) .getUnitPrice ();} (Buy Order of Limit Order & amp; ; sale order F market order) {return (buy order order) .getUnitPrice ();} <:

  if buyOrder.getClass ( ). Equal (LimitOrder.getClass ()) {Return (Selling Limited Orders) .getUnitPrice ();}   

On the other hand, you can use the Ord Class Functions In the form of the index of the map with the object, in this way, you can expand your fun disturbance by adding elements of this map.

You can create an interface called Anonymous Inner Using the class, calculation algorithms can include:

  public interface IFunction {public executed long (order oy, order ob); }   

And decide which behavior is executed to use this map using order classes:

  Map & lt; Class, map & lt; Class, IFunction & gt; & Gt; OpClass2calc algorithm = new Hashmap (); IFunction market_market = New IF Function () {@ Override Public Long Expect (Order A, Order B) {Return Trade Repository. GateLastPris (); }}; IFunction market_limit = New IF Function () {@ Override Public Long Exposure (Order A, Order B) {Return (Limit Order) a) .getUnitPrice (); }}; Map and lieutenant; Class, IFunction & gt; Marketmap = new hashmap (); MarketMap.put (MarketOrder.class, market_market); Marketmap (Limit order class, market_limit); OpClass2calcAlgorithm.put (marketMap); Finally, your  getUnitPrice  method can be implemented in such a way:  
  Public Long Mill Units (Order Buy Order) Order order order) {long rate = 0 L; Maps & lt; Class, procedure & gt; First level = opClass2calc algorithm .get (buyOrder.getClass ()); If returns (first level == zero) returns; IFunction calcAlg = firstLevel.get (sellOrder.getClass ()); If return (calcAlg == faucet) returns; ret = calcAlg.execute (purchase order, cell order); Return writ; }    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -