Title: OOP Observer Pattern: call notifyListener from outside of the subject
Tags: oop;design-patterns;observer-pattern
Question: Is it ok for an Observer object to call notifyListeners() on an observable after changing some of its fields? What disadvantages could arise? Usually it's the subjects' responsibility to notify listeners of changes.
I frequently come across the following problem when wrapping model classes in an observable / subject e.g. to make them accessible in UI.
Should my subject notify its listeners whenever any field of the model object is changed or should I provide some kind of method setModel() that takes a new model object as an argument and only then notifies all listeners?
For example:
class MyData {
String myString;
int myInt;
class MyDataObservable {
private MyData data;
// constructor
void setString(String string) {
data.myString = string;
notifyListeners();
void setInt(int num) {
data.myInt = num;
notifyListeners();
void notifyListeners() {...}
void addListener(Observer o) {...}
I don't like that my subject basically mirrors all the properties of my model .. which is pretty ugly.
class MyDataObservable {
private MyData data;
// constructor
void setData(MyData data) {
this.data = data;
notifyListeners();
void notifyListeners() {...}
void addListener(Observer o) {...}
The downside of this approach is that I have to build a new copy of my data class everytime anything changes, even if it's just a bool flag on my model class.
So my idea was to allow for observers to notify other listeners when they changed something with e.g. an update function.
Are there other, more elegant alternatives?
At the moment, making my data class an observable is not an option because I'm using a framework that provides an observable mechanism that can also hold business logic. For that reason I want to decouple my data class and the logic / observable.
Comment: Do you need to have a separate `MyData` class at all?  What is it used for/ why does it exist? Why couldn't you use the observable class for that?

Comment: You say "observer" in comments, but your code says "Observable".  Those are very different things.

Here is the accepted answer: In this case I would use inheritance rather than composition: MyData is-a Observable rather than Observable has-a MyData.
class Observable {
Collection<Listener&gt; listeners
void addListener(Listener l) {...}
void notifyListeners() {...}
class MyData extends Observable {
String myString;
int myInt;
void setString(String string) {
this.myString = string;
notifyListeners();
void setInt(int num) {
this.myInt = num;
notifyListeners();


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:02/15/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<oop><design-patterns><observer-pattern>', 'Title': 'OOP Observer Pattern: call notifyListener from outside of the subject', 'ViewCount': 83, 'id': 60239138, 'user_AboutMe': '', 'user_AccountId': 8164177.0, 'user_DisplayName': 'Wecherowski', 'user_Reputation': 709.0, 'user_Views': 204.0, 'user_id': 6147174.0}}