本文介绍了C#中的工厂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点理解工厂模式并想出了这个实现.

I was a bit able to understand Factory Pattern and came up with this implementation.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the fruit name to get the Fruit!");
        string fruit = Console.ReadLine();

        FruitsList fruits;
        if (Enum.TryParse(fruit, true, out fruits))
        {
            var fruitWeight = GetFruitWeight(fruits);
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("Fruit Name is undefined!");
            Console.ReadLine();
        }
    }

    private static object GetFruitWeight(FruitsList fruitNumber)
    {
        switch (fruitNumber)
        {
            case FruitsList.Banana:
                return new Banana();
            case FruitsList.Apple:
                return new Apple();
        }

        return null;
    }
}

internal class Banana : IFruits
{
    public float ReturnFruitWeight()
    {
        return (float)10.00;
    }
}

public interface IFruits
{
    float ReturnFruitWeight();
}

public class Apple : IFruits
{
    public float ReturnFruitWeight()
    {
        return (float)30.00;
    }
}

public enum FruitsList
{
    Apple,
    Banana,
}

我的整个想法(从理论理解)是 GetFruitWeights 函数应接受枚举类型,然后返回水果重量.我能够获取特定 Fruit 的对象,但现在坚持如何从 Fruit 对象获取水果的重量.

The whole idea of mine (from the theoretical understanding) is that the GetFruitWeights function shall accept an enum type and then return the fruit weight. I was able to get the object of a particular Fruit but now stuck up with how from the Fruit object, I get the weight of the fruit.

另外,添加到我的疑问列表中,我在这个实现中真的遵循工厂模式吗?而且,互联网上的一些材料也使用抽象类?我应该遵循什么?接口实现还是我应该使用抽象类并覆盖它?

Also, to add to my doubts list, Am I really following Factory pattern in this implementation? And also, some materials in the internet uses an abstract class too ? What should I follow ? A interface implementation or should I use an abstract class and override it ?

预先感谢您的帮助.

推荐答案

如果您不打算在返回时使用 fruit 对象,那么我将直接返回重量而不是水果对象.工厂方法名称实际上确实说它返回的是重量,而不是对象,所以理论上这是正确的方法,即

If you have no intention of using the fruit object when it's returned then I would simply return the weight directly rather than the fruit object. The factory method name actually does say it returns the weight, not the object, so in theory it's the correct approach i.e.

const float AppleWeight = 10;
const float BananaWeight = 10.4;
...
public static float GetFruitWeight(FruitList fruitType)
{
    switch (fruitType)
    {
        case FruitsList.Apple:
            return AppleWeight;
        case FruitsList.Banana:
            return BananaWeight;
        default:
            return 0;
    }
}

但是,如果您确实打算使用 fruit 对象,那么我会将您的方法重命名为 GetFruit,返回 IFruits(不要box) 并在 fruit 实例上调用 ReturnFruitWeight 以获得重量.

However, if you do intend to use the fruit object then I would rename your method to GetFruit, return IFruits (don't box) and call ReturnFruitWeight on the fruit instance for the weight.

在这个实现中我真的遵循工厂模式吗?

工厂模式的重点是允许您在不知道具体类型的情况下创建对象,所以是的,您在这里拥有的是工厂方法模式的一个相当基本的示例.

The point of the factory pattern is to allow you to create objects without knowing the concrete type, so yes what you have here is a fairly basic example of the factory method pattern.

互联网上的一些材料也使用抽象类?我应该遵循什么?接口实现还是我应该使用抽象类并覆盖它?

这完全取决于您的应用程序设计.例如,我个人只会在以下情况下引入抽象基类:

This is all dependant on your application design. For example, I personally would only introduce an abstract base class if:

  • 我可以在各个类之间共享通用代码
  • 我需要某种方法来确定特定类属于属于特定类型的家族,即 Banana 是一种 Fruit.
  • There was common code I could share across the classes
  • I needed some way of determining that a particular class belonged to a family of a particular type i.e. Banana is a type of Fruit.

除此之外,我可能几乎总是采用接口类型方法.请记住,您可以同时拥有两者,没有理由不能拥有支持特定接口的抽象基类...

Other than that, I would probably almost always go for an interfaced type approach. Bare in mind, you can have both, there's no reason why you couldn't have an abstract base class which supports a particular interface...

这篇关于C#中的工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:40