Hi there:
I just want to inject some design patterns into my Java code, but I don't know which style to use -- is inheritance or interface preferred? And why?
Thanks!
Kind regards!
Hi there:
I just want to inject some design patterns into my Java code, but I don't know which style to use -- is inheritance or interface preferred? And why?
Thanks!
Kind regards!
Design patterns aren't a thing to just randomly inject into your application. They're design-time sorts of things, not parmesan cheese that you sprinkle on your code after it's already baked.
That said, Josh Bloch's seminal Effective Java strongly encourages developers to use interfaces for shared behavior rather than using inheritance. This matches my own experience.
ETA: Among other reasons, if you're implementing an interface, you can easily create a mock of that interface for use in testing without worrying about the rest of the inheritance hierarchy.
Design patterns are not 'injected' into your code - you will first need to notice that your problem is similar to problems solved by many others, and that they have distilled a pattern that solves the problem. The most famous ones are here
also, for whether you want to use inheritance (aka extending), or interface depends. usually interface and composition works better.
The two features are not excluding. Interface and implements specify types and compatibility with a type. Inheritance allows for sharing code efficiently. In a classic design, the type hierarchy is expressed using interfaces, while code reuse is achieved using inheritance.
I do agree that one should not think about injecting OR using some design patterns. Design patterns are meant to solve very specific problems within in given context.
About interfaces and inheritance:
Interfaces are being used when you need runtime polymorphism. So, you define an interface and you can have multiple different implementations of it. In client code you can just declare reference as an interface type. Now, you dont bother about actual type of the object passed to the client at runtime. You just care about calling a method on the reference.
interface Car {
void startEngine();
void stopEngine();
}
class Maruti implements Car {
public void startEngine() {
System.out.println("Maruti engine started");
}
@Override
public void stopEngine() {
System.out.println("Maruti engine stopped");
}
}
class Porsche implements Car {
@Override
public void startEngine() {
System.out.println("Porsche engine started");
}
@Override
public void stopEngine() {
System.out.println("Porsche engine stopped");
}
}
In above example as a client, you would just declare reference as Car type. And at runtime you can have Maruti object or Porsche object, you dont care about. What you care about is just to call startEngine or stopEngine.
Inheritance is generally used for code re-usability and extensibility. So, you have common code in two classes and both seem to belong to a common type then you can create a parent class (some times abstract) and move the common code in the parent class. This way you can get rid of duplicate code. The other use case is extensability, sometimes you dont have control on the source code of class and still you would like to add/change some behaviour. You can use inheritance and override certain methods.
There is one more thing called "composition" which is a preferable way over inheritance for extensibility.