鱼C论坛

 找回密码
 立即注册
查看: 2032|回复: 3

这个题的简单工厂的类图怎么画?

[复制链接]
发表于 2023-10-29 18:03:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
8.东汉《风俗通》记录了这样一则神话故事:“开天辟地,未有人民,女娲搏黄土做人”,即女娲造人的故事。女娲是如何造人的呢?首先,女娲采集黄土捏成人的形状,然后放到八卦炉中烧制,最后放到大地上生长。由于女娲是第一次做人,经验不足,对火候的把控不准确,柴火放多了,火太旺,七七四十九天之后,造好拿出来一看,人都被烤黑了,于是黑种人就诞生了;然后女娲开始少放柴火,小火慢炖,又过了七七四十九天,拿出来一看,人还没烧制成熟,于是白种人就诞生了;最后,女娲有经验了,把控好火的大小,就这样又过了七七四十九天,打开八卦炉一看,这次刚刚好,于是黄种人就诞生了。请分别使用简单工厂模式、工厂方法模式和抽象工厂模式模拟实现女娲造人。请画出三种工厂方法的类图,编写程序实现女娲造人过程。并在实验心得中总结三种工厂方法的优缺点。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-29 18:05:12 | 显示全部楼层
这是一个非常有趣的问题,它要求我们使用三种不同的设计模式(简单工厂模式、工厂方法模式和抽象工厂模式)来模拟女娲造人的过程。下面是我为这三种模式分别画出的类图,并简单解释了每种模式的基本概念。

1. 简单工厂模式:

简单工厂模式是一种创建型设计模式,它提供了一种创建对象的方式。在这个模式中,有一个工厂类负责创建产品类的实例。产品类通常有一个共同的接口,工厂类可以根据传入的参数决定创建哪一种产品类的实例。

  1.     +-------------------+
  2.     | SimpleHumanFactory|
  3.     +-------------------+
  4.     | createHuman(type) |
  5.     +-------------------+
  6.             |
  7.             | uses
  8.             v
  9.     +-------------------+
  10.     |      Human        |
  11.     +-------------------+
  12.     | grow()            |
  13.     +-------------------+
  14.             ^
  15.             |
  16.     +-------------------+
  17.     |   BlackHuman      |
  18.     +-------------------+
  19.     | grow()            |
  20.     +-------------------+
  21.             ^
  22.             |
  23.     +-------------------+
  24.     |   WhiteHuman      |
  25.     +-------------------+
  26.     | grow()            |
  27.     +-------------------+
  28.             ^
  29.             |
  30.     +-------------------+
  31.     |   YellowHuman     |
  32.     +-------------------+
  33.     | grow()            |
  34.     +-------------------+
复制代码


2. 工厂方法模式:

工厂方法模式是一种创建型设计模式,它提供了一种方式,将创建实例的代码从主代码中分离出来,通过创建一个工厂接口和多个工厂实现类,每个工厂实现类负责创建一种产品。

  1.     +-------------------+
  2.     |   HumanFactory    |
  3.     +-------------------+
  4.     | createHuman()     |
  5.     +-------------------+
  6.             ^
  7.             |
  8.     +-------------------+
  9.     | BlackHumanFactory |
  10.     +-------------------+
  11.     | createHuman()     |
  12.     +-------------------+
  13.             ^
  14.             |
  15.     +-------------------+
  16.     | WhiteHumanFactory |
  17.     +-------------------+
  18.     | createHuman()     |
  19.     +-------------------+
  20.             ^
  21.             |
  22.     +-------------------+
  23.     |YellowHumanFactory |
  24.     +-------------------+
  25.     | createHuman()     |
  26.     +-------------------+
  27.             |
  28.             | uses
  29.             v
  30.     +-------------------+
  31.     |      Human        |
  32.     +-------------------+
  33.     | grow()            |
  34.     +-------------------+
复制代码


3. 抽象工厂模式:

抽象工厂模式是一种创建型设计模式,它提供了一种方式,可以创建一系列相关或依赖的对象,而无需指定它们具体的类。在这个模式中,每个工厂都能生产一系列的产品。

  1.     +-------------------+
  2.     |  AbstractFactory  |
  3.     +-------------------+
  4.     | createHuman()     |
  5.     | createFire()      |
  6.     +-------------------+
  7.             ^
  8.             |
  9.     +-------------------+
  10.     |   HumanFactory    |
  11.     +-------------------+
  12.     | createHuman()     |
  13.     | createFire()      |
  14.     +-------------------+
  15.             |
  16.             | uses
  17.             v
  18.     +-------------------+
  19.     |      Human        |
  20.     +-------------------+
  21.     | grow()            |
  22.     +-------------------+
  23.             ^
  24.             |
  25.     +-------------------+
  26.     |      Fire         |
  27.     +-------------------+
  28.     | burn()            |
  29.     +-------------------+
