黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 抽象工廠模式實(shí)例

抽象工廠模式實(shí)例

更新時間:2022-09-20 10:09:10 來源:動力節(jié)點(diǎn) 瀏覽1021次

同源政策是否足夠?

同源策略加強(qiáng)了一些安全性,但不足以防止各種攻擊。他們之中有一些是:

跨站點(diǎn)請求偽造(CSRF)攻擊基本上利用了不同的來源。這就是為什么除了同源策略之外還應(yīng)該使用反 CSRF 令牌的原因。

同源策略也可以防止跨站點(diǎn)腳本(XSS)攻擊,但為了防止它必須限制從外部源加載腳本,這可能會破壞 Web 應(yīng)用程序的功能。

抽象工廠模式圍繞創(chuàng)建其他工廠的超級工廠工作。這個工廠也被稱為工廠的工廠。這種類型的設(shè)計模式屬于創(chuàng)建模式,因?yàn)檫@種模式提供了創(chuàng)建對象的最佳方法之一。

在抽象工廠模式中,接口負(fù)責(zé)創(chuàng)建相關(guān)對象的工廠,而無需明確指定它們的類。每個生成的工廠都可以按照工廠模式提供對象。

執(zhí)行

我們將創(chuàng)建一個 Shape 接口和一個實(shí)現(xiàn)它的具體類。我們創(chuàng)建一個抽象工廠類 AbstractFactory 作為下一步。定義了工廠類 ShapeFactory,它擴(kuò)展了 AbstractFactory。創(chuàng)建了一個工廠創(chuàng)建者/生成器類 FactoryProducer。

AbstractFactoryPatternDemo,我們的演示類使用 FactoryProducer 來獲取 AbstractFactory 對象。它將信息(形狀的 CIRCLE / RECTANGLE / SQUARE)傳遞給 AbstractFactory 以獲取它需要的對象類型。

步驟1

為 Shapes 創(chuàng)建一個界面。

public interface Shape {
   void draw();
}

步驟2

創(chuàng)建實(shí)現(xiàn)相同接口的具體類。

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

創(chuàng)建一個 Abstract 類以獲取 Normal 和 Rounded Shape 對象的工廠。

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

步驟4

創(chuàng)建工廠類擴(kuò)展 AbstractFactory 以根據(jù)給定信息生成具體類的對象。

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

創(chuàng)建一個工廠生成器/生產(chǎn)者類,通過傳遞諸如 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

驗(yàn)證輸出。

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

以上就是關(guān)于“抽象工廠模式實(shí)例”的介紹,大家如果對此比較感興趣,想了解更多相關(guān)知識,不妨來關(guān)注一下本站的Java設(shè)計模式技術(shù)文檔,里面還有更豐富的知識等著大家去學(xué)習(xí),希望對大家能夠有所幫助哦。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 日本mv精品中文字幕 | 亚洲精品高清国产一线久久97 | 国产高清视频免费 | 日韩深夜福利 | 大杳焦伊人久久综合热 | 免费看一级黄色 | 亚洲男女在线 | 午夜精品久久久久久影视riav | 欧美黄色大片免费 | 午夜视频在线观看国产www | 色综合在 | 午夜免费观看_视频在线观看 | 国产污污视频 | 美女激情影院午夜网 | 成人午夜毛片 | 无毒不卡在线播放 | 高清欧美一区二区免费影视 | 丝袜视频在线 | free 中国性xxxxhd | 欧美在线影院 | 午夜剧场免费看 | 在线成人免费观看 | 国产精品视频免费视频 | 欧美一区二区三区视视频 | 美女视频永久黄网站免费观看韩国 | 法国18sexxxx性xxx| 亚洲天堂网站 | 日韩一区二区三区在线观看 | 欧美精品伊人久久 | 能看黄的网站 | 在线色站 | 91短视频版高清在线观看免费 | 伊人精品国产 | 免费大片av手机看片 | 好男人www社区视频在线 | 免费亚洲视频 | 最近中文国语字幕在线播放视频 | 在线综合+亚洲+欧美中文字幕 | 老司机午夜性大片免费 | 久操精品视频 | 99在线视频精品 |