更新時間:2022-09-20 10:09:10 來源:動力節點 瀏覽1009次
同源策略加強了一些安全性,但不足以防止各種攻擊。他們之中有一些是:
跨站點請求偽造(CSRF)攻擊基本上利用了不同的來源。這就是為什么除了同源策略之外還應該使用反 CSRF 令牌的原因。
同源策略也可以防止跨站點腳本(XSS)攻擊,但為了防止它必須限制從外部源加載腳本,這可能會破壞 Web 應用程序的功能。
抽象工廠模式圍繞創建其他工廠的超級工廠工作。這個工廠也被稱為工廠的工廠。這種類型的設計模式屬于創建模式,因為這種模式提供了創建對象的最佳方法之一。
在抽象工廠模式中,接口負責創建相關對象的工廠,而無需明確指定它們的類。每個生成的工廠都可以按照工廠模式提供對象。
我們將創建一個 Shape 接口和一個實現它的具體類。我們創建一個抽象工廠類 AbstractFactory 作為下一步。定義了工廠類 ShapeFactory,它擴展了 AbstractFactory。創建了一個工廠創建者/生成器類 FactoryProducer。
AbstractFactoryPatternDemo,我們的演示類使用 FactoryProducer 來獲取 AbstractFactory 對象。它將信息(形狀的 CIRCLE / RECTANGLE / SQUARE)傳遞給 AbstractFactory 以獲取它需要的對象類型。
為 Shapes 創建一個界面。
public interface Shape {
void draw();
}
創建實現相同接口的具體類。
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.");
}
}
創建一個 Abstract 類以獲取 Normal 和 Rounded Shape 對象的工廠。
public abstract class AbstractFactory {
abstract Shape getShape(String shapeType) ;
}
創建工廠類擴展 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;
}
}
創建一個工廠生成器/生產者類,通過傳遞諸如 Shape 之類的信息來獲取工廠
public class FactoryProducer {
public static AbstractFactory getFactory(boolean rounded){
if(rounded){
return new RoundedShapeFactory();
}else{
return new ShapeFactory();
}
}
}
使用 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();
}
}
驗證輸出。
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside RoundedRectangle::draw() method.
Inside RoundedSquare::draw() method.
以上就是關于“抽象工廠模式實例”的介紹,大家如果對此比較感興趣,想了解更多相關知識,不妨來關注一下本站的Java設計模式技術文檔,里面還有更豐富的知識等著大家去學習,希望對大家能夠有所幫助哦。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習