专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 抽象工厂模式实例

抽象工厂模式实例

更新时间:2022-09-20 10:09:10 来源:动力节点 浏览548次

同源政策是否足够?

同源策略加强了一些安全性,但不足以防止各种攻击。他们之中有一些是:

跨站点请求伪造(CSRF)攻击基本上利用了不同的来源。这就是为什么除了同源策略之外还应该使用反 CSRF 令牌的原因。

同源策略也可以防止跨站点脚本(XSS)攻击,但为了防止它必须限制从外部源加载脚本,这可能会破坏 Web 应用程序的功能。

抽象工厂模式围绕创建其他工厂的超级工厂工作。这个工厂也被称为工厂的工厂。这种类型的设计模式属于创建模式,因为这种模式提供了创建对象的最佳方法之一。

在抽象工厂模式中,接口负责创建相关对象的工厂,而无需明确指定它们的类。每个生成的工厂都可以按照工厂模式提供对象。

执行

我们将创建一个 Shape 接口和一个实现它的具体类。我们创建一个抽象工厂类 AbstractFactory 作为下一步。定义了工厂类 ShapeFactory,它扩展了 AbstractFactory。创建了一个工厂创建者/生成器类 FactoryProducer。

AbstractFactoryPatternDemo,我们的演示类使用 FactoryProducer 来获取 AbstractFactory 对象。它将信息(形状的 CIRCLE / RECTANGLE / SQUARE)传递给 AbstractFactory 以获取它需要的对象类型。

步骤1

为 Shapes 创建一个界面。

public interface Shape {
   void draw();
}

步骤2

创建实现相同接口的具体类。

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

步骤3

创建一个 Abstract 类以获取 Normal 和 Rounded Shape 对象的工厂。

public abstract class AbstractFactory {
   abstract Shape getShape(String shapeType) ;
}

步骤4

创建工厂类扩展 AbstractFactory 以根据给定信息生成具体类的对象。

public class ShapeFactory extends AbstractFactory {
   @Override
   public Shape getShape(String shapeType){    
      if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();         
      }else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }	 
      return null;
   }
}
public class RoundedShapeFactory extends AbstractFactory {
   @Override
   public Shape getShape(String shapeType){    
      if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new RoundedRectangle();         
      }else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new RoundedSquare();
      }	 
      return null;
   }
}

步骤5

创建一个工厂生成器/生产者类,通过传递诸如 Shape 之类的信息来获取工厂

public class FactoryProducer {
   public static AbstractFactory getFactory(boolean rounded){   
      if(rounded){
         return new RoundedShapeFactory();         
      }else{
         return new ShapeFactory();
      }
   }
}

步骤6

使用 FactoryProducer 获取 AbstractFactory 以便通过传递类型等信息来获取具体类的工厂。

public class AbstractFactoryPatternDemo {
   public static void main(String[] args) {
      //get shape factory
      AbstractFactory shapeFactory = FactoryProducer.getFactory(false);
      //get an object of Shape Rectangle
      Shape shape1 = shapeFactory.getShape("RECTANGLE");
      //call draw method of Shape Rectangle
      shape1.draw();
      //get an object of Shape Square 
      Shape shape2 = shapeFactory.getShape("SQUARE");
      //call draw method of Shape Square
      shape2.draw();
      //get shape factory
      AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true);
      //get an object of Shape Rectangle
      Shape shape3 = shapeFactory1.getShape("RECTANGLE");
      //call draw method of Shape Rectangle
      shape3.draw();
      //get an object of Shape Square 
      Shape shape4 = shapeFactory1.getShape("SQUARE");
      //call draw method of Shape Square
      shape4.draw();      
   }
}

步骤7

验证输出。

Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside RoundedRectangle::draw() method.
Inside RoundedSquare::draw() method.

以上就是关于“抽象工厂模式实例”的介绍,大家如果对此比较感兴趣,想了解更多相关知识,不妨来关注一下本站的Java设计模式技术文档,里面还有更丰富的知识等着大家去学习,希望对大家能够有所帮助哦。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>