1、基本介绍
- 其定义了一个interface用于创建相关或有依赖关系的对象簇,而无需指明具体的类
- 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合
- 从设计层面看,抽象工厂模式就是对简单工厂模式的改进(或称为进一步的抽象)
- 将工厂抽象成两层,AbsFactory(抽象工厂)和具体实现的工厂子类。可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更有利于代码的维护和扩展
2、UML类图
3、应用实例
Pizza
package com.weirdo.factory.absfactory.pizzastore.pizza; / * 抽象Pizza类 */ public abstract class Pizza {
protected String name; / * 准备材料,不同披萨需要准备的材料不同,因此做成抽象 */ public abstract void prepare(); / * 烘烤 */ public void bake() {
System.out.println(name + " baking;"); } / * 切 */ public void cut() {
System.out.println(name + " cutting;"); } / * 打包 */ public void box() {
System.out.println(name + " boxing;"); } public void setName(String name) {
this.name = name; } }
讯享网
LDPepperPizza
讯享网package com.weirdo.factory.absfactory.pizzastore.pizza; public class LDPepperPizza extends Pizza{
@Override public void prepare() {
setName("伦敦的胡椒pizza"); System.out.println("伦敦的胡椒pizza 准备原材料"); } }
LDCheesePizza
package com.weirdo.factory.absfactory.pizzastore.pizza; public class LDCheesePizza extends Pizza{
@Override public void prepare() {
setName("伦敦的奶酪pizzapizza"); System.out.println("伦敦的奶酪pizza 准备原材料"); } }
BJPepperPizza
讯享网package com.weirdo.factory.absfactory.pizzastore.pizza; public class BJPepperPizza extends Pizza {
@Override public void prepare() {
setName("北京的胡椒pizza"); System.out.println("北京的胡椒pizza 准备原材料"); } }
BJCheesePizza
package com.weirdo.factory.absfactory.pizzastore.pizza; public class BJCheesePizza extends Pizza {
@Override public void prepare() {
setName("北京的奶酪pizza"); System.out.println("北京的奶酪pizza 准备原材料"); } }
AbsFactory
讯享网package com.weirdo.factory.absfactory.pizzastore.order; import com.weirdo.factory.absfactory.pizzastore.pizza.Pizza; public interface AbsFactory {
public Pizza createPizza(String orderType); }
LDFactory
package com.weirdo.factory.absfactory.pizzastore.order; import com.weirdo.factory.absfactory.pizzastore.pizza.LDCheesePizza; import com.weirdo.factory.absfactory.pizzastore.pizza.LDPepperPizza; import com.weirdo.factory.absfactory.pizzastore.pizza.Pizza; public class LDFactory implements AbsFactory {
@Override public Pizza createPizza(String orderType) {
System.out.println("================使用的是抽象工厂模式==================="); Pizza pizza = null; if ("cheese".equals(orderType)) {
pizza = new LDCheesePizza(); } else if ("pepper".equals(orderType)) {
pizza = new LDPepperPizza(); } return pizza; } }
BJFactory
讯享网package com.weirdo.factory.absfactory.pizzastore.order; import com.weirdo.factory.absfactory.pizzastore.pizza.BJCheesePizza; import com.weirdo.factory.absfactory.pizzastore.pizza.BJPepperPizza; import com.weirdo.factory.absfactory.pizzastore.pizza.Pizza; public class BJFactory implements AbsFactory {
@Override public Pizza createPizza(String orderType) {
System.out.println("================使用的是抽象工厂模式==================="); Pizza pizza = null; if("cheese".equals(orderType)) {
pizza = new BJCheesePizza(); } else if ("pepper".equals(orderType)){
pizza = new BJPepperPizza(); } return pizza; } }
OrderPizza
package com.weirdo.factory.absfactory.pizzastore.order; import com.weirdo.factory.absfactory.pizzastore.pizza.Pizza; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderPizza {
AbsFactory factory; // 构造器 public OrderPizza(AbsFactory factory) {
setFactory(factory); } private void setFactory(AbsFactory factory) {
Pizza pizza = null; //用户输入 String orderType = ""; this.factory = factory; do {
orderType = getType(); // factory 可能是北京的工厂子类,也可能是伦敦的工厂子类 pizza = factory.createPizza(orderType); if (pizza != null) {
pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } else {
System.out.println("订购失败"); break; } } while (true); } / * 获取客户希望订购的披萨种类 * @return */ private String getType() {
try {
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("input pizza 种类:"); String str = strin.readLine(); return str; } catch (IOException e) {
e.printStackTrace(); return ""; } } }
PizzaStore
讯享网package com.weirdo.factory.absfactory.pizzastore.order; public class PizzaStore {
public static void main(String[] args) {
new OrderPizza(new LDFactory()); new OrderPizza(new BJFactory()); } }
4、运行结果

