“产品”中的抽象方法- 工厂方法 C#

发布于 2024-08-25 07:28:20 字数 810 浏览 6 评论 0原文

我有一个用 C# 编写的简单类库(COM+ 服务)来使用 5 个 Web 服务:加、减、除、乘和比较。

我创建了抽象产品和抽象工厂类。名为 WS 的抽象产品的代码:

public abstract class WS
{

    public abstract double Calculate(double a, double b);

    public abstract string Compare(double a, double b);
}  

如您所见,当其中一个子类继承 WS 时,必须重写这两个方法,这在某些子类中可能没有用。例如Compare不需要Calculate()方法。

为了实例化一个新的 CompareWS 对象,客户端类将调用 CreateWS() 方法,该方法返回 WS 对象类型。

public class CompareWSFactory : WSFactory
{
    public override WS CreateWS()
    {
        return new CompareWS();
    }
}

但如果 WS 中未定义 Compare(),则无法调用 Compare() 方法。这是因为客户端将使用 WS 类型对象,而不是 CompareWS 对象。

这只是一个有两个方法的例子,但是如果有更多的方法呢?在 WS 类中将所有方法定义为抽象方法是不是很愚蠢?

我的问题是:我想定义 WS 的所有子类所共有的抽象方法,而当工厂创建 WS 对象类型时,可以调用子类的所有方法(重写 WS 的方法以及子类中的方法)。我该怎么做?

I have a simple class library (COM+ service) written in C# to consume 5 web services: Add, Minus, Divide, Multiply and Compare.

I've created the abstract product and abstract factory classes. The abstract product named WS's code:

public abstract class WS
{

    public abstract double Calculate(double a, double b);

    public abstract string Compare(double a, double b);
}  

As you see, when one of the subclasses inherits WS, both methods must be overridden which might not be useful in some subclasses. E.g. Compare doesn't need Calculate() method.

To instantiate a new CompareWS object, the client class will call the CreateWS() method which returns a WS object type.

public class CompareWSFactory : WSFactory
{
    public override WS CreateWS()
    {
        return new CompareWS();
    }
}

But if Compare() is not defined in WS, the Compare() method cannot be invoked. It's because the client will make use of WS type object, not CompareWS object.

This is only an example with two methods, but what if there are more methods? Is it stupid to define all the methods as abstract in the WS class?

My question is: I want to define abstract methods that are common to all subclasses of WS whereas when the factory creates a WS object type, all the methods of the subclasses can be invoked (overridden methods of WS and also the methods in subclasses). How should I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一江春梦 2024-09-01 07:28:20

Regina,抽象类可以定义抽象方法和虚拟方法。虚拟方法将在基类中包含一些默认实现,但可以根据需要由派生类覆盖:

    public abstract class WS
{

    // All derived classes will have to implement this
    public abstract double Calculate(double a, double b);

    // This can be overriden by derived classes (since it's a virtual method)
    public virtual string Compare(double a, double b)
    {
        // Some default implementation.
        // Any derived classes can override this implementation if they want
    }
}  

Regina, abstract classes can define both abstract and virtual methods. Virtual methods will contain some default implementation in the base class, but can be overriden by derived classes as needed:

    public abstract class WS
{

    // All derived classes will have to implement this
    public abstract double Calculate(double a, double b);

    // This can be overriden by derived classes (since it's a virtual method)
    public virtual string Compare(double a, double b)
    {
        // Some default implementation.
        // Any derived classes can override this implementation if they want
    }
}  
离笑几人歌 2024-09-01 07:28:20

您可以在 WS 类中声明所有子类共有的所有方法,但您应该仅声明那些其实现将在派生类中更改的方法为抽象方法。将 Compare 方法设置为抽象方法是没有意义的。为什么说这样的方法不能调用呢?

您始终可以在一个类中拥有抽象和非抽象方法,并且您的 Compare 方法似乎不需要是抽象的。

You can declare all methods that are common to all subclasses in the WS class, but you should declare abstract only those methods whos implementation will be changed in the derived classes. It does not make sence to make Compare method as astract. Why do you say that such method cannot be called?

You can always have abstract and non abstract methods in one class and it looks like your Compare method just don't need to be abstract.

跨年 2024-09-01 07:28:20

如果您的类继承自抽象类并且不为所有抽象方法提供主体,那么该类也将变为抽象类。解决这个问题的一种方法是使用接口,而不是类!

interface ICalculate
{
   double Calculate(double a, double b);
}
interface ICompare
{
   string Compare(double a, double b);
}
class Compare : ICompare
{
   public double ICompare.Compare(double a, double b) { .... }
}
class CompareAndCalculate : ICompare, ICalculate
{
   public string ICompare.Compare(double a, double b) { .... }
   public double ICalculate.Calculate(double a, double b) {...}
}

If you class inherits from abstract class and does not provide body for all abstract methods, then that class becomes abstract as well. One solution to this problem is to use interfaces, instead of classes!

interface ICalculate
{
   double Calculate(double a, double b);
}
interface ICompare
{
   string Compare(double a, double b);
}
class Compare : ICompare
{
   public double ICompare.Compare(double a, double b) { .... }
}
class CompareAndCalculate : ICompare, ICalculate
{
   public string ICompare.Compare(double a, double b) { .... }
   public double ICalculate.Calculate(double a, double b) {...}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文