Factory Design Pattern
generation 1 <- generation 2 <- generation 3//example(generation 1) shape <- (generation 2) rectangle, oval <- (generation 3) rectangle impressionism, rectangle surrealism, oval impressionism, oval surrealism
Factory
Use case: instantiate one object of generation 2
It is a Creational
pattern which allows you to create generation 2
in a simple place. It conforms SRP and OCP - all changes are made in a single class.
enum ShapeType { RECTANGLE, OVAL}class Shape {}//Concrete Products//generation 2class Rectangle extends Shape {}class Oval extends Shape {}//Factoryclass Factory { Shape createShape(ShapeType type) { switch (type) { case RECTANGLE: return new Rectangle(); case OVAL: return new Oval(); } }}//Creatorclass Painter { private Factory factory; Painter(Factory factory) { this.factory = factory; } Shape prepareShape(ShapeType type) { return factory.createShape(type); }}//usingclass Main { void main() { Painter painter = new Painter(new Factory()); Shape shape1 = painter.prepareShape(ShapeType.RECTANGLE); Shape shape2 = painter.prepareShape(ShapeType.OVAL); }}
Factory method
Use case: instantiate one object of generation 3
Helps to work with next generation of family members. Every painter has his own style like Impressionism, Surrealism... Factory Method
uses abstract Creator
as Factory(abstract method) and Concrete Creators
are realizations of this method
enum ShapeType { RECTANGLE, OVAL}class Shape {}//Concrete Products//generation 2class Rectangle extends Shape {}class Oval extends Shape {}//generation 3class RectangleImpressionism extends Rectangle {}class OvalImpressionism extends Oval {}class RectangleSurrealism extends Rectangle {}class OvalSurrealism extends Oval {}//Creatorabstract class Painter { Shape prepareShape(ShapeType type) { return createShape(type); } //Factory method abstract Shape createShape(ShapeType type);}//Concrete Creatorsclass PainterImpressionism { @override Shape createShape(ShapeType type) { switch (type) { case RECTANGLE: return new RectangleImpressionism(); case OVAL: return new OvalImpressionism(); } }}class PainterSurrealism { @override Shape createShape(ShapeType type) { switch (type) { case RECTANGLE: return new RectangleSurrealism(); case OVAL: return new OvalSurrealism(); } }}//usingclass Main { void main() { Painter painterImpressionism = new PainterImpressionism(); Shape shape1 = painterImpressionism.prepareShape(ShapeType.RECTANGLE); Painter painterSurrealism = new PainterSurrealism(); Shape shape2 = painterSurrealism.prepareShape(ShapeType.RECTANGLE); }}
Abstract Factory
Use case: instantiate all objects of generation 3
Factory
is a part of abstract Factory
and realisations in Concrete Factories
//Concrete Products//generation 2class Rectangle extends Shape {}class Oval extends Shape {}//generation 3class RectangleImpressionism extends Rectangle {}class OvalImpressionism extends Oval {}class RectangleSurrealism extends Rectangle {}class OvalSurrealism extends Oval {}//Abstract Factoryinterface Factory { Rectangle createRectangle(); Oval createOval();}//Concrete Factoriesclass ImpressionismFactory implements Factory { @Override public Rectangle createRectangle() { return new RectangleImpressionism(); } @Override public Oval createOval() { return new OvalImpressionism(); }}class SurrealismFactory implements Factory { @Override public Rectangle createRectangle() { return new RectangleSurrealism(); } @Override public Oval createOval() { return new OvalSurrealism(); }}//Creatorclass Painter { Rectangle rectangle; Oval oval; Painter(Factory factory) { rectangle = factory.createRectangle(); rectangle.resize(); oval = factory.createOval(); oval.resize(); }}//usingclass Main { void main() { Painter painter1 = new Painter(new ImpressionismFactory()); Shape shape1 = painter1.rectangle; Shape shape2 = painter1.oval; Painter painter2 = new Painter(new ImpressionismFactory()); Shape shape3 = painter2.rectangle; Shape shape4 = painter1.oval; }}