Vous êtes sur la page 1sur 13

Abstract Factory

Definition
Provides one level of interface higher than the factory pattern. It is used to return one of several factories.

Where to use & benefits


Creates families of related or dependent objects like Kit. Provides a class library of products, exposing interface not implementation. Needs to isolate concrete classes from their super classes. A system needs independent of how its products are created, composed, and represented. Try to enforce a constraint. An alternative to Facade to hide platform-specific classes Easily extensible to a system or a family Related patterns include o Factory method, which is often implemented with an abstract factory. o Singleton, which is often implemented with an abstract factory. o Prototype, which is often implemented with an abstract factory. o faade, which is often used with an abstract factory by providing an interface for creating implementing class.

Example
Suppose you need to write a program to show data in two different places. Let's say from a local or a remote database. You need to make a connection to a database before working on the data. In this case, you have two choices, local or remote. You may use abstract factory design pattern to design the interface in the following way:

class DataInfo {} interface Local { DataInfo[] loadDB(String filename); } interface Remote extends Local{ void connect2WWW(String url); }

class LocalMode implements Local { public DataInfo[] loadDB(String name) { System.out.print("Load from a local database "); return null; } } class RemoteMode implements Remote { public void connect2WWW(String url) { System.out.println("Connect to a remote site "); } public DataInfo[] loadDB(String name) { System.out.println("Load from a remote database "); return null; } } // The Abstract Factory interface ConnectionFactory { Local getLocalConnection(); Remote getRemoteConnection(); } class DataManager implements ConnectionFactory { boolean local = false; DataInfo[] data; //... public Local getLocalConnection() { return new LocalMode(); } public Remote getRemoteConnection() { return new RemoteMode(); } public void loadData() { if(local){ Local conn = getLocalConnection(); data = conn.loadDB("db.db"); }else { Remote conn = getRemoteConnection(); conn.connect2WWW("www.some.where.com"); data = conn.loadDB("db.db"); } } // work on data public void setConnection(boolean b) { local = b; } } //Use the following Test class to test the above classes class Test { public static void main(String[] args) { DataManager dm = new DataManager(); DataInfo[] di = null; String dbFileName = "db.db"; if (args.length == 1) { //assume local is set to true dm.setConnection(true); LocalMode lm = (LocalMode)dm.getLocalConnection();

