
前言

倉(cāng)山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),倉(cāng)山網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為倉(cāng)山成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的倉(cāng)山做網(wǎng)站的公司定做!
工廠模式是面向?qū)ο笤O(shè)計(jì)模式中大家最為熟知的設(shè)計(jì)模式之一。傳統(tǒng)的實(shí)現(xiàn)方式大家都在熟悉不過(guò)了,今天將向大家介紹使用Java8 Lambda 表達(dá)式更加優(yōu)雅的實(shí)現(xiàn)工廠模式。
封面
工廠模式在java中最常用的設(shè)計(jì)模式之一,它提供了一種很好的實(shí)例化對(duì)象的方法,是替代new操作的一種模式常用的方式。工廠設(shè)計(jì)模式可以讓你實(shí)例化對(duì)象的邏輯不用暴露給客戶端。
在下面的文章中我將給出使用傳統(tǒng)的代碼實(shí)現(xiàn)工廠模式的一個(gè)例子,然后再使用 Java8 Lambada 方式重新實(shí)現(xiàn)
一個(gè)例子
首先我將創(chuàng)建一個(gè) Shape 接口以及幾個(gè)實(shí)現(xiàn)類(lèi),然后會(huì)在接下來(lái)的步驟中實(shí)現(xiàn)ShapeFactory,最后會(huì)給出詳細(xì)的調(diào)用實(shí)例并輸出結(jié)果。
新建接口:Shape.java
public interface Shape {
void draw();
}定義兩個(gè) Shape的實(shí)現(xiàn)類(lèi):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)建Shape的工廠類(lèi),并實(shí)現(xiàn)根據(jù)指定參數(shù)返回不同的Shape的工廠方法:
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;
}
}ShapeFactory 根據(jù)傳入的shapeType 返回不同的Shape。
下面是具體使用的例子。
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();
}
}程序輸出
Inside Circle::draw() method. Inside Rectangle::draw() method.
使用Lambada實(shí)現(xiàn)工廠模式
Lambda表達(dá)式允許我們定義一個(gè)匿名方法,并允許我們以函數(shù)式接口的方式使用它。我們也希望能夠在已有的方法上實(shí)現(xiàn)同樣的特性。
方法引用和lambda表達(dá)式擁有相同的特性(例如,它們都需要一個(gè)目標(biāo)類(lèi)型,并需要被轉(zhuǎn)化為函數(shù)式接口的實(shí)例),不過(guò)我們并不需要為方法引用提供方法體,我們可以直接通過(guò)方法名稱(chēng)引用已有方法。
下面例子展示了構(gòu)造方法引用
Supplier circleSupplier = Circle::new; Circle circle = circleSupplier.get();
根據(jù)構(gòu)造方法引用的原理,我們可以重寫(xiě)之前的代碼,定義一個(gè)Map來(lái)保存shape name 和它對(duì)應(yīng)的構(gòu)造方法引用:
final static Map<String, Supplier> map = new HashMap<>();
static {
map.put("CIRCLE", Circle::new);
map.put("RECTANGLE", Rectangle::new);
}現(xiàn)在我們可以使用這個(gè)map來(lái)實(shí)例化不同的shapes
public class ShapeFactory {
final static Map<String, Supplier> map = new HashMap<>();
static {
map.put("CIRCLE", Circle::new);
map.put("RECTANGLE", Rectangle::new);
}
public Shape getShape(String shapeType){
Supplier shape = map.get(shapeType.toUpperCase());
if(shape != null) {
return shape.get();
}
throw new IllegalArgumentException("No such shape " + shapeType.toUpperCase());
}
}使用lambada表達(dá)式實(shí)現(xiàn)的工廠方法來(lái)創(chuàng)建shape對(duì)象:
FactoryPatternDemo.java
public class FactoryPatternDemo {
public static void main(String[] args) {
Supplier shapeFactory = ShapeFactory::new;
//call draw method of circle
shapeFactory.get().getShape("circle").draw();
//call draw method of Rectangle
shapeFactory.get().getShape("rectangle").draw();
}
}程序輸出
Inside Circle::draw() method. Inside Rectangle::draw() method.
這里的Shape::new可以被看作為lambda表達(dá)式的簡(jiǎn)寫(xiě)形式。盡管方法引用不一定(比如在這個(gè)例子里)會(huì)把語(yǔ)法變的更緊湊,但它擁有更明確的語(yǔ)義——如果我們想要調(diào)用的方法擁有一個(gè)名字,我們就可以通過(guò)它的名字直接調(diào)用它。
如果Shape構(gòu)造函數(shù)需要多個(gè)參數(shù),那么你就需要重新實(shí)現(xiàn)自己的Supplier
如:
() -> new Circe(args)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。