JAVA設計模式——(九)工廠模式
- 介紹
- 理解
- 實現
- Product
- Factory
- 測試
- 泛型擴展
- 應用
介紹
定義一個工廠類的接口,幫助一個實際對象 創建實例,并讓其工廠類的子類決定實例化哪個類。
理解
工廠模式中,必定分為了兩部分,一部分是被工廠模式實例化的對象(Product),一部分是工廠模式的接口(Factory)。
這里主要需要注意的是一個對象,工廠類只負責一個對象的創建。
實現
Product
package cn.sh.designepattern.example01;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 10:41* @Contact 643947568@qq.com*/
public interface Product {public void product();
}
實現類:
package cn.sh.designepattern.example01;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 10:41* @Contact 643947568@qq.com*/
public class ActualProduct implements Product {@Overridepublic void product() {System.out.println("生產具體的產品");}
}
Factory
package cn.sh.designepattern.example01;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 10:42* @Contact 643947568@qq.com*/
public interface Factory {public Product factory();}
工廠子類:
package cn.sh.designepattern.example01;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 10:42* @Contact 643947568@qq.com*/
public class ActualFactory implements Factory {@Overridepublic Product factory() {return new ActualProduct();}
}
測試
package cn.sh.designepattern.example01;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 10:18* @Contact 643947568@qq.com*/
public class Main {public static void main(String[] args) {ActualFactory actualFactory = new ActualFactory();Product factory = actualFactory.factory();factory.product();}
}
泛型擴展
采用泛型的工廠類
package cn.sh.designepattern.example02;import java.lang.reflect.InvocationTargetException;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 11:22* @Contact 643947568@qq.com*/
public interface Factory {public <T> T factory(Class<T> t) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException;
}
package cn.sh.designepattern.example02;import java.lang.reflect.InvocationTargetException;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 11:27* @Contact 643947568@qq.com*/
public class ActualFactory implements Factory {@Overridepublic <T> T factory(Class<T> t) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {return t.getDeclaredConstructor().newInstance();}
}
具體類1:
package cn.sh.designepattern.example02;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 11:24* @Contact 643947568@qq.com*/
public interface Product01 {public void product01();}
package cn.sh.designepattern.example02;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 11:24* @Contact 643947568@qq.com*/
public class ActualProductO1 implements Product01{@Overridepublic void product01() {System.out.println("生產01");}
}
具體類2:
package cn.sh.designepattern.example02;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 11:24* @Contact 643947568@qq.com*/
public interface Product02 {public void product02();}
package cn.sh.designepattern.example02;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 11:24* @Contact 643947568@qq.com*/
public class ActualProductO2 implements Product02 {@Overridepublic void product02() {System.out.println("生產02");}
}
測試:
package cn.sh.designepattern.example02;import java.lang.reflect.InvocationTargetException;/*** @Author song* @Version 0.0.1* @Date 2025/4/29 11:28* @Contact 643947568@qq.com*/
public class Main {public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {ActualFactory actualFactory = new ActualFactory();ActualProductO1 productO1 = actualFactory.factory(ActualProductO1.class);productO1.product01();ActualProductO2 productO2 = actualFactory.factory(ActualProductO2.class);productO2.product02();}
}
應用
上述的工廠模式是一個簡單的工廠模式,每次需要對具體類均創建一個工廠類,所以適用性單一。當然也有采用泛型的工廠方法,能夠適用較多的場景。
可用于對對象的封裝,降低模塊的耦合度,因為不需要知道具體類的實現細節,即使具體類的實現改變,也只需要修改具體的工廠類方法,所以有較好的擴展性。