您的当前位置:首页>新品 > 正文

聚焦:工厂模式是什么?工厂模式的详解

来源:CSDN 时间:2022-12-08 10:19:25

文章目录

前言传统模式简单工厂模式工厂方法模式抽象工厂模式


(资料图片仅供参考)

前言

Pizza orderPizza() {Pizza pizza = new Pizza();//准备材料pizza.prepare();//烘烤pizza.bake();//切pizza.cut();//装盒pizza.box();return pizza;}

传统模式

Pizza orderPizza(String type) {Pizza pizza;if(type.equals("chess"){pizza = new ChessPizza();}else if(type.equals("greek"){pizza = new GreekPizza();}else if(type.equals("apple"){pizza = new ApplePizza();}....//准备材料pizza.prepare();//烘烤pizza.bake();//切pizza.cut();//装盒pizza.box();return pizza;}

简单工厂模式

public class OrderPizza {public OrderPizza(SimpleFactory factory){setFactory(factory);    }    SimpleFactory factory = null;    public void setFactory(SimpleFactory simpleFactory){String orderType = "";//用户输入的        this.factory = simpleFactory;        do{orderType = getType();            Pizza pizza = factory.createPizza(orderType);            if (pizza != null){pizza.prepare();                pizza.bake();                pizza.cut();                pizza.box();            }else {System.out.println("订购Pizza失败!");                break;            }        }while (true);    }    //写一个方法 可以获取客户希望订购的pizza    private String getType() {try {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));            System.out.println("请在下面输入你要购买的Pizza");            String pizza = reader.readLine();            return pizza;        } catch (IOException e) {e.printStackTrace();            return "";        }    }}

public class SimpleFactory {public static Pizza createPizza(String pizzaType){Pizza pizza = null;        System.out.println("使用了简单工厂模式");        if (pizzaType.equals("greek")) {pizza = new GreekPizza();            pizza.setName("greek");        } else if (pizzaType.equals("chess")) {pizza = new ChessPizza();            pizza.setName("chess");        } else if (pizzaType.equals("pepper")) {//新增PepperPizza的时候 修改了源代码 违反了ocp原则 如果新增10000个?            //那就很麻烦了            pizza = new PepperPizza();            pizza.setName("pepper");        }        return pizza;    }}

工厂方法模式

AbstractFactory类

public abstract class AbstractFactory {//具体实现在子类,应用到了多态的特性    abstract Pizza createPizza(String orderType);    public AbstractFactory(){String orderType = "";//用户输入的        Pizza pizza = null;        do{orderType = getType();            //创建pizza延迟到子类            pizza = createPizza(orderType);            if (pizza == null){break;            }            //输出制pizza的流程            pizza.prepare();            pizza.bake();            pizza.cut();            pizza.bake();        }while (true);    }    //写一个方法 可以获取客户希望订购的pizza    private String getType() {try {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));            System.out.println("请在下面输入你要购买的Pizza");            String pizza = reader.readLine();            return pizza;        } catch (IOException e) {e.printStackTrace();            return "";        }    }}

BJFactory

public class BJFactory extends AbstractFactory {@Override    Pizza createPizza(String orderType) {Pizza pizza = null;        if (orderType.equals("chess")){pizza = new BJChessPizza();        }else if (orderType.equals("pepper")){pizza = new BJPepperPizza();        }        return pizza;    }}

LDFactory

public class LDFactory extends AbstractFactory {@Override    Pizza createPizza(String orderType) {Pizza pizza = null;        if (orderType.equals("chess")){pizza = new LDChessPizza();        }else if (orderType.equals("pepper")){pizza = new LDPepperPizza();        }        return 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 String getName() {return name;    }    public void setName(String name) {this.name = name;    }}

LDChessPizza

public class LDChessPizza extends Pizza {@Override    public void prepare() {setName("伦敦奶酪披萨");        System.out.println("准备伦敦奶酪pizza的原材料.....");    }}

BJPepperPizza

public class BJPepperPizza  extends Pizza{@Override    public void prepare() {setName("北京胡椒披萨");        System.out.println("准备北京胡椒pizza的原材料.....");    }}

工厂方法模式定义

抽象工厂模式

AbstractFactory接口

public interface AbstractFactory {Pizza createPizza(String orderType);    ···········    //创建酱的方法    ········    //创建蔬菜的方法    ········}

BJFactory类

public class BJFactory implements AbstractFactory {@Override    public Pizza createPizza(String orderType) {System.out.println("~~~使用的是抽象工厂模式~~~");        Pizza pizza = null;        if (orderType.equals("chess")){pizza = new BJChessPizza();        }else if (orderType.equals("pepper")){pizza = new BJPepperPizza();        }        return pizza;    }    //实现其他佐料方法}

LDFactory类

public class LDFactory implements AbstractFactory {@Override    public Pizza createPizza(String orderType) {System.out.println("~~~使用的是抽象工厂模式~~~");        Pizza pizza = null;        if (orderType.equals("chess")){pizza = new LDChessPizza();        }else if (orderType.equals("pepper")){pizza = new LDPepperPizza();        }        return pizza;    }    //实现其他佐料的方法}

OrderPizza类

public class OrderPizza {//抽象工厂    private AbstractFactory factory;    public OrderPizza(AbstractFactory factory) {//通过构造器利用组合的方式        setFactory(factory);    }    //通过set利用组合的方法    public void setFactory(AbstractFactory factory) {this.factory = factory;        do {String orderType = getType();            Pizza pizza = null;            pizza = factory.createPizza(orderType);            if (pizza == null) {System.out.println("订购失败!");            } else {pizza.prepare();                pizza.bake();                pizza.cut();                pizza.box();            }        } while (true);    }    //写一个方法 可以获取客户希望订购的pizza    private String getType() {try {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));            System.out.println("请在下面输入你要购买的Pizza");            String pizza = reader.readLine();            return pizza;        } catch (IOException e) {e.printStackTrace();            return "";        }    }}

HeadFirst中,这段采访很精彩

简单工厂在jdk源码中的引用: 在调用createCalendar中,

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;    }

标签:

最新新闻:

新闻放送
Top