• 尚硅谷设计模式学习(四)工厂模式


    从披萨店的案例引入工厂模式

    披萨的种类很多(比如 GreekPizza、CheesePizza 等)

    • 披萨制作完成后需要进行cut(切片工作),box(包装工作)
    • 完成披萨店的功能

    一、简单工厂模式

    1、基本介绍

    简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。定义了一个创建对象的工厂类,由这个工厂类来封装实例化对象的代码。

    在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式。

    属于创建型模式。

    2、代码实现

    设计方案:   定义一个可以实例化 Pizaa 对象的工厂类,封装创建对象的代码。

      

    Pizza接口

    1. public interface Pizza {
    2. /**
    3. * 披萨切片
    4. */
    5. void cut();
    6. /**
    7. * 披萨包装
    8. */
    9. void box();
    10. }

     奶酪披萨

    1. public class CheesePizza implements Pizza {
    2. public void cut() {
    3. System.out.println("奶酪披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("奶酪披萨打包中");
    7. }
    8. }

    希腊披萨

    1. public class GreekPizza implements Pizza {
    2. public void cut() {
    3. System.out.println("希腊披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("希腊披萨打包中");
    7. }
    8. }

    创建披萨实例的工厂

    1. public class PizzaFactory {
    2. public static Pizza getPizza(String type) {
    3. if(type.equalsIgnoreCase("CheesePizza")){
    4. return new CheesePizza();
    5. }else if(type.equalsIgnoreCase("GreekPizza")){
    6. return new GreekPizza();
    7. }else {
    8. return null;
    9. }
    10. }
    11. }

    测试

    1. public class Client {
    2. public static void main(String[] args) {
    3. Pizza cheesePizza = PizzaFactory.getPizza("CheesePizza");
    4. cheesePizza.cut();
    5. cheesePizza.box();
    6. Pizza greekPizza = PizzaFactory.getPizza("GreekPizza");
    7. greekPizza.cut();
    8. greekPizza.box();
    9. }
    10. }

     结果

    奶酪披萨切片中
    奶酪披萨打包中
    希腊披萨切片中
    希腊披萨打包中

    二、工厂方法模式

    看一个新的需求,客户在点披萨时,可以点不同口味的披萨,比如北京的奶酪 pizza、北京的胡椒 pizza 或者是伦敦的奶酪 pizza、伦敦的胡椒  pizza。 

    思路 1

    使用简单工厂模式,创建不同的简单工厂类,比如 BJPizzaSimpleFactory,LDPizzaSimpleFactory等等。从当前这个案例来说,也是可以的,但是考虑到项目的规模,以及软件的可维护性、可扩展性并不是特别好。

    思路 2

    使用工厂方法模式

    1、基本介绍

    工厂方法模式:定义一个创建对象的抽象方法,由子类工厂决定要实例化的类。工厂方法模式将对象的实例化推迟到子类。

    2、代码实现

     披萨类Pizza

    1. public interface Pizza {
    2. /**
    3. * 披萨切片
    4. */
    5. void cut();
    6. /**
    7. * 披萨包装
    8. */
    9. void box();
    10. }

    四种披萨

    1. public class BJChessPizza implements Pizza{
    2. public void cut() {
    3. System.out.println("北京奶酪披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("北京奶酪披萨打包中");
    7. }
    8. }
    1. public class BJPepperPizza implements Pizza{
    2. public void cut() {
    3. System.out.println("北京胡椒披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("北京胡椒披萨打包中");
    7. }
    8. }
    1. public class LDChessPizza implements Pizza{
    2. public void cut() {
    3. System.out.println("伦敦奶酪披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("伦敦奶酪披萨打包中");
    7. }
    8. }
    1. public class LDPepperPizza implements Pizza{
    2. public void cut() {
    3. System.out.println("伦敦胡椒披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("伦敦胡椒披萨打包中");
    7. }
    8. }

    Pizza抽象工厂

    1. public abstract class PizzaFactory {
    2. public abstract Pizza getPizza(String type);
    3. }

    北京披萨工厂,伦敦披萨工厂

    1. public class BJPizzaFactory extends PizzaFactory {
    2. @Override
    3. public Pizza getPizza(String type) {
    4. if(type.equalsIgnoreCase("chess")){
    5. return new BJChessPizza();
    6. }else if(type.equalsIgnoreCase("pepper")){
    7. return new BJPepperPizza();
    8. }else {
    9. return null;
    10. }
    11. }
    12. }
    1. public class LDPizzaFactory extends PizzaFactory {
    2. @Override
    3. public Pizza getPizza(String type) {
    4. if(type.equals("chess")){
    5. return new LDChessPizza();
    6. }else if(type.equals("pepper")){
    7. return new LDPepperPizza();
    8. }else {
    9. return null;
    10. }
    11. }
    12. }

    测试

    1. public class Client {
    2. public static void main(String[] args) {
    3. //北京披萨工厂
    4. BJPizzaFactory bjPizzaFactory = new BJPizzaFactory();
    5. Pizza bjChessPizza = bjPizzaFactory.getPizza("chess");
    6. bjChessPizza.cut();
    7. bjChessPizza.box();
    8. Pizza bjPepperPizza = bjPizzaFactory.getPizza("pepper");
    9. bjPepperPizza.cut();
    10. bjPepperPizza.box();
    11. //伦敦披萨工厂
    12. LDPizzaFactory ldPizzaFactory = new LDPizzaFactory();
    13. Pizza ldChessPizza = ldPizzaFactory.getPizza("chess");
    14. ldChessPizza.cut();
    15. ldChessPizza.box();
    16. Pizza ldPepperPizza = ldPizzaFactory.getPizza("pepper");
    17. ldPepperPizza.cut();
    18. ldPepperPizza.box();
    19. }
    20. }

    结果

    北京奶酪披萨切片中
    北京奶酪披萨打包中
    北京胡椒披萨切片中
    北京胡椒披萨打包中
    伦敦奶酪披萨切片中
    伦敦奶酪披萨打包中
    伦敦胡椒披萨切片中
    伦敦胡椒披萨打包中

    三、抽象工厂模式

    新需求:有的人喜欢吃肯德基的披萨,有的人喜欢吃麦当劳的披萨

    1、基本介绍

    抽象工厂模式:为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类

    2、代码实现 

    披萨接口

    1. public interface Pizza {
    2. /**
    3. * 披萨切片
    4. */
    5. void cut();
    6. /**
    7. * 披萨包装
    8. */
    9. void box();
    10. }

    商店A披萨,商店B披萨接口

    1. public interface ShopAPizza extends Pizza {
    2. }
    1. public interface ShopBPizza extends Pizza {
    2. }

    商店A披萨的实现类

    1. public class ShopABJChessPizza implements ShopAPizza{
    2. public void cut() {
    3. System.out.println("商店A北京奶酪披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("商店A北京奶酪披萨打包中");
    7. }
    8. }
    1. public class ShopALDChessPizza implements ShopAPizza{
    2. public void cut() {
    3. System.out.println("商店A伦敦奶酪披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("商店A伦敦奶酪披萨打包中");
    7. }
    8. }

    商店B披萨的实现类 

    1. public class ShopBBJChessPizza implements ShopBPizza{
    2. public void cut() {
    3. System.out.println("商店B北京胡椒披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("商店B北京胡椒披萨打包中");
    7. }
    8. }
    1. public class ShopBLDChessPizza implements ShopBPizza{
    2. public void cut() {
    3. System.out.println("商店B伦敦胡椒披萨切片中");
    4. }
    5. public void box() {
    6. System.out.println("商店B伦敦胡椒披萨打包中");
    7. }
    8. }

    披萨工厂抽象类

    1. public abstract class PizzaFactory {
    2. public abstract Pizza getBJChessPizza(String type);
    3. public abstract Pizza getLDChessPizza(String type);
    4. }

     商店A,B的工厂类

    1. public class ShopAPizzaFactory extends PizzaFactory {
    2. @Override
    3. public Pizza getBJChessPizza(String type) {
    4. return new ShopABJChessPizza();
    5. }
    6. @Override
    7. public Pizza getLDChessPizza(String type) {
    8. return new ShopALDChessPizza();
    9. }
    10. }
    1. public class ShopBPizzaFactory extends PizzaFactory {
    2. @Override
    3. public Pizza getBJChessPizza(String type) {
    4. return new ShopBBJChessPizza();
    5. }
    6. @Override
    7. public Pizza getLDChessPizza(String type) {
    8. return new ShopBLDChessPizza();
    9. }
    10. }

     四、JDK中的工厂模式源码案例

    比如说日历类Calendar就用到了简单工厂模式

    我们一般这样使用

    1. public class Test {
    2. public static void main(String[] args) {
    3. Calendar cal = Calendar.getInstance();
    4. System.out.println("年:" + cal.get(Calendar.YEAR));
    5. System.out.println("月:" + (cal.get(Calendar.MONTH) + 1));
    6. System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH));
    7. System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY));
    8. System.out.println("分:" + cal.get(Calendar.MINUTE));
    9. System.out.println("秒:" + cal.get(Calendar.SECOND));
    10. }
    11. }

    具体源码:就用到了简单工厂模式 

    1. /**
    2. * Gets a calendar using the default time zone and locale. The
    3. * Calendar returned is based on the current time
    4. * in the default time zone with the default
    5. * {@link Locale.Category#FORMAT FORMAT} locale.
    6. *
    7. * @return a Calendar.
    8. */
    9. public static Calendar getInstance()
    10. {
    11. return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
    12. }
    1. private static Calendar createCalendar(TimeZone zone,
    2. Locale aLocale)
    3. //根据 TimeZone zone, Locale aLocale创建对应的实例
    4. {
    5. CalendarProvider provider =
    6. LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
    7. .getCalendarProvider();
    8. if (provider != null) {
    9. try {
    10. return provider.getInstance(zone, aLocale);
    11. } catch (IllegalArgumentException iae) {
    12. // fall back to the default instantiation
    13. }
    14. }
    15. Calendar cal = null;
    16. if (aLocale.hasExtensions()) {
    17. String caltype = aLocale.getUnicodeLocaleType("ca");
    18. if (caltype != null) {
    19. switch (caltype) {
    20. case "buddhist":
    21. cal = new BuddhistCalendar(zone, aLocale);
    22. break;
    23. case "japanese":
    24. cal = new JapaneseImperialCalendar(zone, aLocale);
    25. break;
    26. case "gregory":
    27. cal = new GregorianCalendar(zone, aLocale);
    28. break;
    29. }
    30. }
    31. }
    32. if (cal == null) {
    33. // If no known calendar type is explicitly specified,
    34. // perform the traditional way to create a Calendar:
    35. // create a BuddhistCalendar for th_TH locale,
    36. // a JapaneseImperialCalendar for ja_JP_JP locale, or
    37. // a GregorianCalendar for any other locales.
    38. // NOTE: The language, country and variant strings are interned.
    39. if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
    40. cal = new BuddhistCalendar(zone, aLocale);
    41. } else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
    42. && aLocale.getCountry() == "JP") {
    43. cal = new JapaneseImperialCalendar(zone, aLocale);
    44. } else {
    45. cal = new GregorianCalendar(zone, aLocale);
    46. }
    47. }
    48. return cal;
    49. }

    五、工厂模式小结

    1、工厂模式的意义

    将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性。

    在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式

    2、三种工厂模式   (简单工厂模式、工厂方法模式、抽象工厂模式)

    个人觉得这个区别在于产品,如果产品单一,最合适用工厂模式,但是如果有多个业务品种、业务分类时,通过抽象工厂模式产生需要的对象是一种非常好的解决方式。再通俗深化理解下:工厂模式针对的是一个产品等级结构 ,抽象工厂模式针对的是面向多个产品等级结构的。

    再来看看工厂方法模式与抽象工厂模式对比:

    工厂方法模式

    抽象工厂模式

    针对的是一个产品等级结构针对的是面向多个产品等级结构
    一个抽象产品类多个抽象产品类
    可以派生出多个具体产品类每个抽象产品类可以派生出多个具体产品类
    一个抽象工厂类,可以派生出多个具体工厂类一个抽象工厂类,可以派生出多个具体工厂类
    每个具体工厂类只能创建一个具体产品类的实例每个具体工厂类可以创建多个具体产品类的实例

    参考文章:(1条消息) 抽象工厂模式-与-工厂方法模式区别_wyxhd2008的博客-CSDN博客 

  • 相关阅读:
    什么是智能合约,如何熟悉智能合约
    MACU-Net-用于精细分辨率遥感图像语义分割网络
    23年底,我出齐了Spring boot,Spring cloud和案例方面的书,正在写一本面试书(代年终总结)
    【LeetCode】21. Middle of the Linked List· 链表的中间节点
    09-13-Hbase-shell入门操作
    简易实现通讯录(1.0)
    nginx配置详解
    Qt+QtWebApp开发笔记(五):http服务器html中使用json触发ajax与后台交互实现数据更新传递
    Laf Assistant:云开发从未如此爽快!
    CLR Via 读书笔记
  • 原文地址:https://blog.csdn.net/qq_51409098/article/details/126837120