How to wrap your head around object oriented design

Object Oriented Design is a pretty neat concept, but I struggle to wrap my head around most of its aspects. I think the key to good OO design is a good understanding of how to look at it. I usually look at an object oriented approach:

Classes - objects or objects of the real world

Instance fields are attributes of an entity, ('has A')

Methods are similar to actions, verbs, entity abilities

Interfaces are like abilities that you can cast on an object. It can also be an is-or-may relationship, the realizations of which are not set in stone. Superman - Krypton, who is a Kryptonias, has a set of special abilities such as flying, freezing, etc. Superman flies from Green Lantern and Goku and especially Batman, so Flight as an interface is probably a good idea if you're creating a fictional universe.

public class SuperMan extends Man implements Kryptonian{}

public interface Kryptonian extends Flight, FreezeBreath{
    public void fly();
    public void coolBreath();
}

      

Does the problem occur when you add Generics to the mix? Because the given type parameters somehow create a contract between the class / interface and the type.

public interface Flight<T>{
     public void fly(T t);
}

      

In this example, Flight is combined with T, T could be a superhero, a bird, or something that can fly. But is it really so? Because it's like what simple interfaces do? Although a parameterized interface is still an interface, the relationship to type T is what really worries me. Moreover, things also get complicated when you add a constrained constraint on the type of the parameter.

public class Pidgey<T extends Bird> implements Flight<T>{}

      

What is the real specific object you can identify? The above example is pretty flawed, although using a class parameter also restricts the type of flight, probably a good design because Flight is still independent enough that other classes can still use it without any restrictions. But the example itself is wrong. Pidgey is a bird that can fly, but what can be T? Well, T can be anything, it can be another object or ability. The question is, what are its implications, why put T there? What are real-life examples of this?

It is easy to understand when you are talking about collections because collections are like containers. You can create many containers that contain different objects.

public class WaterBottle<T extends Liquid> implements UniqueCap{}

      

But I saw that Generics is used for more than just container-like objects? How can you design such objects that they are considering?

+3


source to share


2 answers


Your analogies with various OOP features are definitely valid. Generics definitely make the most sense when it comes to collections / containers / hashmaps. However, they use them elsewhere. For example, if a bank wants to process banknotes in many currencies, they can write a public class moneyProcessor

However, generators are not required . In the context of your interface, Flight

there wouldn't be a great reason to use generics. This brings me to another point:



Just because someone does something on one occasion does not mean that you should. OOP is very flexible for a reason. There is always more than one correct path. If a method takes Object

as a parameter, it's not the end of the world. Just make sure you can read it later. :)

Edit: others may read it too.

+4


source


Its a convention to use T when dealing with generics. it makes the code readable as others reading your code will immediately know that you are referencing generic and nothing specific



0


source







All Articles