简体   繁体   English

基类还是普通类?

[英]Base class or common class?

for our asp.net web application v 4.0, we are in process of defining a class that contains methods that are common across the application. 对于我们的asp.net Web应用程序v 4.0,我们正在定义一个包含应用程序中常见方法的类。 to achieve this there are 2 Suggestions within our team.. one to create a base class, define the methods in that and derive all the other classes from that base class.. the other one is to create a seperate class (not a base class) and instantiate that common class in other classes when required to access the common methods. 为了实现这一目标,我们团队中有两个建议。一个用于创建基类,定义其中的方法并从该基类派生所有其他类。另一个是创建一个单独的类(不是基类)并且当需要访问公共方法时,在其他类中实例化该公共类。 Please guide me in identifying the best approach.. 请指导我确定最佳方法..

I'd only go the base class route if there is a real is-a relationship between the base class and the derived classes. 如果基类和派生类之间存在真正的is-a关系,我只会去基类路由。 One reason is that a class can only inherit from a single base class. 一个原因是类只能从单个基类继承。 I'd use this relationship in a sensible way. 我会以明智的方式使用这种关系。 Just sharing some helper methods is not a scenario worth blocking this relationship. 仅仅共享一些辅助方法并不是一种值得阻止这种关系的方案。

If you want to use some helper methods in several classes, composition is the better way as you describe in the 2nd approach. 如果你想在几个类中使用一些辅助方法,那么组合是你在第二种方法中描述的更好的方法。 Instead of creating the objects in the classes, you should think about whether you can inject the instances into the classes (see this link for details on dependency injection), eg: 您应该考虑是否可以实例注入到类中而不是在类中创建对象(有关依赖项注入的详细信息,请参阅此链接 ),例如:

public class HelperClass
{
    public virtual void HelperMethod()
    { 
        // ...
    }
}

public class ClassThatUsesHelper
{
    private readonly HelperClass _helper;

    public ClassThatUsesHelper(HelperClass helper)
    {
        _helper = helper;
    }

    public void DoSomething()
    {
        _helper.HelperMethod();
    }
}

By injecting the helper class you decouple the classes so that you can substitute the helper class by a different implementation that shares the same interface. 通过注入帮助程序类,可以对类进行解耦,以便可以通过共享相同接口的其他实现来替换辅助类。 ClassThatUsesHelper works with any class that is derived from HelperClass (or HelperClass itself of course). ClassThatUsesHelper工作与从派生的任何类HelperClass (或HelperClass本身当然)。 So if you need to decorate the helper method or need a special implementation in some cases, this is possible without any problem. 因此,如果您需要修饰辅助方法或在某些情况下需要特殊实现,则可以毫无问题地进行此操作。

Using composition also enables you to test the helper methods separately. 使用组合还可以分别测试辅助方法。

However, if it is about very basic helper methods, you might also think about having a static class with static helper methods. 但是,如果它是关于非常基本的帮助器方法,您可能还会考虑使用静态辅助方法的静态类 Please note that you introduce a strong dependency between the classes and that you cannot adjust the implementation easily. 请注意,您在类之间引入了强烈的依赖关系,并且您无法轻松调整实现。

public static class HelperClass
{
    public static void HelperMethod()
    { 
        // ...
    }
}

public class ClassThatUsesHelper
{
    public void DoSomething()
    {
        HelperClass.HelperMethod();
    }
}

Your question is vague, but if you need a method which all objects in your program will need to have access to, that uses their member variables, then I wold recommend creating an abstract class upon which your objects are based. 你的问题很模糊,但是如果你需要一个方法,你的程序中的所有对象都需要访问它,使用它们的成员变量,那么我建议你创建一个你的对象所基于的抽象类。

If you need a means of performing some sort of calculation from anywhere in your code, just create a public static method in a class meant for the purpose. 如果您需要在代码中的任何位置执行某种计算,只需在专门用于此目的的类中创建公共静态方法。 MyMathClass.InterestingFourierTransform() , for example. 例如, MyMathClass.InterestingFourierTransform()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM