更新時間: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)對象的工廠,而無需明確指定它們的類。每個生成的工廠都可以按照工廠模式提供對象。
我們將創(chuàng)建一個 Shape 接口和一個實(shí)現(xiàn)它的具體類。我們創(chuàng)建一個抽象工廠類 AbstractFactory 作為下一步。定義了工廠類 ShapeFactory,它擴(kuò)展了 AbstractFactory。創(chuàng)建了一個工廠創(chuàng)建者/生成器類 FactoryProducer。
AbstractFactoryPatternDemo,我們的演示類使用 FactoryProducer 來獲取 AbstractFactory 對象。它將信息(形狀的 CIRCLE / RECTANGLE / SQUARE)傳遞給 AbstractFactory 以獲取它需要的對象類型。
為 Shapes 創(chuàng)建一個界面。
public interface Shape {
void draw();
}
創(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.");
}
}
創(chuàng)建一個 Abstract 類以獲取 Normal 和 Rounded Shape 對象的工廠。
public abstract class AbstractFactory {
abstract Shape getShape(String shapeType) ;
}
創(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;
}
}
創(chuàng)建一個工廠生成器/生產(chǎn)者類,通過傳遞諸如 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();
}
}
驗(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í),希望對大家能夠有所幫助哦。
初級 202925
初級 203221
初級 202629
初級 203743