复制代码


以上就是三种工厂模式的类图,具体的代码实现和优缺点分析需要根据具体的编程语言和项目需求来进行。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-29 18:08:08 | 显示全部楼层
简单工厂模式(Simple Factory Pattern)是一种创建型设计模式,它提供了一个统一的接口来创建对象,但是隐藏了对象的具体实现细节。

在女娲造人的例子中,我们可以将女娲定义为一个工厂类,其中包含一个静态方法createHuman,根据传入的参数来创建不同种类的人。

类图如下所示:

  1. +---------------------+
  2. |    HumanFactory     |
  3. +---------------------+
  4. | + createHuman(type) |
  5. +---------------------+

  6. +----------------+
  7. |     Human      |
  8. +----------------+
  9. | + getColor()   |
  10. | + talk()       |
  11. +----------------+

  12. +----------------+
  13. |    BlackHuman   |
  14. +----------------+
  15. | + getColor()   |
  16. | + talk()       |
  17. +----------------+

  18. +----------------+
  19. |    WhiteHuman   |
  20. +----------------+
  21. | + getColor()   |
  22. | + talk()       |
  23. +----------------+

  24. +----------------+
  25. |    YellowHuman  |
  26. +----------------+
  27. | + getColor()   |
  28. | + talk()       |
  29. +----------------+
复制代码


其中,HumanFactory是工厂类,负责创建不同种类的人。Human是人类的抽象接口,定义了人类的共同行为。BlackHuman、WhiteHuman和YellowHuman是具体的人类实现,分别代表黑种人、白种人和黄种人。

在代码实现中,我们可以根据传入的参数type来判断应该创建哪种类型的人,并返回相应的实例。

  1. class Human:
  2.     def getColor(self):
  3.         pass

  4.     def talk(self):
  5.         pass

  6. class BlackHuman(Human):
  7.     def getColor(self):
  8.         return "黑色"

  9.     def talk(self):
  10.         return "黑种人说话"

  11. class WhiteHuman(Human):
  12.     def getColor(self):
  13.         return "白色"

  14.     def talk(self):
  15.         return "白种人说话"

  16. class YellowHuman(Human):
  17.     def getColor(self):
  18.         return "黄色"

  19.     def talk(self):
  20.         return "黄种人说话"

  21. class HumanFactory:
  22.     @staticmethod
  23.     def createHuman(type):
  24.         if type == "黑种人":
  25.             return BlackHuman()
  26.         elif type == "白种人":
  27.             return WhiteHuman()
  28.         elif type == "黄种人":
  29.             return YellowHuman()

  30. # 测试代码
  31. human1 = HumanFactory.createHuman("黑种人")
  32. print(human1.getColor())  # 输出:黑色
  33. print(human1.talk())  # 输出:黑种人说话

  34. human2 = HumanFactory.createHuman("白种人")
  35. print(human2.getColor())  # 输出:白色
  36. print(human2.talk())  # 输出:白种人说话

  37. human3 = HumanFactory.createHuman("黄种人")
  38. print(human3.getColor())  # 输出:黄色
  39. print(human3.talk())  # 输出:黄种人说话
复制代码


简单工厂模式的优点是实现简单,客户端只需要知道工厂类和产品类的接口即可,不需要关心具体的产品实现细节。缺点是如果需要添加新的产品,就需要修改工厂类的代码,违反了开闭原则。

下面是工厂方法模式的类图和代码实现。

工厂方法模式(Factory Method Pattern)是一种创建型设计模式,它将对象的创建延迟到子类中,子类可以决定要实例化的类是哪一个。

在女娲造人的例子中,我们可以将女娲定义为一个抽象工厂类,其中包含一个抽象方法createHuman,由子类来实现创建不同种类的人的具体逻辑。

