View on GitHub

wiki

Object-oriented programming

Terminology

OOP Best practices

Constructors

Class best practices

When to refactor functions

Interfaces and inheritance

Interfaces do not exist (specifically in Java) to support multiple inheritance. That’s kind of a hack people occasionally use them for, but their real purpose is to define a contract that classes support to facilitate polymorphism.

Prefer using interfaces (abstract base classes” in Python) over inheritance; hierarchical inheritance can get really messy.

Naming

“_able” adjective if interface named after things that can be done to the object (e.g. Enumerable), a “_er” noun if interface named after things your object does to other objects (e.g. EqualityComparer), noun if your interface is named after a thing whose behavior it mimics (e.g. List)

Inversion of control

Non-IOC Programming

You will instantiate ObjectB and ObjectC within Class A by calling constructors of B and C somewhere within Class A. That means creation of ObjectB and ObjectC depends on programming.

Class A{
    B b;
    C c;
    /*
    See how b and c are instantiated by calling constructor of class B and C.
    That means, Class A needs knowledge about B and C like
    ‘Which are the possible constructors of B and C?
    What should be the constructor parameters?’
    This is called as Tight coupling between A and B/A and C.
    */
    Public A(){
        b = new B();
        c = new C();
    }
}

IOC Programming

Instantiation of ObjectB and ObjectC will be done by container and will be provided to Class A directly. You can either provide created object as a Constructor Argument (Constructor Injection) or using Setter Method (Setter Injection). Results in loose coupling, reusable components and easy to configure application.

In summary, you should think twice and hard about having “new” in a constructor. Prefer to set instance variables as arguments or setters.

Also consider: you might want to group commonly used combination of parameters into factories. For example, in class Class(A a, B b, C c), where A B and C are interfaces, if you use, say Class(new AConcrete1(), new BConcrete3(), new CConcrete4()) at some point (especially if you do this multiple times), do this in a factory instead of copying that same code in each time it’s used. If you have to change the concrete objects being used for that situation, then you only have to change the passed parameters in one place.

Class A{
    B b;
    C c;
    /*
    Look Object of class B and C will be created by Container and provided as Constructor argument.
    So, Class A doesn’t need to know class B and C constructors and all.
    It is loose coupling between A and B/A and C.
    */
    Public A(B b, C c){
        this.b = b;
        this.c = c;
    }
}