Thinking in Patterns Chapter 12: System decoupling

Thinking in Patterns with Java V0.9: Chapter 12: System decoupling: TIPatterns.htm#_Toc41169737

1, Sections:

Observer. 95

Observing flowers. 97

A visual example of observers. 101

Mediator. 103

Exercises. 104

2, The main content and feeling:

This is a really difficult reading experience for me in this chapter's

learning. After three times' reading, at last, I can say, Observer Pattern, I

got it.

1), Observer Pattern

From my own understanding with a simplest instance, Observer Pattern can be used in the situation below:

I am prepairing for dinner, I am an Observable; My mother is reading a book in the book room and waiting for dinner, She is an Observer.

When the dinner is ready, I send her a signal: A dinner begin! From my

Observable role, I just:

 setChanged(){/*dinner has ready*/}

 super.notifyObservers(); //notify her the dinner has ready

And she just:

update(){/* put the book into desk, go the dinner room to eat */}

A key of this pattern must be aware:

The Observable object's class you used must inherit from the base class:

java.util.Observable !

Because, in the base Observable class, notifyObservers() don't call

setChanged() method, before you notify the Observer, you must call it in you

own derived-class which inherited from Observable base class; Otherwise,

although you have notified Observer, they notice that "changed" flag hasn't

changed, they don't update() anything. Just an Observer say: yes, I get you

notify, but, I see you don't change anything, so, I needn't do a useless

thing.

Note, in the book, Bruce say:


To get an effect, you must inherit from Observable and somewhere in your derived-class code call setChanged( ). This is the method that sets the ?changed? flag, which means that when you call notifyObservers() all of the observers will, in fact, get notified. In the example above setChanged( ) is simply called within notifyObservers( ), but you could use any criterion you want to decide when to call setChanged( ).

I understood his meaning is: You can put the setChanged() code in anywhere of

your code. But, I think if you want Observer update() himself, you must do a

setChanged() before you call notifyObservers() in the

Observable object. Is there any wrong with me?

 

Another point is important of my understanding is:

One Observable Object can be tied with many of different Observer Object

dynamically. Just like the book's example below:


   ...

    f.opening().addObserver(ba.openObserver());

    f.opening().addObserver(bb.openObserver());

    f.closing().addObserver(ha.closeObserver());

    f.closing().addObserver(hb.closeObserver());

   ...

At last, the Observer Patterns descripted in this book is taken the use of standard java.util library. Of course, it must there are some other Observer Pattern implementions need me to learn.

2), Mediator


Sweep coupling under the rug, how is this different from MVC?

MVC has distinct model and view; mediator could be anything. MVC a flavor of

mediator

Why no more words in pattern of Mediator? So, I can't understand what is

Mediator. And, the sentence above I can't understand it yet. 

3, Questions about programming:

1), What is "callback"?

2), Notice, however, that the control of the flag?s state is protected, so that only an inheritor can decide what constitutes a change, and not the end user of the resulting derived Observer class.

   Who is "end user"?

3), Then it moves through the set and calls back to the update( ) member function of each Observer.

  Is there any "call back" action to the update() method of observer when observed object notify changes to observer?

4), What is Mediator Pattern?

5), Exercises.

4, Questions about english language:

1), Strange words:

Document-View Architecture, plot, textual, facilitate, potentially,

surrounding, observable, constitutes, in case, phenomena, have access to, come

in handy,

2), Difficult sentences:

Sweep coupling under the rug, how is this different from MVC?

MVC has distinct model and view; mediator could be anything. MVC a flavor of

mediator.

According to my learning, Sweep coupling under the rug, just like:(From

http://dict.cn)

sweep the dust under the carpet:

把不愉快的事情掩盖起来

 

                                     incomplete