Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is Abstract factory pattern with example?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 787
    Comment on it

    We know that factory pattern provides single level abstraction and used to create object easily but in a case of more security, we can use one more level of abstraction by using the abstract factory pattern. we can say that this provides factory of factories.

    Example :

    1. Create interfaces :

    1. public interface Shape {
    2. public void draw(String shapeType);
    3. }
    4.  
    5.  
    6. public interface Color {
    7. public void color(String fillColor);
    8. }

     

    2. Create rectangle and circle type class :

    1. public class Rectangle implements Shape, Color{
    2.  
    3. @Override
    4. public void color(String fillColor) {
    5. System.out.println("fill color :" + fillColor);
    6. }
    7.  
    8. @Override
    9. public void draw(String shapeType) {
    10. System.out.println("shape type :" + shapeType);
    11. }
    12.  
    13. }
    14.  
    15.  
    16. public class Circle implements Shape, Color{
    17.  
    18. @Override
    19. public void color(String fillColor) {
    20. System.out.println("fill color :" + fillColor);
    21. }
    22.  
    23. @Override
    24. public void draw(String shapeType) {
    25. System.out.println("shape type :" + shapeType);
    26. }
    27. }

     

    3.Create Abstract Factory class

    1. public abstract class AbstractFactory {
    2. abstract Shape getShape(String type);
    3. abstract Color getColor(String color);
    4. }


    4. Create Shape factory that 'll define shape :

    1. public class ShapeFactory extends AbstractFactory{
    2.  
    3. @Override
    4. Shape getShape(String type) {
    5. if (type.equalsIgnoreCase("rectangle")) {
    6. return new Rectangle();
    7. }else if (type.equalsIgnoreCase("circle")) {
    8. return new Circle();
    9. }
    10. return null;
    11. }
    12.  
    13. @Override
    14. Color getColor(String color) {
    15. return null;
    16. }
    17.  
    18. }
    19.  
    20.  
    21. and then color factory to define color :
    22.  
    23.  
    24. public class ColorFactory extends AbstractFactory{
    25.  
    26. @Override
    27. Shape getShape(String type) {
    28. return null;
    29. }
    30.  
    31. @Override
    32. Color getColor(String color) {
    33. if (color.equalsIgnoreCase("red")) {
    34. return new Rectangle();
    35. }else if (color.equalsIgnoreCase("orange")) {
    36. return new Circle();
    37. }
    38. return null;
    39. }
    40.  
    41. }

     

    5.Now create factory provider that ll provide type of factory based on given information :

    1. public class FactoryProvider {
    2. public static AbstractFactory getFactory(String factoryFor){
    3. if (factoryFor.equalsIgnoreCase("shape")) {
    4. return new ShapeFactory();
    5. }else if (factoryFor.equalsIgnoreCase("color")) {
    6. return new ColorFactory();
    7. }
    8. return null;
    9. }
    10. }

     

    6. In Main :

    1. public class AbstractFactoryDemoClass {
    2.  
    3. public static void main(String[] args) {
    4. AbstractFactory abstractFactoryShape = FactoryProvider.getFactory("shape");
    5. Shape shapeRectangle = abstractFactoryShape.getShape("rectangle");
    6. Shape shapeCircle = abstractFactoryShape.getShape("circle");
    7. shapeRectangle.draw("rectangle");
    8. shapeCircle.draw("circle");
    9. AbstractFactory abstractFactoryColor = FactoryProvider.getFactory("color");
    10. Color colorRectangle = abstractFactoryColor.getColor("red");
    11. Color colorCircle = abstractFactoryColor.getColor("orange");
    12. colorRectangle.color("red");
    13. colorCircle.color("orange");
    14. }
    15.  
    16. }


    You can get Factory using Factory provider by giving Shape type then u ll have Abstract Factory to generate Shape object and color object.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: