Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

tonilopezmr
Copy link
Member

@tonilopezmr tonilopezmr commented Jun 3, 2017

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:

Value: First request a value and updates
Value: Third request a value

Notify: Toni
First updated: Toni
Second updated: Toni

Notify: Cotel
First updated: Cotel
Second updated: Cotel

Unregister all

Notify: Fran
Nothing happens

Register first and second

Notify: That's all
First updated: That's all
Second updated: That's all

Coverage:

screen shot 2017-06-03 at 13 18 20

}


class Subscription<A> internal constructor(internal val update: (A) -> Unit,
Copy link
Member

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? 😲

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

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
Copy link
Member

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? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

to subscribe and unsuscribe

Copy link
Member

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) {
Copy link
Member

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...)

Copy link
Member Author

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 {
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

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) })
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

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 😝

Copy link
Member Author

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" {
Copy link
Member

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 👍 🔝

Copy link
Member Author

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 👍

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.

2 participants