原文地址:https://dzone.com/articles/factory-pattern-using-lambda-exPRession-in-java-8?utm_source=Top%205&utm_medium=email&utm_campaign=2017-03-04
工廠模式是Java中最常用的設(shè)計(jì)模式之一。 這種類型的設(shè)計(jì)模式屬于主機(jī)的創(chuàng)造模式,因?yàn)檫@種模式提供了一種最好的方式來創(chuàng)建一個(gè)對(duì)象。 工廠設(shè)計(jì)模式允許您創(chuàng)建對(duì)象,而不將實(shí)例化邏輯暴露給客戶端。
在這篇文章中,我想給出一個(gè)Factory模式的示例,然后使用在Java 8中引入的lambda表達(dá)式重寫相同的示例。
我將創(chuàng)建一個(gè)Shape接口以及實(shí)現(xiàn)Shape接口的具體類。 一個(gè)工廠類ShapeFactory被定義為下一步。
創(chuàng)建一個(gè)接口: Shape.java
public interface Shape { void draw();}考慮這個(gè)Shape接口的兩個(gè)實(shí)現(xiàn): Circle.java & Rectangle.java
public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }}創(chuàng)建工廠根據(jù)給定信息生成具體類的對(duì)象。
public class ShapeFactory { //use getShape method to get object of type shape public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } return null; }}使用Factory通過傳遞類型為: FactoryPatternDemo.java的信息來獲取具體類的對(duì)象
public class FactoryPatternDemo { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); //get an object of Circle and call its draw method. Shape shape1 = shapeFactory.getShape("CIRCLE"); //call draw method of Circle shape1.draw(); //get an object of Rectangle and call its draw method. Shape shape2 = shapeFactory.getShape("RECTANGLE"); //call draw method of Rectangle shape2.draw(); }}驗(yàn)證輸出:
Inside Circle::draw() method.Inside Rectangle::draw() method.工廠模式:使用Lambda表達(dá)式的重構(gòu)
在Java 8中,我們可以參考構(gòu)造函數(shù),就像我們引用方法一樣,通過使用 方法引用。 例如,下面是如何引用Circle構(gòu)造函數(shù):
Supplier<Shape> circleSupplier = Circle::new;Circle circle = circleSupplier.get();使用這種技術(shù),我們可以通過創(chuàng)建映射形狀的Map來重寫前面的代碼 名稱到其構(gòu)造函數(shù):
final static Map<String, Supplier<Shape>> map = new HashMap<>(); static { map.put("CIRCLE", Circle::new); map.put("RECTANGLE", Rectangle::new); }我們現(xiàn)在可以使用這個(gè)Map來實(shí)例化不同的形狀,就像我們以前的工廠代碼一樣:
public class ShapeFactory { final static Map<String, Supplier<Shape>> map = new HashMap<>(); static { map.put("CIRCLE", Circle::new); map.put("RECTANGLE", Rectangle::new); } public Shape getShape(String shapeType){ Supplier<Shape> shape = map.get(shapeType.toUpperCase()); if(shape != null) { return shape.get(); } throw new IllegalArgumentException("No such shape " + shapeType.toUpperCase()); }}使用工廠獲得具體類的對(duì)象,使用lambda表達(dá)式: FactoryPatternDemo.java
public class FactoryPatternDemo { public static void main(String[] args) { Supplier<ShapeFactory> shapeFactory = ShapeFactory::new; //call draw method of circle shapeFactory.get().getShape("circle").draw(); //call draw method of Rectangle shapeFactory.get().getShape("rectangle").draw(); }}驗(yàn)證輸出:
Inside Circle::draw() method.Inside Rectangle::draw() method.這是一個(gè)相當(dāng)整潔的方式使用Java 8功能來實(shí)現(xiàn)相同的意圖工廠模式。 但是這種技術(shù)不能很好地縮放,如果工廠方法getShape需要采取多個(gè)參數(shù)傳遞給Shape構(gòu)造函數(shù)! 你必須提供與簡(jiǎn)單的供應(yīng)商不同的功能接口。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注