Observer Pattern with beta 2

Philipp

Member
Licensed User
Longtime User
Hi Erel,

how would you implement the observer pattern with the new beta.

Polymorphism is there, but interfaces and inheritance aren't. Is there a way to 'simulate' these oop features or what would you suggest how to implement it?

Thanks Erel

Philipp
 

DeerBear

Member
Licensed User
Longtime User
Hi Erel,

how would you implement the observer pattern with the new beta.

Polymorphism is there, but interfaces and inheritance aren't. Is there a way to 'simulate' these oop features or what would you suggest how to implement it?

Thanks Erel

Philipp

Coming from an OOP background, the answer is easy:
just create your observer classes and then use
composition to hook them up into your hierarchy.

The idea is this:

- CObserver -> Implements observer behaviour, raising an
event when needed
- CSubject -> Implements subject behaviour and exposes a "Notify"
method. This could have a parameter of type "Object" in
order to catch all data types.
- CYourObserving class -> Includes an instance of CObserver and
registers it with your subject class(this is
important)
- CYourSubject class -> Includes an instance of CSubject and exposes a
proxy method with a parameter of type CObserver.
This will be passed on to the correct CSubject
instance.

This is how I would do it.

Regards,

A
 
Upvote 0

Philipp

Member
Licensed User
Longtime User
Hi dearbear,

I already implemented observer pattern with c# and forms but wasn't sure what would be the best way to do it with b4a.
What do you mean with proxy methods?

Edited with tapatalk...

Gesendet von meinem GT-P1000 mit Tapatalk 2
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several ways to implement such a pattern.

You can create an Observer class:
B4X:
'Class module
Sub Class_Globals
   Private targets As List
End Sub

Sub Initialize
   targets.Initialize
End Sub

Public Sub AddTarget(Target As Object)
   targets.Add(Target)
End Sub

Public Sub RemoveTarget(Target As Object)
   Dim i As Int
   i = targets.IndexOf(Target)
   If i > -1 Then targets.RemoveAt(i)
End Sub

Public Sub NotifyTargets
   For i = 0 To targets.Size - 1
      CallSub(targets.Get(i), "Notify")
   Next
End Sub

Now lets say you want to create a List object that raises some event.
So you add an Observer instance to the MyList class:
B4X:
'Class module
Sub Class_Globals
   Private obs As Observer
End Sub

'Initializes the object. You can add parameters to this method if needed.
Sub Initialize
   obs.Initialize
End Sub

Public Sub RegisterForSomeEvent(target As Object)
   obs.AddTarget(target)
End Sub

Private Sub TestWhetherToRaiseEvent
   If ...
      obs.NotifyTargets
   End If
End Sub

Other modules (activities, services or any other class) that want to be notified should create a Notify sub and call MyList.RegisterForSomeEvent(Me).

Note that currently (v1.95) Me only works with classes. However it will also work with Activities and services.
 
Upvote 0

DeerBear

Member
Licensed User
Longtime User
Hi dearbear,

I already implemented observer pattern with c# and forms but wasn't sure what would be the best way to do it with b4a.
What do you mean with proxy methods?

Edited with tapatalk...

Gesendet von meinem GT-P1000 mit Tapatalk 2

A proxy method is a method that encapsulates another one to
which redirect calls.

Imagine you have 2 classes:

C1 incorporates(by composition) an instance of C2.
If C2 is private(and it should, in my opinion, when doing
patterns) you have no way to call its methods from any
class referencing C1.

Thus, in C1, you create a method with the same name and the
code will just call the corresponding method of C2, with the same
signature.

Regards,

A
 
Upvote 0

Philipp

Member
Licensed User
Longtime User
A proxy method is a method that encapsulates another one to
which redirect calls.

@DeerBear: Thank you for your explanation. I never heard of proxy methods before. I did a quick google and found a pattern named 'proxy pattern' which reads like what you wrote. Is this the same? Then this wouldn't be a native oop-functionality but a pattern like the observer pattern?

