Observer vs interface abstract template

I am having problems with the observer pattern. He says that Observer and Subject should be interfaces. I understand why observers are interfaces, but why is it not better for the theme to be an abstract class? Could you already implement at least delete / register?

+8
source share
5 answers

Design samples are designed to adapt to the specific needs of applications; they do not prescribe the rules established in stone. In particular, do you need to decide on some abstract class or interface, taking into account all the consequences that the solution has for the rest of the application.

However, interfaces are recommended for abstract classes in general for several reasons. For example, abstract classes require the use of inheritance, and in many languages ​​you cannot inherit more than one class. If this is not a problem for your use case, continue to use abstract classes if you find them more convenient.

+10
source

Why not just have an abstract class that implements the theme? Using the interface just gives you more flexibility. In fact, you are not buying anything to start with an abstract class. If things all change a lot (say, crossing the boundaries of a process), then your Observable will depend on an abstract implementation.

+7
source

In a design pattern, when the word interface is used, it means an abstract API that is exposed to a client component that will have different specific implementations.

When a design pattern interface maps to the Java world, it can be either a Java interface or an abstract Java class, and a specific design pattern design class for a regular Java class (non-abstract).

However, when making a decision, you need to understand the difference between the Java interface and the abstract class, as well as their purpose, as well as the pros and cons.

See: Vs interface Abstract class

+3
source

why is it not better that the subject be an abstract class

To avoid design binding to a specific concrete implementation. Remember that the goal is to create a template that will give you the flexibility to swap specific objects as needed and not have observers related to what the original implementation was.

You do not want observers to refer to FirstConcreteSubject , but to the ISubject interface, which you can quickly change so that it can be implemented using SecondConcreteSubject without having to change the observers.

However, there is nothing wrong (IMHP) with having an abstract BaseSubject class to hold some code that would otherwise be duplicated by FirstConcreteSubject and SecondConcreteSubject .

+1
source

Java interfaces describe types (as defined in Design Patterns). The limiting factor in Java interfaces is that you cannot declare the instance variable needed for your observer list.

This is where the abstract class comes in. You can declare an instance variable as follows:

 import java.util.List; public abstract class Subject { List<Observer> observers; // this would not be allowed in an interface public void attachObserver(ObjectObserver objectObserver) { // implementation }; public void detachObserver(ObjectObserver objectObserver) { // implementation }; public abstract void notifyObservers(); } 

However, interfaces are better because they are better at forcing encapsulation. You can add another layer of abstraction by declaring three methods in an interface, which in turn declares these methods / "types". Then an abstract class can implement these methods. Of course, any class that extends an abstract class will not have to implement the attachObserver and detachObserver methods.

0
source

Source: https://habr.com/ru/post/946617/


All Articles