抽象工厂模式说明 [英] Abstract Factory Pattern Explanation

查看:104
本文介绍了抽象工厂模式说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究设计模式,遇到了抽象工厂模式,其定义为:

I was studying the design patterns and came across Abstract Factory Pattern which by definition is :


Abstract Factory Pattern表示仅定义一个接口或
抽象类来创建相关(或从属)对象
的族,但不指定其具体子类。 b Factory让一个类返回一个类的工厂。

Abstract Factory Pattern says that just define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes.That means Abstract Factory lets a class returns a factory of classes.

但是我无法完全理解它。我什至查看了此链接这个问题,但没有任何帮助。

But I am not able to understand it thoroughly. I even went through some examples given in this link and this question, but nothing helped.

任何人都可以提供清楚的信息吗? 抽象工厂模式的简单,真实的示例进行解释,并说明应该使用这种设计模式的情况。

Can anyone provide a clear explanation with a simple, real life example of Abstract Factory Pattern and the cases in which we should use this design pattern.

推荐答案

这是Abstract工厂模式的流程。它在java中实现

This is the flow of the Abstract factory pattern.Its implemented in java

//创建形状接口和实现者类形状

//create a shape interface and implementer classes shape

public interface Shape {
   void draw();
}

public class Rectangle implements Shape {

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

public class Square implements Shape {

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

//创建颜色界面及其实现者

//create color interface and its implementers

public interface Color {
   void fill();
}

public class Red implements Color {

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


public class Blue implements Color {

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

///创建通常是一个类的抽象工厂类会生成界面或以简单的语言显示的工厂可以制造您要求的任何东西

//create abstract factory class that is generally a class that generates the interfaces or in easy language a factory that can manufacture anything you ask of it

public abstract class AbstractFactory {
   abstract Color getColor(String color);
   abstract Shape getShape(String shape) ;
}

///创建形状工厂,就像您知道普通工厂制造东西一样。这是制造形状的工厂。您只要给它提供所需形状的名称,它就会制造出来

//create shape factory just like you know normal factories manufacture things. This is the factory that manufactures shapes.you just give it the name of the shape you want and it will manufacture it

public class ShapeFactory extends AbstractFactory {

   @Override
   public Shape getShape(String shapeType){

      if(shapeType == null){
         return null;
      }     

      if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();

      }else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }

      return null;
   }

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

// color factory。这是制造工厂颜色。您只需给它提供所需颜色的名称,它就会制造出来

//color factory .This is the factory that manufactures colors . you just give it the name of the color you want and it will manufacture it

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("BLUE")){
         return new Blue();
      }

      return null;
   }
}

//生产工厂。现在的此类就像建造工厂的投资者。

//produces factories .Now this class is like an investor who constructs factories . give it the name and it will construct for you the factory that manufactures that.

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;
   }
}

//这是像经销商一样的演示班会要求投资者建立一个形状工厂
,然后该工厂可以制造矩形,正方形等。

//This is the demo class like the dealer who would request an investor to construct a shape factory and this factory can then manufacture rectangles, squares etc.

public class AbstractFactoryPatternDemo {
   public static void main(String[] args) {

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


      //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 Blue
      Color color3 = colorFactory.getColor("BLUE");

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

这篇关于抽象工厂模式说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