All methods in one class or polymorphism implementation?

Here is what you need to know in order to understand the question:

  • I want to connect the SCL class to the Region class.
  • Now I have many different ways in which I want to connect an instance of these 2 classes.
  • Writing is java
  • Global variables not used

Therefore, I can either create several classes (around 9) that use polymorphism, but then each class has only one method called connect (...) with many different parameter lists. I think this is called a functor class.

For example, the class "SCLToRegionOverlapCircleConnect" will have a connection method that looks like

public void connect(SCL scl, Region region, int radius, int overlapPercentage) {...} 

while the class "RegionToRegionNonOverlapSquareConnect" will have a connection method that looks like

 public void connect(Region bottomRegion, Region topRegion, int sideLength) {...} 

OR

I can just create one class called ConnectionTypes and just have 9 different methods, each with a different method signature.

What are the PRO and CON of each implementation? Thanks!

+4
source share
3 answers

If you use polymorphism, then you define a join method when instantiating the SCL object. Does this make sense? Or can the SCL class be connected to the Region in various ways, despite its life? In this case, polymorphism does not make sense. One important aspect that we don’t know about is what happens with the parameters of the connect(...) method. Do they need to be stored in the SCL class, in which case with different parameters, polymorphism may again make sense so that each class can store the corresponding parameters.

Another idea is that the act of connecting the SCL class to the region is really a method for the SCL class in general or should it live somewhere else?

0
source

I suggest you use the second one.

  • fewer classes, so a clarelier project than another.
  • you do not need to move 9 classes when you want to use your methods in another project (for example), but only one class.
  • during programming, one rule does not duplicate code; using 9 classes, you should write a declaration 9 times, and possibly declare the same global variables 9 times, using a lot more memory than using one.
  • overload, this means that if you need to do one thing but in several ways (for example, you need to print some objects, and the result will be one line, but you need to write it differently for one type of object, etc., you can use this technique), you can write 9 methods with the same name, with one output, but with different inputs.
  • inheritance: if you want to create a class that inherits these methods, you MUST use only one class, because java does not support multiple inheritance.

I do not see any CONs, except that you need to reinitialize global variables to avoid problems.

0
source

Let me directly say two things:

  • You are thinking of a Command pattern, not a Functor pattern. The difference is that the latter also has a method to get the return value, but your connect method is not valid.
  • The Functor template would not have a different signature for each of your connect methods; instead, each particular class would have special settings for the parameters (specific to the particular way you want to connect) and the same inconspicuous public void connect() method. The latter will be the only method declared in the general style of connect .

I can provide an example code if you want.

Pro: if it makes sense in your code to work with Connect commands without knowing which of the 9 ways you deal with, then the Command pattern is your friend.

Con: you will have more code, and encapsulating pure functionality may slightly decrease the comprehensibility of your code.

0
source

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


All Articles