quinta-feira, 6 de novembro de 2014

Design Patterns - Observer

This week was time to make another visit to Observer Pattern, this time implementing on "Frontent" JavaScript.

Counter is a "class" that manages an, obviously, a number counter, this counter is shown in an HTML page in a DIV.

The Observer pattern is used to observe whenever the counter changes and updates the HTML elements in charge.

HTML page look something like:

1:  <html>  
2:       <head/>  
3:       <body>  
4:            <div id="counter1"/>  
5:            <progress id="counter2" value="1" max="100"/>  
6:            <button type="button" onclick="aCounter.sum(10)">Increment</button>  
7:            <button type="button" onclick="aCounter.sub(10)">Decrement</button>  
8:            <script src="script.js"/>  
9:            <script>        
10:                 var aCounter = new Counter();        
11:                 aCounter.addObserver( new CounterObserver('counter1') );        
12:                 aCounter.addObserver( new ProgressBarCounterObserver('counter1') );      
13:            </script>  
14:       </body>  
15:  </html>  

And our counter would look like (comments and other stuff removed):

1:  var Counter = (function () {  
2:    function Counter() {  
3:      this.value = 1;      
4:            this.observerList = [];    
5:       }  
6:    Counter.prototype.sub = function( how_much ) {  
7:      this.value -= how_much;      
8:            this.executeObservers();    
9:       };    
10:       Counter.prototype.sum = function( how_much ) {  
11:      this.value += how_much;      
12:            this.executeObservers();    
13:       };    
14:       Counter.prototype.getValue = function () {  
15:      return this.value;    
16:       };    
17:       Counter.prototype.addObserver = function( aObserver ) {  
18:      this.observerList.push( aObserver );    
19:       };    
20:       Counter.prototype.executeObservers = function () {  
21:      var that = this;      
22:            this.observerList.forEach( function( item ) {  
23:        item.execute(that);      
24:            });    
25:       };    
26:       return Counter;  
27:  })();  

One of our Observers is meant for updating one DIV in HTML page:

1:  var CounterObserver = (function () {  
2:    function CounterObserver( aDOMId ) {  
3:      this.DOMId = aDOMId;    
4:       }  
5:    CounterObserver.prototype.execute = function( aCounter ) {  
6:      document.getElementById(this.DOMId).innerHTML = aCounter.getValue().toString();    
7:       };    
8:       return CounterObserver;  
9:  })();  
And the last is one Observer for a HTML Progress Bar:

1:  var ProgressBarCounterObserver = (function () {  
2:    function ProgressBarCounterObserver( aDOMId ) {  
3:      this.DOMId = aDOMId;      
4:            document.getElementById(aDOMId).value = 0;      
5:            document.getElementById(aDOMId).max = 100;    
6:       }  
7:    ProgressBarCounterObserver.prototype.execute = function( aCounter ) {  
8:      document.getElementById(this.DOMId).value = aCounter.getValue();    
9:       };    
10:       return ProgressBarCounterObserver;  
11:  })();  

The result is something like


Sem comentários:

Enviar um comentário