Archive for the Design Pattern with Java, C++ and Ruby Category

Observer Pattern

Posted in Design Pattern with Java, C++ and Ruby on May 30, 2009 by Asanga Bandara Wijekoon

First of all I need to emphasis that any pattern does not depend on a language and actually it is a powerful concept. Programmer should have the ability to format it according to the language going to use. My first explanation is based on Observer Pattern. This pattern use to implement to one-to-many relationship. Here we have a subject object and if its state is changed it inform to all the observer objects.  Let me explain it first using Java.
In Java we have pre implemented way of this pattern but I think its better to explain how we implement this pattern on our hand with better suiting to our interest and after that I explain how to use the lava pre-implemented pattern implementation.
First we define an interface for create concrete subject class. Here I need to note that we always try to code for an interface and not use concrete classes. This is good programming practice and it helps to manage code very much and dynamic object creation. Basically this interface and concrete class is for doing the a server kind of a job by managing the observers and they are some sort of clients.

public interface Subject {
public void registerObserver(Observer o);

/* observers are the clients who were been updating when subject status were changed*/
public void removeObserver(Observer o);
public void notifyObservers();
}

Now we are defining the ConcreteSubject class.
import java.util.*;
public class ConcreateSubject implements Subject {
private ArrayList observers;
public ConcreateSubject() {
observers = new ArrayList();
}
public void registerObserver(Observer o) {
observers.add(o);
}
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}

public void notifyObservers() {
for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer)observers.get(i);
observer.update();
}
}
}

Now we need to inform the updates to the observers. To implement that I used an interface named it as Observer. Here one to many relationship perform as one subject relate to many observers.

public interface Observer {
public void update();
}

Now we are implement it using concrete observer class.
public ConcreteObserver implements Observer {
public void update(){
System.out.println(“updated”);
}
}

So that’s it. Now we are considering the Java inbuilt observer pattern support. Resource classes are available in the java.util package. Here we have Observable interface works as the Subject interface in our previous example and Observer is as normal Observer as in our previous example. (source codes of below examples took from http://www.java2s.com/Code/Java/Design-Pattern/Observableandobserver.htm)

import java.util.Observable;
import java.util.Observer;

public class MessageBoard extends Observable {
private String message;

public String getMessage() {
return message;
}

public void changeMessage(String message) {
this.message = message;
setChanged();
notifyObservers(message);
}

public static void main(String[] args) {
MessageBoard board = new MessageBoard();
Student bob =  new Student();
Student joe = new Student();
board.addObserver(bob);
board.addObserver(joe);
board.changeMessage(“More Homework!”);
}
}

class Student implements Observer {
public void update(Observable o,  Object arg) {
System.out.println(“Message board changed: ” + arg);
}
}

Introduction For Pattern

Posted in Design Pattern with Java, C++ and Ruby on April 6, 2009 by Asanga Bandara Wijekoon

I hope to share my knowledge on design patterns with you and I will post series of posts on this topic. First I am going to discuss what is design pattern and why people use this and then list what are the design patterns I am going to cover here. I will explain each pattern using Java,c++ and Ruby example codes. Usually pattern will not depend on a language user of it should have to have the ability to use it with any programming language.

A design pattern is a solution well tested by thousands of programmers and it gives solution for a problem in a context (problem domain). When we are using pattern for solve a problem always should consider following fact.

“Don’t consider pattern first and push it to solve the problem by forcing”

Good designers are not try to push patterns for the solution by force manner and they apply pattern in natural way. When they design solutions them fit to some patterns and then they used them. They strongly recommend that don’t make more complex the solution by pushing patterns to it in an unnecessary manner. Anyway for simple system also, its good to use a pattern if it drive for changes in future because by using pattern we can easily change the code. Other than that I need to mention here about the “forces” of a design, which is a buzzword in system designing and it consists of problem and the constraints in the context. Finally in this post I list patterns I’m going to address in my future posts in below,

  1. Observer pattern
  2. Decorator pattern
  3. Factory pattern
  4. Singleton pattern
  5. Command pattern
  6. Adapter pattern
  7. Facade pattern
  8. Template Method pattern
  9. Iterator pattern
  10. Composite pattern
  11. State pattern
  12. Strategy pattern
  13. Proxy pattern
  14. Compound pattern