简体   繁体   中英

What is the best design pattern in java to implement large set of methods having similar signatures?

I want to implement all the cdl (candle stick pattern) methods of below class in TA-Lib .

There are around 61 cdl analytic methods out of which around 90% are having similar signature, just that their core implementation is different.

For example:

 public RetCode cdl2Crows(int startIdx,
      int endIdx,
      double inOpen[],
      double inHigh[],
      double inLow[],
      double inClose[],
      MInteger outBegIdx,
      MInteger outNBElement,
      int outInteger[])


public RetCode cdl3BlackCrows(int startIdx,
      int endIdx,
      double inOpen[],
      double inHigh[],
      double inLow[],
      double inClose[],
      MInteger outBegIdx,
      MInteger outNBElement,
      int outInteger[])

I was thinking if I can pass the method name as a argument from my source class and then using reflection invoke the methods something like to avoid duplicate code

public invokeAnalytic(String analyticMethodName, common params .....)
{
    // using reflection invoke analyticMethodName of Core class
    // and pass rest of the params
}
  1. What is the best design pattern in java to follow for such a scenario?
  2. Is there going to be a performance issue if I use reflection for such a scenario?

How about wrapping the arguments in an immutable Value Object ?

Eg

MyValueObject params = new MyValueObject(int startIdx,
    int endIdx,
    double inOpen[],
    double inHigh[],
    double inLow[],
    double inClose[],
    MInteger outBegIdx,
    MInteger outNBElement,
    int outInteger[]);

// ....
someObject.cdl2Crows(params);
// ...
someObject.cdl3BlackCrows(params);

Create a final class of the common data points (similar to a struct in C) and pass it as an argument to your functions. It's a little heavy, but not as bad as you might think (especially if the class is declared final ).

public interface CDL

    public RetCode invoke
    (
          int startIdx,
          int endIdx,
          double inOpen[],
          double inHigh[],
          double inLow[],
          double inClose[],
          MInteger outBegIdx,
          MInteger outNBElement,
          int outInteger[]
    );

static Map<String,CDL> map = new HashMap<>();


map.put("cdl2Crows", new CDL()
{ 
    public RetCode invoke(...)
    { 
        impl... 
    }
});
...

Reflection should be avoided in this case as you'll lose both safety and performance for not much more than having to type less.

In this case, I'd just use either a hierarchy of interfaces or abstract classes based on where the method signatures are the same/where the implementation details are shared.

I think the Strategy Pattern is your best choice for this:

http://java.dzone.com/articles/design-patterns-strategy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM