I have a complex class, and I want to simplify it by implementing the facade class (suppose I have no control over the complex class). My problem is that a complex class has many methods, and I will simply simplify some of them, and the rest will remain as it is. What I mean by “simplification” is explained below.
I want to find a way that, if the method is implemented with a facade, then call it, if not, then call the method in a complex class. The reason I want this is to write less code :) [less]
Example:
Facade facade = // initialize facade.simplified(); // defined in Facade class so call it // not defined in Facade but exists in the complex class // so call the one in the complex class facade.alreadySimple();
Possible options:
Option 1: Write a class that contains a variable of a complex class and implements complex ones, then implement simple ones with direct delegation:
class Facade { private PowerfulButComplexClass realWorker =
But with this approach, I will need to implement all the simple methods with only one delegation expression. So I need to write more code (it's simple though)
Option 2: Extend a complex class and implement simplified methods, but then both simple and complex versions of these methods will be visible.
In python, I can achieve similar behavior as follows:
class PowerfulButComplexClass(object): def alreadySimple(self):
So what is the Java way to do this?
Edit: what do I mean by “simplify”: a complex method can either be a method with too many arguments
void complex method(arg1, arg2, ..., argn)
or a set of related methods that will almost always be called together to achieve a single task
outArg1 = someMethod(arg1, arg2); outArg2 = someOtherMethod(outArg1, arg3); actualResult = someAnotherMethod(outArg2);
so we want to have something like this:
String simplified(arg1, arg2, arg3) { outArg1 = someMethod(arg1, arg2); outArg2 = someOtherMethod(outArg1, arg3); return someAnotherMethod(outArg2); }