mercredi 20 avril 2016

Replacing interfaces by abstract classes in Bridge pattern

Looking at this explanation of the Bridge pattern, it says that the Abstraction and Implementors are interfaces. However I don't see the reason why they can't be Abstract classes instead.

Example: Imagine that all the constructors from AbstractionImp will always receive 1 parameter, the Implementor, and the only thing the constructor does, is to store that parameter in a AbstractionImp field. Like this:

public AbstractionImp(Implementor implementor) {
   this.implementor  = implementor;
}

So why instead of repeating this alll over the other AbstractImp classes, why not just have this instead? Note: in this case AbstractionImp extends Abstraction which is an abstract class.

public AbstractionImp(Implementor implementor) {
   super(implementor);
}

public Abstraction(Implementor implementor) {
   this.implementor = implementor;
}

Am I violating the Bridge Pattern if I replace the interfaces by abstract classes? Doing it like this, a lot of boilerplate code can be avoided.

Aucun commentaire:

Enregistrer un commentaire