类图如下所示:

  1. +---------------------+
  2. |   AbstractFactory   |
  3. +---------------------+
  4. | + createHuman()     |
  5. +---------------------+

  6. +----------------+
  7. |     Human      |
  8. +----------------+
  9. | + getColor()   |
  10. | + talk()       |
  11. +----------------+

  12. +----------------+
  13. |    BlackHuman   |
  14. +----------------+
  15. | + getColor()   |
  16. | + talk()       |
  17. +----------------+

  18. +----------------+
  19. |    WhiteHuman   |
  20. +----------------+
  21. | + getColor()   |
  22. | + talk()       |
  23. +----------------+

  24. +----------------+
  25. |    YellowHuman  |
  26. +----------------+
  27. | + getColor()   |
  28. | + talk()       |
  29. +----------------+

  30. +------------------+
  31. | BlackHumanFactory|
  32. +------------------+
  33. | + createHuman()  |
  34. +------------------+

  35. +------------------+
  36. | WhiteHumanFactory|
  37. +------------------+
  38. | + createHuman()  |
  39. +------------------+

  40. +------------------+
  41. | YellowHumanFactory|
  42. +------------------+
  43. | + createHuman()  |
  44. +------------------+
复制代码


其中,AbstractFactory是抽象工厂类,定义了创建人类的抽象方法createHuman。Human是人类的抽象接口,定义了人类的共同行为。BlackHuman、WhiteHuman和YellowHuman是具体的人类实现,分别代表黑种人、白种人和黄种人。BlackHumanFactory、WhiteHumanFactory和YellowHumanFactory是具体的工厂类,分别负责创建对应种类的人。

在代码实现中,我们可以定义一个抽象工厂类AbstractFactory,其中包含一个抽象方法createHuman。然后定义具体的工厂类,如BlackHumanFactory、WhiteHumanFactory和YellowHumanFactory,分别实现createHuman方法来创建对应种类的人。

  1. class Human:
  2.     def getColor(self):
  3.         pass

  4.     def talk(self):
  5.         pass

  6. class BlackHuman(Human):
  7.     def getColor(self):
  8.         return "黑色"

  9.     def talk(self):
  10.         return "黑种人说话"

  11. class WhiteHuman(Human):
  12.     def getColor(self):
  13.         return "白色"

  14.     def talk(self):
  15.         return "白种人说话"

  16. class YellowHuman(Human):
  17.     def getColor(self):
  18.         return "黄色"

  19.     def talk(self):
  20.         return "黄种人说话"

  21. class AbstractFactory:
  22.     def createHuman(self):
  23.         pass

  24. class BlackHumanFactory(AbstractFactory):
  25.     def createHuman(self):
  26.         return BlackHuman()

  27. class WhiteHumanFactory(AbstractFactory):
  28.     def createHuman(self):
  29.         return WhiteHuman()

  30. class YellowHumanFactory(AbstractFactory):
  31.     def createHuman(self):
  32.         return YellowHuman()

  33. # 测试代码
  34. factory1 = BlackHumanFactory()
  35. human1 = factory1.createHuman()
  36. print(human1.getColor())  # 输出:黑色
  37. print(human1.talk())  # 输出:黑种人说话

  38. factory2 = WhiteHumanFactory()
  39. human2 = factory2.createHuman()
  40. print(human2.getColor())  # 输出:白色
  41. print(human2.talk())  # 输出:白种人说话

  42. factory3 = YellowHumanFactory()
  43. human3 = factory3.createHuman()
  44. print(human3.getColor())  # 输出:黄色
  45. print(human3.talk())  # 输出:黄种人说话
复制代码


工厂方法模式的优点是符合开闭原则,增加新的产品只需要添加对应的工厂类即可,不需要修改已有的代码。缺点是每个具体的工厂只能创建一种具体产品,如果需要创建多种产品,就需要创建多个具体工厂类。

最后是抽象工厂模式的类图和代码实现。

抽象工厂模式(Abstract Factory Pattern)是一种创建型设计模式,它提供了一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。

在女娲造人的例子中,我们可以将女娲定义为一个抽象工厂类,其中包含三个抽象方法createBlackHuman、createWhiteHuman和createYellowHuman,由子类来实现创建不同种类的人的具体逻辑。

