概念
根據定義一個工廠父類來定義創建對象的公共接口,而子類負責生成特定對象。
工廠模式主要分爲普通工廠模式(又叫簡單工廠模式)、工廠方法模式和抽象工廠模式這三種模式,下面我們針對每一種模式做一個詳細說明。
工廠模式類型
1、普通工廠模式(簡單工廠模式)
普通工廠模式也叫簡單工廠模式,簡單的說它就是創建了一個公共的接口工廠類,然後其他類都實現這個接口。
在該模式中,工廠類是整個模式的關鍵。它有着一個必要的判斷邏輯,能夠根據你的需要來創建所需要的實例。使用者使用的時候無序知道這些對象是怎麼樣創建的,也不用去關心這些。這有利於整個軟件系統的優化構架。
但是他也有缺點,因爲工廠類含有所有實例的創建邏輯,所以它不適合“高內聚”。還有當系統中特定產品類的數量增加時,可能需要工廠類也同樣做出調整,因此它的可伸縮性和擴展性不好。
下面我舉個栗子來說明一下:
1、創建一個公用的接口類
public interface SendMsg {
public void sendMsg();
}
2、分別創建兩個實現類
public class SendMsg1 implements SendMsg {
@Override
public void sendMsg() {
System.out.println("this is SendMsg1 !");
}
}
public class SendMsg2 implements SendMsg {
@Override
public void sendMsg() {
System.out.println("this is SendMsg2!");
}
}
3、創建工廠類
public class SendFactory {
public SendMsg produce(String type) {
if ("sendMsg1".equals(type)) {
return new SendMsg1();
} else if ("sendMsg2".equals(type)) {
return new SendMsg2();
} else {
System.out.println("正在開發中!");
return null;
}
}
}
4、測試
public class FactoryTest {
public static void main(String[] args) {
SendFactory factory = new SendFactory();
SendMsg sendMsg = factory.produce("sendMsg1");
sendMsg.Send();
}
}
2、工廠方法模式
如果產品出現多個抽象時,我們就需要有多個產品接口,這個場景就是我們說的工廠方法模式或者叫多個工廠方法模式。
話不多說,我們只需要將上面SendFactory類的代碼稍微改動下就行,直接上demo,如下所示
public class SendFactory {
public SendMsg produceSendMsg1(){
return new SendMsg1();
}
public SendMsg produceSendMsg2(){
return new SendMsg2();
}
}
測試
public class FactoryTest {
public static void main(String[] args) {
SendFactory factory = new SendFactory();
SendMsg sendMsg = factory.produceSendMsg1();
sendMsg.Send();
}
}
3、抽象工廠模式
抽象工廠是所有工廠模式中最抽象的一種形態。抽象工廠指多個抽象角色同時使用同一個工廠模式。使用他,我們只需要向客戶端提供一個接口,使客戶端能夠在不指定特定產品的情況下能實現多個產品中的產品對象的創建。
產品類
//汽車品牌和汽車名
public interface Car {
}
public class CarA extends Car {
public CarA(){
System.out.println("豐田-->CarA");
}
}
public class CaeB extends Car {
public EngineB(){
System.out.println("本田-->CarB");
}
}
//手機和手機型號
public interface MobilePhone {
}
public class MobilePhoneA extends MobilePhone {
public MobilePhoneA(){
System.out.println("手機-->MobilePhoneA");
}
}
public class MobilePhoneB extends MobilePhone {
public MobilePhoneB(){
System.out.println("手機-->MobilePhoneB");
}
}
工廠類
//創建工廠的接口
public interface AbstractFactory {
//製造汽車
public Car createCar();
//製造手機
public MobilePhone createMobilePhone();
}
//豐田生產的車型
public class FactoryCarLeiling implements AbstractFactory{
@Override
public Car createCar() {
return new CarA();
}
@Override
public MobilePhone createMobilePhone() {
return new MobilePhoneA();
}
}
public class FactoryCarKaluola implements AbstractFactory {
@Override
public Car createCar() {
return new CarB();
}
@Override
public MobilePhone createMobilePhone() {
return new MobilePhoneB();
}
}
客戶
public class Customer {
public static void main(String[] args){
FactoryCarLeiling factoryCarLeiling = new FactoryCarLeiling();
factoryCarLeiling.createCar();
factoryCarLeiling.createMobilePhone();
FactoryCarKaluola factoryCarKaluola = new FactoryCarKaluola();
factoryCarKaluola.createCar();
factoryCarKaluola.createMobilePhone();
}
}
總結
不管是簡單工廠模式、工廠方法模式、還是抽象工廠模式,它們都屬於工廠模式,而且他們的形態特徵都非常的相似,它們的目的都是爲了解耦。我們在使用時,不必在意到底使用哪個纔好,我們只需要知道它能否達到降低耦合度的目的就行了。
原創聲明:本文爲【Java學習提升】原創博文,轉載請註明出處。
本文來源於公衆號:【Java學習提升】 專注於Java領域技術分享,Java知識體系學習、分享面試經驗,讓我們結伴而行,共同成長!