Java設(shè)計(jì)模式——工廠模式,java工廠設(shè)計(jì)模式
工廠模式是一種常見的創(chuàng)建型設(shè)計(jì)模式,它提供了一種創(chuàng)建對(duì)象的方法,將對(duì)象的創(chuàng)建與使用分離開來,可以減少代碼的耦合度。在工廠模式中,我們通過一個(gè)工廠類來創(chuàng)建對(duì)象,而不是直接在代碼中實(shí)例化對(duì)象。
工廠模式有三種常見的實(shí)現(xiàn)方式:簡(jiǎn)單工廠模式、工廠方法模式和抽象工廠模式。下面我將簡(jiǎn)單介紹一下這三種模式的實(shí)現(xiàn)方式。
1. 簡(jiǎn)單工廠模式
簡(jiǎn)單工廠模式又稱為靜態(tài)工廠模式,它通過一個(gè)工廠類來創(chuàng)建對(duì)象,客戶端只需要知道要?jiǎng)?chuàng)建的產(chǎn)品的類型,而不需要知道具體的實(shí)現(xiàn)細(xì)節(jié)。簡(jiǎn)單工廠模式適用于創(chuàng)建的對(duì)象比較少的情況。
下面是一個(gè)簡(jiǎn)單工廠模式的示例代碼:
```java
// 抽象產(chǎn)品類
interface Product {
? ? void use();
}
// 具體產(chǎn)品類
class ConcreteProductA implements Product {
? ? public void use() {
? ? ? ? System.out.println("使用產(chǎn)品A");
? ? }
}
class ConcreteProductB implements Product {
? ? public void use() {
? ? ? ? System.out.println("使用產(chǎn)品B");
? ? }
}
// 工廠類
class SimpleFactory {
? ? public static Product createProduct(String type) {
? ? ? ? if (type.equals("A")) {
? ? ? ? ? ? return new ConcreteProductA();
? ? ? ? } else if (type.equals("B")) {
? ? ? ? ? ? return new ConcreteProductB();
? ? ? ? } else {
? ? ? ? ? ? throw new IllegalArgumentException("不支持的產(chǎn)品類型");
? ? ? ? }
? ? }
}
// 客戶端
public class Client {
? ? public static void main(String[] args) {
? ? ? ? Product productA = SimpleFactory.createProduct("A");
? ? ? ? productA.use();
? ? ? ? Product productB = SimpleFactory.createProduct("B");
? ? ? ? productB.use();
? ? }
}
```
上面的代碼中,我們定義了一個(gè)抽象產(chǎn)品類 `Product` 和兩個(gè)具體產(chǎn)品類 `ConcreteProductA` 和 `ConcreteProductB`。然后我們定義了一個(gè)工廠類 `SimpleFactory`,其中的 `createProduct` 方法根據(jù)傳入的參數(shù)創(chuàng)建不同的產(chǎn)品對(duì)象。
客戶端只需要調(diào)用 `SimpleFactory.createProduct` 方法,傳入對(duì)應(yīng)的參數(shù),就可以得到不同的產(chǎn)品對(duì)象。
2. 工廠方法模式
工廠方法模式是指定義一個(gè)抽象工廠接口,具體的工廠類實(shí)現(xiàn)這個(gè)工廠接口,并且每個(gè)具體工廠類只能創(chuàng)建對(duì)應(yīng)的產(chǎn)品。工廠方法模式適用于創(chuàng)建的對(duì)象比較多的情況。
下面是一個(gè)工廠方法模式的示例代碼:
```java
// 抽象產(chǎn)品類
interface Product {
? ? void use();
}
// 具體產(chǎn)品類
class ConcreteProductA implements Product {
? ? public void use() {
? ? ? ? System.out.println("使用產(chǎn)品A");
? ? }
}
class ConcreteProductB implements Product {
? ? public void use() {
? ? ? ? System.out.println("使用產(chǎn)品B");
? ? }
}
// 抽象工廠類
interface Factory {
? ? Product createProduct();
}
// 具體工廠類
class ConcreteFactoryA implements Factory {
? ? public Product createProduct() {
? ? ? ? return new ConcreteProductA();
? ? }
}
class ConcreteFactoryB implements Factory {
? ? public Product createProduct() {
? ? ? ? return new ConcreteProductB();
? ? }
}
// 客戶端
public class Client {
? ? public static void main(String[] args) {
? ? ? ? Factory factoryA = new ConcreteFactoryA();
? ? ? ? Product productA = factoryA.createProduct();
? ? ? ? productA.use();
? ? ? ? Factory factoryB = new ConcreteFactoryB();
? ? ? ? Product productB = factoryB.createProduct();
? ? ? ? productB.use();
? ? }
}
```
上面的代碼中,我們定義了一個(gè)抽象產(chǎn)品類 `Product` 和兩個(gè)具體產(chǎn)品類 `ConcreteProductA` 和 `ConcreteProductB`。然后我們定義了一個(gè)抽象工廠類 `Factory`,其中的 `createProduct` 方法用于創(chuàng)建產(chǎn)品對(duì)象。
接著我們定義了兩個(gè)具體工廠類 `ConcreteFactoryA` 和 `ConcreteFactoryB`,它們分別實(shí)現(xiàn)了 `Factory` 接口,并且每個(gè)工廠類只能創(chuàng)建對(duì)應(yīng)的產(chǎn)品。
客戶端需要先創(chuàng)建對(duì)應(yīng)的具體工廠類對(duì)象,然后通過工廠對(duì)象的 `createProduct` 方法來創(chuàng)建產(chǎn)品對(duì)象。
3. 抽象工廠模式
抽象工廠模式是指定義一個(gè)抽象工廠接口,具體的工廠類實(shí)現(xiàn)這個(gè)工廠接口,并且每個(gè)工廠類可以創(chuàng)建一組產(chǎn)品。抽象工廠模式適用于創(chuàng)建的對(duì)象有多個(gè)產(chǎn)品族的情況。
下面是一個(gè)抽象工廠模式的示例代碼:
```java
// 抽象產(chǎn)品類
interface ProductA {
? ? void use();
}
interface ProductB {
? ? void eat();
}
// 具體產(chǎn)品類
class ConcreteProductA1 implements ProductA {
? ? public void use() {
? ? ? ? System.out.println("使用產(chǎn)品A1");
? ? }
}
class ConcreteProductA2 implements ProductA {
? ? public void use() {
? ? ? ? System.out.println("使用產(chǎn)品A2");
? ? }
}
class ConcreteProductB1 implements ProductB {
? ? public void eat() {
? ? ? ? System.out.println("吃產(chǎn)品B1");
? ? }
}
class ConcreteProductB2 implements ProductB {
? ? public void eat() {
? ? ? ? System.out.println("吃產(chǎn)品B2");
? ? }
}
// 抽象工廠類
interface Factory {
? ? ProductA createProductA();
? ? ProductB createProductB();
}
// 具體工廠類
class ConcreteFactory1 implements Factory {
? ? public ProductA createProductA() {
? ? ? ? return new ConcreteProductA1();
? ? }
? ? public ProductB createProductB() {
? ? ? ? return new ConcreteProductB1();
? ? }
}
class ConcreteFactory2 implements Factory {
? ? public ProductA createProductA() {
? ? ? ? return new ConcreteProductA2();
? ? }
? ? public ProductB createProductB() {
? ? ? ? return new ConcreteProductB2();
? ? }
}
// 客戶端
public class Client {
? ? public static void main(String[] args) {
? ? ? ? Factory factory1 = new ConcreteFactory1();
? ? ? ? ProductA productA1 = factory1.createProductA();
? ? ? ? productA1.use();
? ? ? ? ProductB productB1 = factory1.createProductB();
? ? ? ? productB1.eat();
? ? ? ? Factory factory2 = new ConcreteFactory2();
? ? ? ? ProductA productA2 = factory2.createProductA();
? ? ? ? productA2.use();
? ? ? ? ProductB productB2 = factory2.createProductB();
? ? ? ? productB2.eat();
? ? }
}
```
上面的代碼中,我們定義了兩個(gè)抽象產(chǎn)品類 `ProductA` 和 `ProductB`,以及四個(gè)具體產(chǎn)品類 `ConcreteProductA1`、`ConcreteProductA2`、`ConcreteProductB1` 和 `ConcreteProductB2`。
然后我們定義了一個(gè)抽象工廠類 `Factory`,其中的 `createProductA` 和 `createProductB` 方法分別用于創(chuàng)建 `ProductA` 和 `ProductB` 對(duì)象。
接著我們定義了兩個(gè)具體工廠類 `ConcreteFactory1` 和 `ConcreteFactory2`,它們分別實(shí)現(xiàn)了 `Factory` 接口,并且每個(gè)工廠類可以創(chuàng)建一組產(chǎn)品。
客戶端需要先創(chuàng)建對(duì)應(yīng)的具體工廠類對(duì)象,然后通過工廠對(duì)象的 `createProductA` 和 `createProductB` 方法來創(chuàng)建產(chǎn)品對(duì)象。