类图如下所示:

  1. +---------------------+
  2. |   AbstractFactory   |
  3. +---------------------+
  4. | + createBlackHuman()|
  5. | + createWhiteHuman()|
  6. | + createYellowHuman()|
  7. +---------------------+

  8. +----------------+
  9. |     Human      |
  10. +----------------+
  11. | + getColor()   |
  12. | + talk()       |
  13. +----------------+

  14. +----------------+
  15. |    BlackHuman   |
  16. +----------------+
  17. | + getColor()   |
  18. | + talk()       |
  19. +----------------+

  20. +----------------+
  21. |    WhiteHuman   |
  22. +----------------+
  23. | + getColor()   |
  24. | + talk()       |
  25. +----------------+

  26. +----------------+
  27. |    YellowHuman  |
  28. +----------------+
  29. | + getColor()   |
  30. | + talk()       |
  31. +----------------+

  32. +------------------+
  33. |   FemaleFactory  |
  34. +------------------+
  35. | + createBlackHuman()|
  36. | + createWhiteHuman()|
  37. | + createYellowHuman()|
  38. +------------------+

  39. +------------------+
  40. |   MaleFactory    |
  41. +------------------+
  42. | + createBlackHuman()|
  43. | + createWhiteHuman()|
  44. | + createYellowHuman()|
  45. +------------------+
复制代码


其中,AbstractFactory是抽象工厂类,定义了创建人类的抽象方法createBlackHuman、createWhiteHuman和createYellowHuman。Human是人类的抽象接口,定义了人类的共同行为。BlackHuman、WhiteHuman和YellowHuman是具体的人类实现,分别代表黑种人、白种人和黄种人。FemaleFactory和MaleFactory是具体的工厂类,分别负责创建对应种类的人。

在代码实现中,我们可以定义一个抽象工厂类AbstractFactory,其中包含三个抽象方法createBlackHuman、createWhiteHuman和createYellowHuman。然后定义具体的工厂类,如FemaleFactory和MaleFactory,分别实现这三个抽象方法来创建对应种类的人。

  1. class Human:
  2.     def getColor(self):
  3.         pass

  4.     def talk(self):
  5.         pass

  6. class BlackHuman(Human):
  7.     def getColor(self):
  8.         return "黑色"

  9.     def talk(self):
  10.         return "黑种人说话"

  11. class WhiteHuman(Human):
  12.     def getColor(self):
  13.         return "白色"

  14.     def talk(self):
  15.         return "白种人说话"

  16. class YellowHuman(Human):
  17.     def getColor(self):
  18.         return "黄色"

  19.     def talk(self):
  20.         return "黄种人说话"

  21. class AbstractFactory:
  22.     def createBlackHuman(self):
  23.         pass

  24.     def createWhiteHuman(self):
  25.         pass

  26.     def createYellowHuman(self):
  27.         pass

  28. class FemaleFactory(AbstractFactory):
  29.     def createBlackHuman(self):
  30.         return BlackHuman()

  31.     def createWhiteHuman(self):
  32.         return WhiteHuman()

  33.     def createYellowHuman(self):
  34.         return YellowHuman()

  35. class MaleFactory(AbstractFactory):
  36.     def createBlackHuman(self):
  37.         return BlackHuman()

  38.     def createWhiteHuman(self):
  39.         return WhiteHuman()

  40.     def createYellowHuman(self):
  41.         return YellowHuman()

  42. # 测试代码
  43. factory1 = FemaleFactory()
  44. blackHuman1 = factory1.createBlackHuman()
  45. print(blackHuman1.getColor())  # 输出:黑色
  46. print(blackHuman1.talk())  # 输出:黑种人说话

  47. factory2 = MaleFactory()
  48. whiteHuman1 = factory2.createWhiteHuman()
  49. print(whiteHuman1.getColor())  # 输出:白色
  50. print(whiteHuman1.talk())  # 输出:白种人说话

  51. factory3 = FemaleFactory()
  52. yellowHuman1 = factory3.createYellowHuman()
  53. print(yellowHuman1.getColor())  # 输出:黄色
  54. print(yellowHuman1.talk())  # 输出:黄种人说话
复制代码


抽象工厂模式的优点是可以创建多个产品族,而不仅仅是单个产品。缺点是增加新的产品族比较困难,需要修改抽象工厂的接口和所有的具体工厂类。

通过以上的示例和代码实现,我们可以对简单工厂模式、工厂方法模式和抽象工厂模式有一个更深入的理解,并了解它们的优缺点。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-29 18:08:17 | 显示全部楼层
类图设计:

首先,我们需要创建一个表示人的接口或抽象类,然后派生出三个具体的人种类(黑种人、白种人、黄种人)。接下来,我们可以使用不同的工厂方法模式来创建这些人种类的实例。

简单工厂模式类图:

[/code]
+-------------------+
|     HumanFactory  |
+-------------------+
| + createHuman()   |
+-------------------+

        ^
        |
        |
