設計模式——工廠模式

設計模式——工廠模式

工廠模式:一種創建行模式,提供了一種創建具有相同屬性或行爲的對象的最佳方式。顧名思義工廠生產出來的產品都是標準化的,如果在項目中需要在多處地方創建相同的對象(對對象進行了相同的處理並提供出來),此時可以考慮使用工廠模式!

廢話不多說,show code!

 

1-定義Factory,定義抽象方法

工廠模式,首先需要工廠一般定義成一個接口或者抽象類

package designPattern.test.factory.simple;

/**
 * 工廠接口,還不確定生產的產品,定義成泛型接口
 * 生產類型爲T的產品
 */
public interface Factory<T> {
    T create();
}

2-定義產品

工廠的存在就是生產產品的,有了工廠就可以根據具體的項目定義具體的產品了,下面以手機爲例介紹工廠模式中的產品

2.1-定義產品的行爲

產品生產出來就是供使用的,將產品的行爲抽象出來

package designPattern.test.factory.simple.product;

/**
 * 手機接口
 */
public interface Phone {

    /**
     * 手機可以打電話
     */
    void call(String name);

    /**
     * 手機可以發短信
     */
    void sendMessage(String name,String message);

}

2.2-定義抽象產品

工廠生產的產品具有一些特定的屬性,通常都是標準化的,一般不會改變!

package designPattern.test.factory.simple.product;

import java.io.Serializable;

/**
 * 抽象產品
 */
public abstract class BasePhone implements Serializable {

    //長度
    private double length;

    //寬度
    private double width;

    //厚度
    private double thick;

    //顏色
    private String color;

    public BasePhone(){}

    public BasePhone(double length, double width, double thick, String color) {
        this.length = length;
        this.width = width;
        this.thick = thick;
        this.color = color;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getThick() {
        return thick;
    }

    public void setThick(double thick) {
        this.thick = thick;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "BasePhone{" +
                "length=" + length +
                ", width=" + width +
                ", thick=" + thick +
                ", color='" + color + '\'' +
                '}';
    }

}

2.3定義具體產品

具體的產品,定義ApplePhone

package designPattern.test.factory.simple.product;

/**
 * Apple手機
 */
public class ApplePhone extends BasePhone implements Phone {

    public ApplePhone() {
        super();
    }

    public ApplePhone(double length, double width, double thick, String color) {
       super(length,width,thick,color);
    }

    @Override
    public void call(String name) {
        System.out.println("使用Apple手機" + this.toString() + "打電話給" + name);
    }

    @Override
    public void sendMessage(String name, String message) {
        System.out.println("使用Apple手機" + this.toString() + "發短信給" + name + ",內容爲:" + message);
    }

}

定義具體產品:HuaWeiPhone

package designPattern.test.factory.simple.product;

/**
 * 華爲手機
 */
public class HuaWeiPhone extends BasePhone implements Phone {

    public HuaWeiPhone() {
        super();
    }

    public HuaWeiPhone(double length, double width, double thick, String color) {
        super(length, width, thick, color);
    }

    @Override
    public void call(String name) {
        System.out.println("使用HuaWei手機" + this.toString() + "打電話給" + name);
    }

    @Override
    public void sendMessage(String name, String message) {
        System.out.println("使用HuaWei手機" + this.toString() + "發短信給" + name + ",內容爲:" + message);
    }
}

3定義工廠

3-1定義抽象工廠

package designPattern.test.factory.simple.factory;

import designPattern.test.factory.simple.Factory;
import designPattern.test.factory.simple.product.BasePhone;

/**
 * 手機工廠
 */
public abstract class PhoneFactory implements Factory<BasePhone> {

    /**
     * 獲取Factory方法
     */
    public static Factory getFactory(Class clazz) {
        Factory factory = null;
        try {
            factory = (Factory) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return factory;
    }

}

3-2定義具體手機工廠

ApplePhoneFactory

package designPattern.test.factory.simple.factory;

import designPattern.test.factory.simple.product.ApplePhone;
import designPattern.test.factory.simple.product.BasePhone;

/**
 * Apple手機工廠
 */
public class ApplePhoneFactory extends PhoneFactory {

    /**
     * 生產具體的ApplePhone產品,如果需要對ApplePhone進行統一的初始化操作
     * 可以都定義在這裏,項目中就不需要在重複初始化了
     */
    @Override
    public BasePhone create() {
        return new ApplePhone(155, 88, 9.1, "golden");
    }

}

HuaWeiPhoneFactory

package designPattern.test.factory.simple.factory;

import designPattern.test.factory.simple.product.BasePhone;
import designPattern.test.factory.simple.product.HuaWeiPhone;

/**
 * 華爲手機
 */
public class HuaWeiPhoneFactory extends PhoneFactory {

    /**
     * 生產具體的HuaWeiPhone產品,如果需要對HuaWeiPhone進行統一的初始化操作
     * 可以都定義在這裏,項目中就不需要在重複初始化了
     */
    @Override
    public BasePhone create() {
        return new HuaWeiPhone(155, 88, 10.2, "golden");
    }

}

4測試

package designPattern.test.factory.simple;

import designPattern.test.factory.simple.factory.ApplePhoneFactory;
import designPattern.test.factory.simple.factory.HuaWeiPhoneFactory;
import designPattern.test.factory.simple.factory.PhoneFactory;
import designPattern.test.factory.simple.product.Phone;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 工廠模式測試
 */
public class FactoryTest {

    private static final String APPLEPHONE = "applePhone";
    private static final String HUAWEIPHONE = "huaweiPhone";

    /**
     * 工廠集合
     */
    private static Map<String, Factory> factoryMap = new HashMap<>();

    /**
     * 初始化工廠放入集合中
     */
    static {
        Factory applePhoneFactory = PhoneFactory.getFactory(ApplePhoneFactory.class);
        Factory huaweiPhoneFactory = PhoneFactory.getFactory(HuaWeiPhoneFactory.class);
        factoryMap.put(APPLEPHONE, applePhoneFactory);
        factoryMap.put(HUAWEIPHONE, huaweiPhoneFactory);
    }

    /**
     * 生產ApplePhone,打電話發短信
     */
    @Test
    public void testApplePhone() {
        Factory factory = factoryMap.get(APPLEPHONE);
        callAndSendMsg(factory, "yudaijing", "HelloWorld");
    }

    /**
     * 生產ApplePhone,打電話發短信
     */
    @Test
    public void testHuaWeiPhone() {
        Factory factory = factoryMap.get(HUAWEIPHONE);
        callAndSendMsg(factory, "yudaijing", "HelloWorld");
    }

    /**
     * 工廠生產出手機後就打電話發短信
     */
    private void callAndSendMsg(Factory factory, String name, String msg) {
        Phone phone = (Phone) factory.create();
        phone.call(name);
        phone.sendMessage(name, msg);
    }

}

總結:一般來講,創建複雜的、經常需要創建的對象就應該考慮使用工廠了,一般直接new方法就可以創建的JavaBean不建議使用工廠模式!










發佈了31 篇原創文章 · 獲贊 41 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章