C#设计模式之0-简单工厂模式

简单工厂模式(Simple Factory Pattern)

简单工厂模式属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GoF设计模式之一。学习简单工厂模式是学习其它工厂模式的前提条件。

简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。

角色:

1、工厂(Creator)

简单工厂模式的核心,它负责实现创建所有实例的内部逻辑。工厂类的创建产品类的方法可以被外界直接调用,创建所需的产品对象;

2、抽象产品(Product)

简单工厂模式所创建的所有对象的抽象基类,它负责描述所有实例所共有的公共接口;

3、具体产品(Concrete Product)

这是简单工厂模式的创建目标,所有创建的对象都是充当这个角色的某个具体类的实例。

示例:

命名空间SimpleFactory中包含抽象水果基类Fruit、三个具体水果类、水果工厂类FruitFactory、未知水果异常类UnknowFruitException。本案例将向大家展示如何使用简单工厂模式来生产不同种类的水果。

namespace SimpleFactory
public abstract class Fruit {

    protected Color Color { get; set; } = Color.Orange;

    protected abstract string BornInfo();

    protected const string LINE_BREAK =
        "------------------------------" +
        "------------------------------";
    //文章排版需要,故折成2行


    public void WhatsFruit() {
        Console.WriteLine("Printing!");
        OnPrint();
        Console.WriteLine("Printed!");
        OnPrinted();
    }

    protected virtual void OnPrint() {
        Console.WriteLine($"I'm a(n) {this.ToString().Replace(nameof(SimpleFactory) + ".", "")} " +
                            $"with {Color.ToString()} and I born in {BornInfo()}!");
    }

    protected virtual void OnPrinted() {
        Console.WriteLine(LINE_BREAK);
    }

}

抽象水果基类Fruit 包含颜色Color属性、出生信息BornInfo抽象方法、这是什么水果WhatsFruit方法、打印水果OnPrint和打印完OnPrinted方法。

public class Apple : Fruit {

    public Apple() {
        Color = Color.Red;
    }

    protected override string BornInfo() => "summer";

    protected override void OnPrint() {
        Console.WriteLine($"I'm an apple with {Color.ToString()},not an IPhone!");
    }

}
public class Orange : Fruit {

    protected override string BornInfo() => "autumn";

    protected override void OnPrinted() {
        Console.WriteLine("override OnPrinted()!");
        Console.WriteLine(LINE_BREAK);
    }

}
public class Pear : Fruit {

    public Pear() {
        Color = Color.Yellow;
    }

    protected override string BornInfo() => "China";

}

具体水果类,苹果Apple类、橘子Orange类和梨子Pear类,各自实现或重写不同的构造函数、抽象方法、虚拟方法和属性。

public enum FruitType {
    Unknow = -1,
    Apple,
    Orange,
    Pear
}
public static class FruitFactory {

    public static Fruit CreateFruit(FruitType type) {
        Fruit fruit = null;
        switch (type) {
            case FruitType.Apple:
                fruit = new Apple();
                break;
            case FruitType.Orange:
                fruit = new Orange();
                break;
            case FruitType.Pear:
                fruit = new Pear();
                break;
            default:
                throw new UnknowFruitException();
        }

        return fruit;
    }

}

水果工厂类FruitFactory,该类是简单工厂的核心类,包含CreateFruit方法,传递FruitType参数以便确定产出何种水果。方法返回抽象水果基类,以便调用方使用基类变量接受返回值。

public class UnknowFruitException : Exception {

    public UnknowFruitException()
        : base("Not Supported Fruit!") {

    }

    public UnknowFruitException(string message, Exception innerException)
        : base(message, innerException) {

    }

}

使用未知水果异常类UnknowFruitException,进行简单的异常处理。

public static void Main(string[] args) {
    try {
        var fruit = FruitFactory.CreateFruit(FruitType.Pear);
        fruit.WhatsFruit();

        fruit = FruitFactory.CreateFruit(FruitType.Apple);
        fruit.WhatsFruit();

        fruit = FruitFactory.CreateFruit(FruitType.Orange);
        fruit.WhatsFruit();

        fruit = FruitFactory.CreateFruit(FruitType.Unknow);
        fruit.WhatsFruit();
    }
    catch (UnknowFruitException ex) {
        Console.WriteLine(nameof(UnknowFruitException) + ":" + ex.Message);
    }
    catch (Exception ex) {
        Console.WriteLine(nameof(Exception) + ":" + ex.Message);
    }

    Console.ReadKey();
}

调用方用变量fruit接受水果工厂不同的产出,由WhatsFruit方法在控制台打印出水果信息,用catch分别处理不同类型的异常。以下是这个案例的输出结果:

Printing!
I'm a(n) Pear with Color [Yellow] and I born in China!
Printed!
-----------------------------------------------------------
Printing!
I'm an apple with Color [Red],not an IPhone!
Printed!
-----------------------------------------------------------
Printing!
I'm a(n) Orange with Color [Orange] and I born in autumn!
Printed!
override OnPrinted()!
-----------------------------------------------------------
UnknowFruitException:Not Supported Fruit!

优点:

1、工厂类是整个模式最核心的部分,包含了对象的创建逻辑。调用者无需关心对象创建的细节,只要给出参数,就可以得到相应的对象;
2、他们之间相互独立,各司其职,有利于整个软件体系架构。

缺点:

1、由于工厂类集中了所有实例的创建逻辑,违反了高内聚责任分配原则;
2、将全部创建逻辑集中到了一个工厂类中,如果需要添加新的类,则就需要改变工厂类了,明显违反开闭原则。工厂方法模式中这个问题有一定程度的缓解。

使用场景:

1、工厂类负责创建的对象比较少;
2、客户只知道传入工厂类的参数,对于创建对象的细节不关心或无法知道;
3、由于简单工厂很容易违反高内聚责任分配原则,所以在实际开发中很少用到。

猜你喜欢

转载自blog.csdn.net/qq_31116753/article/details/81096030