Factory Method is a creational design pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. It solves the problem of having to specify the exact class of an object that will be created, allowing for greater flexibility and code reusability.
There are several benefits to using the Factory Method pattern:
Factory Method is a creational design pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. It solves the problem of having to specify the exact class of an object that will be created, allowing for greater flexibility and code reusability.
There are several benefits to using the Factory Method pattern:
The Factory Method pattern works by defining an interface for creating objects. This interface is then implemented by subclasses that define the specific type of object that will be created. The client code then uses the factory method to create objects without having to specify the exact class of the object that will be created.
The Factory Method pattern is useful in the following situations:
Here is an example of how to use the Factory Method pattern in Java:
public interface ShapeFactory {
Shape getShape(String shapeType);
}
public class ShapeFactoryImpl implements ShapeFactory {
@Override
public Shape getShape(String shapeType) {
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new Square();
} else {
return null;
}
}
}
public class Shape {
// Common methods for all shapes
}
public class Circle extends Shape {
// Specific methods for circles
}
public class Rectangle extends Shape {
// Specific methods for rectangles
}
public class Square extends Shape {
// Specific methods for squares
}
public class Client {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactoryImpl();
Shape circle = shapeFactory.getShape("CIRCLE");
Shape rectangle = shapeFactory.getShape("RECTANGLE");
Shape square = shapeFactory.getShape("SQUARE");
// Use the shapes as needed
}
}
The Factory Method pattern is a creational design pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. It is a useful pattern when you need to create a family of related objects without having to specify the exact class of each object, when you want to decouple client code from concrete classes, when you want to promote code reusability, and when you want to support extensibility.
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.