How to simplify a class without writing already simple methods?

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 = // initialize public void simplified() { // do stuff } public void alreadySimple() { realWorker.alreadySimple(); } // more stuff } 

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): # really simple # some very complex methods class Facade(object): def __init__(self): self.realworker = PowerfulButComplexClass() def simplified(self): # simplified version of complex methods in PowerfulButComplexClass def __getattribute__(self, key): """For rest of the PowerfulButComplexClass' methods use them as they are because they are simple enough. """ try: # return simplified version if we did attr = object.__getattribute__(self, key) except AttributeError: # return PowerfulButComplexClass' version because it is already simple attr = object.__getattribute__(self.data, key) return attr obj = Facace() obj.simplified() # call the one we have defined obj.alreadySimple( # call the one defined in PowerfulButComplexClass 

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) // n is sufficiently large 

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); } 
+4
source share
4 answers

He called Inheritance. Consider the following code:

 class Complex { public function complex() { /* Complex Function */ } public function simple() { /* Already simple function */ } } class SimplifiedComplex extends Complex { public function complex() { /* Simplify Here */ } } 

The simple() method will work with the SimplifiedComplex object.

+3
source

I think you already called it. I would choose option 1. It provides maximum flexibility, given the rigidity of java.

I prefer it because it contributes to composition over inheritance. Although this creates more code, I find that such projects are generally easier to change in the long run.

Inheritance should only be used to model strict "is" relationships in which a subclass necessarily has all the properties and behavior of a base class . If you use inheritance for something else, then you are asking for trouble.

Finally, I do not buy the idea of ​​"Less Is More" (add an incredibly short, illegible perl example). I buy the principle "The code should be as simple as it should be and not easier."

+2
source

Depending on your use case, you can create a facade in front of some functions of a complex class by delegating (option 1), and instead of providing support for the rest of the functionality of a complex class, you provide access to the complex class directly (getComplexClass).

This can make the design more understandable. Consider, for example, a complex class that processes most of the functions of the banking system. Creating a class called “Account” that has access to a complex class but uses only methods related to a bank account helps the project. For convenience, the Account class could have a getBankSystemForAccount method or something similar.

+1
source

This is ugly in Java, but you can write a function that takes the name of the method you want to call, and a list of parameters, and use reflection to find the appropriate method to call. It will be conceptually similar to the way you do it in Python, with the exception of the much uglier one.

+1
source

Source: https://habr.com/ru/post/1411394/


All Articles