Blog Ronald

Welcome to my Blog

Archive for the ‘Design Pattern’ Category

Observer Pattern

Posted by T. Ronald S. on February 26, 2009

Wait for update…

Posted in Design Pattern | Leave a Comment »

Factory Pattern

Posted by T. Ronald S. on February 26, 2009

Better use Factory Pattern when:

    1. The creation of object precludes reuse without significantly duplicating code
    2. The creation of object requires the access information or resource not appropriate to contain within the composing object
    3. The lifetime management of created objects needs to be centralised to ensure consistent behaviour

Examples:

public abstract class Connection {
public String description() {
return "generic";
}

public class OracleConnection extends Connection {
public String description() {
return "oracle";
}
}

public class MysqlConnection extends Connection {
public String description() {
return "mysql"
}
}

public class PostgreConnection extends Connection {
public String description() {
return "postgre"
}
}

public class FirstFactory {
protected String type;
public FirstFactory(String t) {
type = t;
}
public Connection createConnection() {
if(type.equals("oracle")) {
return new OracleConnection();
}else if(type.equals("mysql")) {
return new MysqlConnection();
}else {
return new PostgreConnection();
}
}

public class Main {
public static void main(String[] args) {
FirstFactory factory;
factory = new FirstFactory("oracle");
Connection connection = factory.createConnection();
System.out.println("You're connecting with " + connection.description());
}

}

 

Posted in Design Pattern | Leave a Comment »

Decorator Pattern

Posted by T. Ronald S. on February 26, 2009

The idea of this pattern is to allow new/additional behaviour to be added to an existing class dynamically.
Examples:

public class Computer {
public String description() {
return "a Computer";
}
}

public abstract Decorator extends Computer {
public abstract String description();
}

public class Disk extends Decorator {
private Computer computer;
public Disk(Computer c) {
computer = c;
}
public String description() {
return computer.description() + " and a Disk";
}

public class Monitor extends Decorator {
private Computer computer;
public Monitor(Computer c) {
computer = c;
}
public String description() {
return computer.description() + " and a Monitor";
}
}

public class Main {
public static void main(String[] args) {
Computer computer = new Computer();
computer = new Disk(computer);
computer = new Monitor(computer);
System.out.println("You're getting a " + computer.description());
}
}

Posted in Design Pattern | Leave a Comment »

Strategy Pattern

Posted by T. Ronald S. on February 26, 2009

The idea of this pattern is to encapsulate each algorithms as an object and make them interchangeable. below is examples of using strategy pattern in Java.

public interface GoAlgorithm{
public void go();
}
public class GoByDriving implements GoAlgorithm{
public void go(){
System.out.println("I'm Driving");
}
}
public class GoByFlying implements GoAlgorithm {
public void go(){
System.out.println("I'm flying");
}
}
public abstract Vehicle {
private GoAlgorithm goAlgorithm;
public void setGoAlgorithm(GoAlgorithm algorithm){
goAlgorithm = algorithm;
}
public void go() {
goAlgorithm.go();
}
}
public class Car extends Vehicle {
public Car(){
setGoAlgorithm(new GoByDriving());
}
}

public class Helicopter extends Vehicle {
public Helicopter() {
setGoAlgorithm(new GoByFlying());
}
}

public class Main {
public static void main(String[] args) {
Car c = new Car();
c.go();
Helicopter h = new Helicopter();
h.go();
}
}

Posted in Design Pattern | Leave a Comment »

Introduction

Posted by T. Ronald S. on February 26, 2009

Sometimes we experienced our codes are difficult to follow by our team mates, or sometimes we found same problems in programming but we forget the algorithm that we’ve used before. Design Pattern will help you write your codes well, easier for people who works with you to follow your code and help you to solve some common problems in your programs. I found there are 23 Design Patterns. I will try to digest and write some examples using JAVA.

Posted in Design Pattern | Leave a Comment »