-2

I decided to split the last part of that question here into a new question here: https://softwareengineering.stackexchange.com/questions/411738/extension-of-classes-where-to-put-behaviour-how-much-direct-access-is-allowe

If i have a lib and i want to use it, i wrote mostly a own class. This class has one method. In that there is the code how to instantiate the lib/framework. Sometimes there are a few more methods, with them i not only instantiate the class but use it. For example if i want to start a http-server i have there a start-method.

class Container
{

TheLib theLib;

public void init() //or a constructor
{
   //some init of the theLib 
}

public void start() //
{
    theLib.doSomething(...)
    theLib.doSomethingmore(...);
    theLib.start(...);
}

//important!
public TheLib getTheLib()
{
    return this.theLib; //after i started configured it and so on, i want of course use all methods, 
                    which the lib have in some other parts in my application
}


}

But it seems not to be the best solution.
Are there any better solutions, that OO is?
Often i also use only one method, a own class for this seems to be here a big overhead?
Exposing the lib breaks encapsulation? Tell-Dont-Ask is also violated?

Robin Kreuzer
  • 162
  • 1
  • 10

1 Answers1

1

Everything depend on what you actually need or how you have access to your 'the lib' instance.

public class Container {
    
    private TheLib theLib;
    
    /* #1: Do you already created the instance before? */
    public Container(TheLib theLib) {
        this.theLib = theLib;
    }
    
    /* #2: Do you need to created the instance each time? */
    public Container() {
        this.theLib = new TheLib();
    }
    
    public void start() {
        theLib.doSomething(...)
        theLib.doSomethingmore(...);
        theLib.start(...);
    }
    
    public TheLib getTheLib() {
        return this.theLib;
    }
    
    public static void main(String[] args) {
        /* #1 */
        TheLib theLib = ...;
        Container container = new Container(theLib);

        /* #2 */
        Container container = new Container();
        
        /* Continue the flow of your program */
        container.start();
        
        container.getTheLib().doSomethingEvenMore();
    }
    
}

Or maybe you actually need only one instance of your 'Container' class. In this case, you should look on how to make a singleton: Java Singleton and Synchronization

Anwser: Often i also use only one method, a own class for this seems to be here a big overhead?

Well, in Java, you cannot do formal programming like in C, so everything line of code that you write, or will be using, has to be in a class of some sort. If your piece of code is small and don't really need an object, static function might do the work.

Enzo Caceres
  • 519
  • 3
  • 15
  • maybe you have there still some ideas to (Exposing the lib breaks encapsulation? Tell-Dont-Ask is also violated?) and (Are there any better solutions, that OO is?), because my question is not a technically question more a programm-design-question.... – Robin Kreuzer Jun 20 '20 at 19:17
  • If you want to better understand how encapsulation might break, i suggest this video https://www.youtube.com/watch?v=QM1iUe6IofM , the theory is pretty hard to apply sometimes, you will often found in codebase a lot of code breaking the OO rules. – Enzo Caceres Jun 20 '20 at 19:23
  • ok^^ yeah i know this video, but didnt watch it, maybe i should^^.... but you said its hard to apply oo sometimes? really only sometimes? and is it still hard or even impossible? i will post soon a question where i would ask if oo is failed. However i think i vote your answer up, but still wait a few days if there is some more, then i decide to mark one of them as acceptance one. If you think my question here isnt so bad, so feel free to vote up and delete one -1^^ – Robin Kreuzer Jun 20 '20 at 19:30
  • 1
    I don't think that your question is bad, but just not well asked. When we are reading it, we think that you are asking technical question, not theorical one. OOP is very difficult to do right, but it is not difficult to make it work. Sometime you should just not think too hard for a simple problem: if your code is solution is dump, but work, then that a good solution. – Enzo Caceres Jun 20 '20 at 19:38