We are just beginning with patterns at the evening school, the observer being the first one, so please forgive me my ignorance :sign0104:

But for a project I would like to start with b4a instead of pure Java+SDK the observer pattern would fit perfectly into my needs.

@Erel: Are you mixing the Subject-Class or ConcreteSubject-Class with the Observer-Class? Where is the update()-Method in the Observer-Class?

As I understood the Observer-Pattern, the benefit is that the data stays in the ConcreteSubject and it notifies all attached observers when the state has changed. Or maybe I don't understand your code fully....?
 
Upvote 0

DeerBear

Member
Licensed User
Longtime User
@DeerBear: Thank you for your explanation. I never heard of proxy methods before. I did a quick google and found a pattern named 'proxy pattern' which reads like what you wrote. Is this the same? Then this wouldn't be a native oop-functionality but a pattern like the observer pattern?

We are just beginning with patterns at the evening school, the observer being the first one, so please forgive me my ignorance :sign0104:

But for a project I would like to start with b4a instead of pure Java+SDK the observer pattern would fit perfectly into my needs.

@Erel: Are you mixing the Subject-Class or ConcreteSubject-Class with the Observer-Class? Where is the update()-Method in the Observer-Class?

As I understood the Observer-Pattern, the benefit is that the data stays in the ConcreteSubject and it notifies all attached observers when the state has changed. Or maybe I don't understand your code fully....?

Proxy is another pattern :)

Patterns are not ready-to-use recipes, they are ways of doing things.
Thus, there is no such thing as a good or bad implementation, but
there are more or less fit-for-purpose implementations.

To put it in another way, they are a way of solving a problem, but
that does not mean my implementation is worse than Erel's one or the
other way around. They are different as they use a different
approach and can be used best in different contexts.

I tend to be more philosophical about it :) and thus decouple the
actual implementation code from what it should be
doing(the famous black box you might have read about somewhere :) ).

And yeah, it's true, as soon as you start using a pattern, you find
that you also use many more as a result of one :)

Some patterns you will want to have a peek at:

- Observer
- Proxy
- Facade
- Decorator
- Builder
- Command

Some of these we use in basic4android programming already,
although they do not surface as such.
Observer is used in events, for example, where
Decorator is used in the GUI(you have a surface and
decorate it with views).

And lots, lots more...

Regards,

A
 
Upvote 0

Philipp

Member
Licensed User
Longtime User
But aren't you adding the instance of an observer to another instance of an observer? So basically you don't have an explicit subject, but use one observer-class that acts as observer and as subject?
 
Upvote 0

Philipp

Member
Licensed User
Longtime User
I -think- I understand now. Sticking to the class-diagram from the link, your observer should be called Subject or ConcreteSubject, MyList-Class is the Controller which implements the Subject-Class and the ConcreteObserver is any Class that hooks itself into.

Did I get it this time?
 
Upvote 0

DeerBear

Member
Licensed User
Longtime User
I -think- I understand now. Sticking to the class-diagram from the link, your observer should be called Subject or ConcreteSubject, MyList-Class is the Controller which implements the Subject-Class and the ConcreteObserver is any Class that hooks itself into.

Did I get it this time?

Look mate, it's easy.

Subject: holds observers.
Observers register with subject.

Regards,

A
 
Upvote 0

Philipp

Member
Licensed User
Longtime User
Look mate, it's easy.

Subject: holds observers.
Observers register with subject.

Regards,

A

I know it's easy, I'm just trying to combine the b4a-syntax with what I know how to implement with c# or java. Just stumbling over the b4a-syntax with Erel's class names.

I won't be starting my 'bigger' project with b4a, before I didn't create some smaller one's before.
 
Upvote 0

Philipp

Member
Licensed User
Longtime User
Hurray!
(Or are your simply being nice to me and/or giving up? ;) )

Thank you all for your patience!

Sent from my GT-P1000 using Tapatalk 2
 
Upvote 0
Top