+-------|-------+
|   Human       |
+---------------+
| + getColor()  |
| + talk()      |
+---------------+

        ^
        |
        |
+-------|-------+
|  BlackHuman   |
+---------------+

        ^
        |
        |
+-------|-------+
|  WhiteHuman   |
+---------------+

        ^
        |
        |
+-------|-------+
|  YellowHuman  |
+---------------+
[/code]

工厂方法模式类图:

[/code]
+------------------+
|   AbstractHumanFactory   |
+------------------+
| + createHuman()  |
+------------------+

        ^
        |
        |
+-------|-------+
|   HumanFactoryA   |
+------------------+

        ^
        |
        |
+-------|-------+
|   HumanFactoryB   |
+------------------+

        ^
        |
        |
+-------|-------+
|   HumanFactoryC   |
+------------------+

        ^
        |
        |
+-------|-------+
|   Human       |
+---------------+
| + getColor()  |
| + talk()      |
+---------------+

        ^
        |
        |
+-------|-------+
|  BlackHuman   |
+---------------+

        ^
        |
        |
+-------|-------+
|  WhiteHuman   |
+---------------+

        ^
        |
        |
+-------|-------+
|  YellowHuman  |
+---------------+
[/code]

抽象工厂模式类图:

[/code]
+------------------+
|     AbstractFactory     |
+------------------+
| + createBlackHuman()    |
| + createWhiteHuman()    |
| + createYellowHuman()   |
+------------------+

        ^
        |
        |
+-------|-------+
|   FactoryA            |
+------------------+

        ^
        |
        |
+-------|-------+
|   FactoryB            |
+------------------+

        ^
        |
        |
+-------|-------+
|   FactoryC            |
+------------------+

        ^
        |
        |
+-------|-------+
|   Human       |
+---------------+
| + getColor()  |
| + talk()      |
+---------------+

        ^
        |
        |
+-------|-------+
|  BlackHuman   |
+---------------+

        ^
        |
        |
+-------|-------+
|  WhiteHuman   |
+---------------+

        ^
        |
        |
+-------|-------+
|  YellowHuman  |
+---------------+
[/code]

以上是简单工厂模式、工厂方法模式和抽象工厂模式的类图设计。简单工厂模式中,只有一个工厂类负责创建不同种类的人;工厂方法模式中,每个具体工厂类负责创建一种人种;抽象工厂模式中,每个具体工厂类负责创建一组相关的产品(人种)。

下面是使用三种工厂方法模式实现女娲造人的示例代码:

简单工厂模式实现:

[/code]java
public interface Human {
    String getColor();
    void talk();
}

public class BlackHuman implements Human {
    @Override
    public String getColor() {
        return "Black";
    }

    @Override
    public void talk() {
        System.out.println("Black human is talking.");
    }
}

public class WhiteHuman implements Human {
    @Override
    public String getColor() {
        return "White";
    }

    @Override
    public void talk() {
        System.out.println("White human is talking.");
    }
}

public class YellowHuman implements Human {
    @Override
    public String getColor() {
        return "Yellow";
    }

    @Override
    public void talk() {
        System.out.println("Yellow human is talking.");
    }
}

public class HumanFactory {
    public static Human createHuman(String type) {
        if (type.equals("black")) {
            return new BlackHuman();
        } else if (type.equals("white")) {
            return new WhiteHuman();
        } else if (type.equals("yellow")) {
            return new YellowHuman();
        }
        return null;
    }
}

public class Main {
    public static void main(String[] args) {
        Human blackHuman = HumanFactory.createHuman("black");
        blackHuman.talk();  // Output: Black human is talking.

        Human whiteHuman = HumanFactory.createHuman("white");
        whiteHuman.talk();  // Output: White human is talking.

        Human yellowHuman = HumanFactory.createHuman("yellow");
        yellowHuman.talk();  // Output: Yellow human is talking.
    }
}
[/code]

工厂方法模式实现:

[/code]java
public interface Human {
    String getColor();
    void talk();
}

public class BlackHuman implements Human {
    @Override
    public String getColor() {
        return "Black";
    }

    @Override
    public void talk() {
        System.out.println("Black human is talking.");
    }
}

public class WhiteHuman implements Human {
    @Override
    public String getColor() {
        return "White";
    }

    @Override
    public void talk() {
        System.out.println("White human is talking.");
    }
}

public class YellowHuman implements Human {
    @Override
    public String getColor() {
        return "Yellow";
    }

