ABSTRACT FACTORY ABSTRACT FACTORY Create instances of classes
ABSTRACT FACTORY
ABSTRACT FACTORY • • • Create instances of classes belonging to different families. 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.
Abstract 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 Abstract. Factory as next step. Factory classes. Shape. Factory and Color. Factory are defined where each factory extends Abstract. Factory. A factory creator/generator class Factory. Producer is created. ü Abstract. Factory. Pattern. Demo, our demo class uses Factory. Producer to get a Abstract. Factory object. It will pass information (CIRCLE / RECTANGLE / SQUARE for Shape) to Abstract. Factory to get the type of object it needs. It also passes information (RED / GREEN / BLUE for Color) to Abstract. Factory to get the type of object it needs.
Steps for Implementation 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. "); } }
Create concrete class implementing the same interface. Square. java public class Square implements Shape { @Override public void draw() { System. out. println("Inside Square: : draw() method. "); } } Create concrete class implementing the same interface. Circle. java public class Circle implements Shape { @Override public void draw() { System. out. println("Inside Circle: : draw() method. "); } }
Step 3 - Create an interface. – Color. java public interface color { void fill(); } Step 4 - Create concrete classes implementing the same interface.
Step 5 - Create an Abstract class to get factories for Color and Shape Objects. Abstract. Factory. java Step 6 - Create Factory classes extending Abstract. Factory to generate object of concrete class based on given information. - Shape. Factory. java
Color. Factory. java
Step 7 - Create a Factory generator/producer class to get factories by passing an information such as Shape or Color.
Step 7 - Use the Factory. Producer to get Abstract. Factory in order to get factories of concrete classes by passing information such as type Abstract. Factory. Pattern. Demo. java
Pros. /cons. Pros: • Shields clients from concrete classes. • Easy to switch product family at run-time, just change concrete factory. Cons: • Adding a new product means changing factory interface and all concrete factories.
- Slides: 15