di = lm.loadDB(dbFileName); } else { //Note: dm.local = false is default setting RemoteMode rm = (RemoteMode)dm.getRemoteConnection(); rm.connect2WWW("www.javacamp.org/db/"); di = rm.loadDB(dbFileName); } //use one set of methods to deal with loaded data. //You don't need to worry about connection from this point. //Like di.find(), di.search() etc. } }

C:\ Command Prompt


C:\> java Test Connect to a remote site Load from a remote database

C:\ Command Prompt


C:\> java Test local Load from a local database

Such design is often used in SCJD project assignment. If you have a multiple places to load data, you just add more methods in the connection interface without altering other structure, or add a location variable in.
Factory pattern is one of most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

Implementation
We're going to create a Shape interface and concrete classes implementing the Shape interface. A factory class ShapeFactory is defined as a next step. FactoryPatternDemo, our demo class will use ShapeFactory to get a Shape object. It will pass information (CIRCLE / RECTANGLE / SQUARE) to ShapeFactory to get the type of object it needs.

Step 1
Create an interface. Shape.java public interface Shape { void draw(); }

Step 2
Create concrete classes implementing the same interface. Rectangle.java public class Rectangle implements Shape {

@Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } Square.java public class Square implements Shape {

@Override public void draw() { System.out.println("Inside Square::draw() method."); } } Circle.java public class Circle implements Shape {

@Override public void draw() { System.out.println("Inside Circle::draw() method."); } }

Step 3
Create a Factory to generate object of concrete class based on given information. ShapeFactory.java public class ShapeFactory {

//use getShape method to get object of type shape public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } }

Step 4

Use the Factory to get object of concrete class by passing an information such as type. FactoryPatternDemo.java public class FactoryPatternDemo {

public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory();

//get an object of Circle and call its draw method. Shape shape1 = shapeFactory.getShape("CIRCLE");

//call draw method of Circle shape1.draw();

//get an object of Rectangle and call its draw method. Shape shape2 = shapeFactory.getShape("RECTANGLE");

//call draw method of Rectangle shape2.draw();

//get an object of Square and call its draw method. Shape shape3 = shapeFactory.getShape("SQUARE");

//call draw method of circle shape3.draw(); } }

Step 5
Verify the output. Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method.

Abstract Factory patterns works around a super-factory which creates other factories. This factory is also called as Factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

In Abstract Factory pattern an interface is responsible for creating a factory of related objects, without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.

Implementation
We're going to create a Shape and Color interfaces and concrete classes implementing these interfaces. We creates an abstract factory class AbstractFactory as next step. Factory classes ShapeFactory and ColorFactory are defined where each factory extends AbstractFactory. A factory creator/generator class FactoryProducer is created. AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get a AbstractFactory object. It will pass information (CIRCLE / RECTANGLE / SQUARE for Shape) to AbstractFactory to get the type of object it needs. It also passes information (RED / GREEN / BLUE for Color) to AbstractFactory to get the type of object it needs.

Step 1
Create an interface for Shapes. Shape.java public interface Shape { void draw(); }

Step 2
Create concrete classes implementing the same interface.

Rectangle.java public class Rectangle implements Shape {

@Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } Square.java public class Square implements Shape {

@Override public void draw() { System.out.println("Inside Square::draw() method."); } } Circle.java public class Circle implements Shape {

@Override public void draw() { System.out.println("Inside Circle::draw() method."); } }

Step 3
Create an interface for Colors. Color.java public interface Color { void fill(); }

Step4
Create concrete classes implementing the same interface.

Red.java public class Red implements Color {

@Override public void fill() { System.out.println("Inside Red::fill() method."); } } Green.java public class Green implements Color {

@Override public void fill() { System.out.println("Inside Green::fill() method."); } } Blue.java public class Blue implements Color {

@Override public void fill() { System.out.println("Inside Blue::fill() method."); } }

Step 5
Create an Abstract class to get factories for Color and Shape Objects. AbstractFactory.java public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape) ; }

Step 6

Create Factory classes extending AbstractFactory to generate object of concrete class based on given information. ShapeFactory.java public class ShapeFactory extends AbstractFactory {

@Override public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; }

@Override Color getColor(String color) { return null; } } ColorFactory.java public class ColorFactory extends AbstractFactory {

@Override public Shape getShape(String shapeType){ return null; }

@Override Color getColor(String color) { if(color == null){ return null; }

if(color.equalsIgnoreCase("RED")){ return new Red(); } else if(color.equalsIgnoreCase("GREEN")){ return new Green(); } else if(color.equalsIgnoreCase("BLUE")){ return new Blue(); } return null; } }

Step 7
Create a Factory generator/producer class to get factories by passing an information such as Shape or Color FactoryProducer.java public class FactoryProducer { public static AbstractFactory getFactory(String choice){ if(choice.equalsIgnoreCase("SHAPE")){ return new ShapeFactory(); } else if(choice.equalsIgnoreCase("COLOR")){ return new ColorFactory(); } return null; } }

Step 8
Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing an information such as type. AbstractFactoryPatternDemo.java public class AbstractFactoryPatternDemo { public static void main(String[] args) {

//get shape factory AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

//get an object of Shape Circle Shape shape1 = shapeFactory.getShape("CIRCLE");

//call draw method of Shape Circle shape1.draw();

//get an object of Shape Rectangle Shape shape2 = shapeFactory.getShape("RECTANGLE");

//call draw method of Shape Rectangle shape2.draw();

//get an object of Shape Square Shape shape3 = shapeFactory.getShape("SQUARE");

//call draw method of Shape Square shape3.draw();

//get color factory AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

//get an object of Color Red Color color1 = colorFactory.getColor("RED");

//call fill method of Red color1.fill();

//get an object of Color Green Color color2 = colorFactory.getColor("Green");

//call fill method of Green color2.fill();

//get an object of Color Blue Color color3 = colorFactory.getColor("BLUE");

//call fill method of Color Blue color3.fill(); } }

Step 9
Verify the output. Inside Inside Inside Inside Inside Inside Circle::draw() method. Rectangle::draw() method. Square::draw() method. Red::fill() method. Green::fill() method. Blue::fill() method.

Vous aimerez peut-être aussi