    @Override
    public void talk() {
        System.out.println("Yellow human is talking.");
    }
}

public interface HumanFactory {
    Human createHuman();
}

public class BlackHumanFactory implements HumanFactory {
    @Override
    public Human createHuman() {
        return new BlackHuman();
    }
}

public class WhiteHumanFactory implements HumanFactory {
    @Override
    public Human createHuman() {
        return new WhiteHuman();
    }
}

public class YellowHumanFactory implements HumanFactory {
    @Override
    public Human createHuman() {
        return new YellowHuman();
    }
}

public class Main {
    public static void main(String[] args) {
        HumanFactory blackHumanFactory = new BlackHumanFactory();
        Human blackHuman = blackHumanFactory.createHuman();
        blackHuman.talk();  // Output: Black human is talking.

        HumanFactory whiteHumanFactory = new WhiteHumanFactory();
        Human whiteHuman = whiteHumanFactory.createHuman();
        whiteHuman.talk();  // Output: White human is talking.

        HumanFactory yellowHumanFactory = new YellowHumanFactory();
        Human yellowHuman = yellowHumanFactory.createHuman();
        yellowHuman.talk();  // Output: Yellow human is talking.
    }
}
[/code]

抽象工厂模式实现:

[/code]java
public interface Human {
    String getColor();
    void talk();
}

public class BlackHuman implements Human {
    @Override
    public String getColor() {
        return "Black";
    }

    @Override
    public void talk() {
        System.out.println("Black human is talking.");
    }
}

public class WhiteHuman implements Human {
    @Override
    public String getColor() {
        return "White";
    }

    @Override
    public void talk() {
        System.out.println("White human is talking.");
    }
}

public class YellowHuman implements Human {
    @Override
    public String getColor() {
        return "Yellow";
    }

    @Override
    public void talk() {
        System.out.println("Yellow human is talking.");
    }
}

public interface AbstractFactory {
    Human createBlackHuman();
    Human createWhiteHuman();
    Human createYellowHuman();
}

public class FactoryA implements AbstractFactory {
    @Override
    public Human createBlackHuman() {
        return new BlackHuman();
    }

    @Override
    public Human createWhiteHuman() {
        return new WhiteHuman();
    }

    @Override
    public Human createYellowHuman() {
        return new YellowHuman();
    }
}

public class FactoryB implements AbstractFactory {
    @Override
    public Human createBlackHuman() {
        return new BlackHuman();
    }

    @Override
    public Human createWhiteHuman() {
        return new WhiteHuman();
    }

    @Override
    public Human createYellowHuman() {
        return new YellowHuman();
    }
}

public class FactoryC implements AbstractFactory {
    @Override
    public Human createBlackHuman() {
        return new BlackHuman();
    }

    @Override
    public Human createWhiteHuman() {
        return new WhiteHuman();
    }

    @Override
    public Human createYellowHuman() {
        return new YellowHuman();
    }
}

public class Main {
    public static void main(String[] args) {
        AbstractFactory factoryA = new FactoryA();
        Human blackHuman = factoryA.createBlackHuman();
        blackHuman.talk();  // Output: Black human is talking.

        AbstractFactory factoryB = new FactoryB();
        Human whiteHuman = factoryB.createWhiteHuman();
        whiteHuman.talk();  // Output: White human is talking.

        AbstractFactory factoryC = new FactoryC();
        Human yellowHuman = factoryC.createYellowHuman();
        yellowHuman.talk();  // Output: Yellow human is talking.
    }
}
[/code]

三种工厂方法模式的优缺点总结:

- 简单工厂模式:
  - 优点:简单工厂模式实现简单,适用于创建对象较少且变化不频繁的情况。
  - 缺点:当需要增加新的人种类时,需要修改工厂类的代码,不符合开闭原则。

- 工厂方法模式:
  - 优点:每个具体工厂类负责创建一种产品,符合单一职责原则;增加新的产品时,只需增加相应的具体工厂类,不需要修改已有代码,符合开闭原则。
  - 缺点:增加了类的个数,增加了系统的复杂度。

- 抽象工厂模式:
  - 优点:每个具体工厂类负责创建一组相关的产品,符合单一职责原则;增加新的产品族时,只需增加相应的具体工厂类,不需要修改已有代码,符合开闭原则。
  - 缺点:增加了类的个数,增加了系统的复杂度。

根据具体的需求和设计考虑,选择适合的工厂方法模式。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 01:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表