5、工厂模式在JDK-Calendar中应用的源码
package com.weirdo.factory.absfactory.pizzastore.order; import java.util.Calendar; public class PizzaStore {
public static void main(String[] args) {
// new OrderPizza(new LDFactory()); // new OrderPizza(new BJFactory()); calendarTest(); } public static void calendarTest(){
//getInstance是Calendar的静态方法 Calendar calendar = Calendar.getInstance(); //月份下标从0开始,所以月份要加1 System.out.println("年:"+calendar.get(Calendar.YEAR)); System.out.println("月:"+(calendar.get(Calendar.MONTH)+1)); System.out.println("日:"+calendar.get(Calendar.DAY_OF_MONTH)); System.out.println("时:"+calendar.get(Calendar.HOUR_OF_DAY)); System.out.println("分:"+calendar.get(Calendar.MINUTE)); System.out.println("秒:"+calendar.get(Calendar.SECOND)); } }
讯享网 protected Calendar(TimeZone zone, Locale aLocale) {
fields = new int[FIELD_COUNT]; isSet = new boolean[FIELD_COUNT]; stamp = new int[FIELD_COUNT]; this.zone = zone; setWeekCountData(aLocale); } / * Gets a calendar using the default time zone and locale. The * <code>Calendar</code> returned is based on the current time * in the default time zone with the default * {@link Locale.Category#FORMAT FORMAT} locale. * * @return a Calendar. */ public static Calendar getInstance() {
return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT)); } / * Gets a calendar using the specified time zone and default locale. * The <code>Calendar</code> returned is based on the current time * in the given time zone with the default * {@link Locale.Category#FORMAT FORMAT} locale. * * @param zone the time zone to use * @return a Calendar. */ public static Calendar getInstance(TimeZone zone) {
return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT)); } / * Gets a calendar using the default time zone and specified locale. * The <code>Calendar</code> returned is based on the current time * in the default time zone with the given locale. * * @param aLocale the locale for the week data * @return a Calendar. */ public static Calendar getInstance(Locale aLocale) {
return createCalendar(TimeZone.getDefault(), aLocale); } / * Gets a calendar with the specified time zone and locale. * The <code>Calendar</code> returned is based on the current time * in the given time zone with the given locale. * * @param zone the time zone to use * @param aLocale the locale for the week data * @return a Calendar. */ public static Calendar getInstance(TimeZone zone, Locale aLocale) {
return createCalendar(zone, aLocale); } private static Calendar createCalendar(TimeZone zone, Locale aLocale) {
CalendarProvider provider = LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale) .getCalendarProvider(); if (provider != null) {
try {
return provider.getInstance(zone, aLocale); } catch (IllegalArgumentException iae) {
// fall back to the default instantiation } } Calendar cal = null; if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca"); if (caltype != null) {
switch (caltype) {
case "buddhist": cal = new BuddhistCalendar(zone, aLocale); break; case "japanese": cal = new JapaneseImperialCalendar(zone, aLocale); break; case "gregory": cal = new GregorianCalendar(zone, aLocale); break; } } } if (cal == null) {
// If no known calendar type is explicitly specified, // perform the traditional way to create a Calendar: // create a BuddhistCalendar for th_TH locale, // a JapaneseImperialCalendar for ja_JP_JP locale, or // a GregorianCalendar for any other locales. // NOTE: The language, country and variant strings are interned. if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
cal = new BuddhistCalendar(zone, aLocale); } else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja" && aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale); } else {
cal = new GregorianCalendar(zone, aLocale); } } return cal; }
6、工厂模式小结
- 工厂模式的意义:将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性。
- 三种工厂模式:简单工厂模式、工厂方法模式、抽象工厂模式
- 设计模式的
依赖抽象原则:
1、创建对象实例时,不要直接new类,而是把这个new类的动作放在一个工厂的方法中,并返回。
2、不要让类继承具体类,而是继承抽象类或者是实现interface(接口)
3、不要覆盖基类中已经实现的方法。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/113045.html