Skip to content

Conversation

@frnlpz
Copy link
Member

@frnlpz frnlpz commented May 7, 2017

Here's another example of observer pattern. In this case, the goal is to notify players in a game of roulette of changes in the category picked after the wheel has spinned.


class Roulette{

var observer: CategoryObserver? = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to create a separated object in this case. I think it is more natural if Players are observing the Roulette directly 👍

package oop.Observer2


data class Player (val name: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Player is the observer. The Roulette is observable.

package oop.Observer2

interface Observer<T>{
val list: MutableList<Player>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why must an Observer have a list of Players? This is an unnecessary coupling 😅

Copy link
Member

@Cotel Cotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is really illustrative. Even more than my previous example I think. Maybe we should change it in the README 🤔

Write some tests before merging please 🙏

@frnlpz
Copy link
Member Author

frnlpz commented May 7, 2017

Now it's much clearer! Thx @Cotel

I'm writing some tests between this evening and tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants