鱼C论坛

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

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

[复制链接]
发表于 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-10-3 03:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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