-
Notifications
You must be signed in to change notification settings - Fork 1
Observer with subscription #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
} | ||
|
||
|
||
class Subscription<A> internal constructor(internal val update: (A) -> Unit, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Internal constructor??? what is that? 😲
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal is like in java a class without public, is a visibility package only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is for testing purpose I suppose?
|
||
open class Observer<A>(private val value: A) { | ||
|
||
private var subscription: Subscription<A>? = null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need a Subscription class? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to subscribe and unsuscribe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the register method is in the publisher, you only need to add another method to remove subscriber
|
||
} | ||
|
||
open class Observer<A>(private val value: A) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why open class
? You could have made it abstract or even an interface with implementation (I think Kotlin let to do that...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, should exist a Observer interface, but for this example is more clear without a one component more.
@@ -1,13 +1,77 @@ | |||
package oop.Proxy | |||
|
|||
interface Car { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a missed commit or it is included too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a gost commit lol hahaha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ghost* 👻
fun main(args: Array<String>) { | ||
|
||
|
||
var carVirtualProxy = CarVirtualProxy({ RealCar("tonilopezmr", 5) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you wrap that constructor parameter in a lambda?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be lazy, do you remember?
Because you don't want to do a new
inside https://github.com/CloudCoders/Design-Patterns/pull/55/files/ad103bb6a835ebef42ee91260faf98a8889db69e#diff-50695285805be0311b4d9813df63d03dR22
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I confused it with CarProtectionProxy
which has not function as a constructor parameter. My bad 😝
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because Virtual is lazy
val observers = mutableListOf<Observer<String>>() | ||
val subject: PublishSubject<String> = PublishSubject(observers) | ||
|
||
"register a observer" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow nice and klean 😏 I like it 👍 🔝
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I was starting with a JUnit test, but how I have created a one file for 3 classes, I prefer use this kind of test to separate differents things, like Subject and Observer 👍
You can notify for all observers but some observers can subscribed and unsuscribed in any moment, this is very common in Android because Screens are subscribed and unsuscribed every time.
Example output:
Coverage: