Monday, February 9, 2009

Abstract Factory Pattern

Abstract Factory Pattern

Saturday, February 7, 2009

Factory Pattern

This is a Creational pattern, so this pattern can be used to hide the object instantiation process.
Simply, if we have a super class and set of sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory design pattern to solve this.

Just get an idea by watching this video.



Let's think that we have set of shapes Line, Square, Circle and we need to draw the shape as per the request from the client. In other words based on the data provided by the client.

A Shape is an interface. Several implementing classes(Line, Square, Circle) may be designed in the following way.

//Shape interface which will be implemented by
Line, Square, Circle classes.
interface Shape {
public void draw();
}

//Line class implementation
class Line implements Shape {
Point x, y;
Line(Point a, Point b) {
x = a;
y = b;
}
public void draw() {
//draw a line;
}
}

//Square class implementation
class Square implements Shape {
Point start;
int width, height;
Square(Point s, int w, int h) {
start = s;
width = w;
height = h;
}
public void draw() {
//draw a square;
}
}

//Circle class implementation
class Circle implements Shape {
....
}

class ShapeFactory{
public getShape(){
Shape shape=null;
if(isLine){
shape =new Line(int x, int y);
}
else if(isSquare){
shape =new Square(int x, int y, int z);
}
else
shape =new Circle(int x, int y);
}
}

//Client calling code will be like this,
ShapeFactory fac=new ShapeFactory();
Shape s=fac.getShape(int x, int y)
s.draw();// this will draw a line

Shape s=fac.getShape(int x, int y, int z)
s.draw();// this will draw a Square

Shape s=fac.getShape(int x, int y)
s.draw();// this will draw a Circle

Please note that the above code is not complete.


Real World Example

To illustrate how to use factory design pattern with class level implementation, here is a real world example. A company has a website to display testing result from a plain text file. Recently, the company purchased a new machine which produces a binary data file, another new machine on the way, it is possible that one will produce different data file. How to write a system to deal with such change. The website just needs data to display. Your job is to provide the specified data format for the website.

Here comes a solution. Use an interface type to converge the different data file format. The following is a skeleton of implementation.

//Let's say the interface is Display
interface Display {

//load a file
public void load(String fileName);

//parse the file and make a consistent data type
public void formatConsistency();

}

//deal with plain text file
class CSVFile implements Display{

public void load(String textfile) {
System.out.println("load from a txt file");
}
public void formatConsistency() {
System.out.println("txt file format changed");
}
}

//deal with XML format file
class XMLFile implements Display {

public void load(String xmlfile) {
System.out.println("load from an xml file");
}
public void formatConsistency() {
System.out.println("xml file format changed");
}
}

//deal with binary format file
class DBFile implements Display {

public void load(String dbfile) {
System.out.println("load from a db file");
}
public void formatConsistency() {
System.out.println("db file format changed");
}
}

//Test the functionality
class TestFactory {

public static void main(String[] args) {
Display display = null;

//use a command line data as a trigger
if (args[0].equals("1"))
display = new CSVFile();
else if (args[0].equals("2"))
display = new XMLFile();
else if (args[0].equals("3"))
display = new DBFile();
else
System.exit(1);

//converging code follows
display.load("");
display.formatConsistency();
}
}
//after compilation and run it

C:\>java TestFactory 1
load from a txt file
txt file format changed

C:\>java TestFactory 2
load from an xml file
xml file format changed

C:\>java TestFactory 3
load from a db file
db file format changed

In the future, the company may add more data file with different format, a programmer just adds a new class in accordingly. Such design saves a lot of code and is easy to maintain.




Related links to Factory pattern

Intro to Design Patterns: Factory Method Pattern
Java Design Patterns Factory Method

Thursday, February 5, 2009

Introduction to Java Design Patterns


  • If a problem occurs over and over again, and that problem has been solved by experts and used effectively. Then that solution is described as a design pattern.
  • The design patterns are language-independent strategies for solving common object-oriented design problems.
  • If you want to be a professional Java developer, you should know at least some popular solutions to coding problems. Such solutions have been proved efficient and effective by the experienced developers.
  • Learning design patterns speeds up your experience accumulation in OOA/OOD. Once you grasped them, you would be benefit from them for all your life and jump up yourselves to be a master of designing and developing.




Some of the useful definitions are as follows,

"Design patterns constitute a set of rules describing how to accomplish
certain tasks in the realm of software development."

"A pattern addresses a recurring design problem that arises in specific
design situations and presents a solution to it"



How many design patterns?

  • The 23 design patterns by GOF are well known, and more are to be discovered on the way.
  • A site says at least 250 existing patterns are used in OO world, including Spaghetti which refers to poor coding habits.


Patterns: According to commonly known practices, there are 23 design patterns in Java. These patterns are grouped under three heads:

1. Creational Patterns
2. Structural Patterns
3. Behavioral Patterns


1. Creational Patterns

All of the creational patterns' responsibility is to abstract the object creational process and deal with the best way to create instances of objects. This is important because your program should not depend on how objects are created and arranged.

There are five types of Creational Patterns.
1. Factory Pattern
2. Abstract Factory Pattern
3. Builder Pattern
4. Prototype Pattern
5. Singleton Pattern


2. Structural Patterns

Structural patterns describe how classes and objects can be combined to form larger structures. The difference between class patterns and object patterns is that class patterns describe how inheritance can be used to provide more useful program interfaces. Object patterns, on the other hand, describe how objects can be composed into larger structures using object composition, or the inclusion of objects within other objects.

There are seven types of Structural Patterns.
1. Adapter Pattern
2. Bridge Pattern
3. Composite Pattern
4. Decorator Pattern
5. Facade Pattern
6. Flyweight Pattern
7. Proxy Pattern


3. Behavioral Patterns

Behavioral patterns are those which are concerned with interactions between the objects. The interactions between the objects should be such that they are talking to each other and still are loosely coupled. The loose coupling is the key to n-tier architectures. In this, the implementation and the client should be loosely coupled in order to avoid hard-coding and dependencies.
There are eleven types of Behavioral Patterns.
1. Chain of Resposibility Pattern
2. Command Pattern
3. Interpreter Pattern
4. Iterator Pattern
5. Mediator Pattern
6. Momento Pattern
7. Observer Pattern
8. State Pattern
9. Strategy Pattern
10. Template Pattern
11. Visitor Pattern


Go to Factory pattern >>

Followers