Posts

Showing posts from November, 2017

Creating our own factory method in Java

Image
Creating our own factory method in Java Any factory method is created as a method belonging to an interface or abstract class. Hence that method is implemented, in the implementation classes or sub classes as case may be.

What are factory methods ?A factory method is a method that creates and returns an object to the class to which it belongs. A single factory  method replaces several constructors in the class by accepting different options from the user , while creating the object.



For example, to create a factory method getFees() that will give the fees details for a course in an engineering college, we need to perform the following steps :

1> create an interface or abstract class
interface Fees {
   double showFees();
}

2>  Implement the abstract , public methods of the above interface.
class CSE implements Fees {
public double showFees(){
return 120000; // assumed some constant figure
}
}
// their can be more implementation classes also.

3> Create a factory class that contains fac…