what is a factory class ?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   manikandan_c
Posted On:   Sunday, April 10, 2005 02:23 AM

in which package does the factory class come.... and what are the methods present in the factory class......

Re: what is a factory class ?

Posted By:   WarnerJan_Veldhuis  
Posted On:   Sunday, April 10, 2005 03:15 AM

I forgot the part about the methods. Usually, factories have static methods, so you don't have to instantiate the factory, but use methods directly:

class CarFactory {

public static Car createComplexCar(String brand) {
Car result = null;
if ( "Mercedes".equals(brand) ) {
result = new Mercedes();
result.assembleParts();
//....
}
else if ( "Honda".equals( brand) ) {
result = new Honda();
result.assembleParts();
//....
}

return result;
}

}

Re: what is a factory class ?

Posted By:   WarnerJan_Veldhuis  
Posted On:   Sunday, April 10, 2005 03:10 AM

A factory is not a class, but a design pattern. (google...). A Factory pattern is a way to create objects without the client knowing how it is done.



For example:

Car honda = CarFactory.createComplexCar("Honda");

Car mercedes = CarFactory.createComplexCar("Mercedes");



Now you have two car-objects, without knowing how it's done, which saves a lot of time, and hides the complexity.
About | Sitemap | Contact