0

I have package ( of java classes ) in my project.Which need to be removed under some requirement. I have to compile my project to target

Highend

and

lowend

Requirement constraints

  1. Package need to be there in Highend, and should not be there for lowend.
  2. My code base is same for both the targets

To achive this requiment I want a way to define

class myclass{
static final boolean isEnable = false;
public void  API1(){
if(isEnabled){
//function logic
}
}
public boolean  API2(){
if(isEnabled){
//function logic
//return value
}
else
return false;
}
}

Although the "static final " private variable provide a solution to reduce my class size as the is the isEnabled code is false. the compiler remove the bytecode from the complied class.But using this I think it won't solve my problem, it reduces the class size for lowend target But the the function persist which return some default value. Is there any better way or design pattern is solve this problem, as with the current solution I have to made changes in all API of the classes present in the package, Although I have to remove all complete package.

This requirement is related to a java package in Android, So I am including it Android in the tags

dead programmer
  • 4,223
  • 9
  • 46
  • 77
  • google for feature toggle android and find stuff [like this](http://www.togglz.org/), that could do it. NB: I don't endorse this one it's just the first result. Or maybe you could do some kind of "library" for the highend api and have two builds –  Feb 22 '16 at 17:45
  • Sounds like you want a preprocessor. Maybe have a look at [conditional compilation options in Java](https://stackoverflow.com/q/4526113)? – Siguza Feb 22 '16 at 17:46
  • I'd suggest to configure your build tool (gradle, maven, ant, ...) to generate two artifacts, say `highend.jar` and `lowend.jar`. The only issue is that you have to implement `API1` and `API2` twice, once for each artifact. – Luís Bianchin Feb 22 '16 at 22:25

1 Answers1

0

You may want to restructure your code so that API2 is contained in a class which gets eliminated altogether in the lowend version. Moreover, as the callers of API2 get no sane answer in the lowend version, you may be able to eliminate the test inside of API2, too. I mean the following

  • nobody uses the class containing API2
  • the whole class gets eliminated
  • the method needs no isEnabled check

While this sounds good to me, note that it's just one design goal, don't try too hard to follow it, so that you don't end with some insane design. If there are tens of isEnabled checks, then you're (probably) doing something wrong.


You may also want to use a simple lowend implementation overridden by a highend implementation like

class LowendSomething {
    boolean doFancyStuff() {
        throw new BuyTheHighendVersionException();
    }
}

class HighendSomething {
    boolean doFancyStuff() {
        ... do fancy stuff
    }
}

together with a line like

LowendSomething something =
    isEnabled ? new HighendSomething() : new LowendSomething();
maaartinus
  • 44,714
  • 32
  • 161
  • 320