将此传递给基本构造函数

问题描述 投票:4回答:3

我正在尝试为正在编写的程序实现良好的设计模式。我有一个这样的类结构。

abstract class SomeBase
{
     public SomeObject obj { get; protected set; } 

     protected SomeBase(SomeObject x)
     {
           obj = x;
     }

     //Other methods and fields...
}

public class SomeDerived : SomeBase
{

     public SomeDerived() : base(new SomeObject(this))
     {

     }

}

现在,我确定您知道,您不能在基本构造函数中传递此参数,因为该对象此时尚未初始化。无论如何,我真的希望有一个解决方法。对于我来说,让SomeDerived()处理基类字段的设置不是最佳实践。我想将此新对象传递给整个链。

c# constructor derived-class base-class
3个回答
4
投票

这是不可能的,请在构造函数之后使用Init方法:

 abstract class SomeBase
 {
      private SomeObject _obj { get; set; } 
      public SomeObject obj 
      {
           get 
           {    // check _obj is inited:
                if (_obj == null) throw new <exception of your choice> ;
                return _obj;
           } 
      }

      protected SomeBase()
      {
           obj = null;
      }

      protected void Init()
      {
           obj = x;
      }

      //Other methods and fields...
 }

 public class SomeDerived : SomeBase
 {

      public SomeDerived() : base()
      {
           Init(new SomeObject(this));
      }

 }

0
投票

嗯,实际上您已经在基本构造函数中拥有它,因此不需要传递它。

abstract class SomeBase
{
    public SomeObject obj { get; protected set; }

    protected SomeBase()
    {
        obj = (SomeObject)Activator.CreateInstance(typeof(SomeObject), this); // "this" here is SomeDerived object
    }
}

class SomeDerived : SomeBase
{
    public SomeDerived()
    {
    }
}

class SomeObject
{
    public SomeObject(SomeDerived obj)
    {
        if (obj.obj == null)
        {
            // You have the reference to SomeDerived here
            // But its properties are not yet initialized (both SomeDerived and SomeBase constructors are in the progress of execution)
            // So you should not access them in the SomeObject class constructor
        }
    }
}

-3
投票

1)构造函数在设计上根本是错的-看起来像实例方法,但实际上这是一半实例的一半方法。

2)“具有模式的良好设计程序”不会在聚合中导致类之间的直接循环依赖,正如我们在此处看到的那样-两个类都必须在创建时知道彼此并使用(!!!),他们知道SomeObject对“ this”进行的操作在它的构造函数中???

所以在“模式”中有两个问题-类之间的高度依赖关系以及初始化逻辑的不可用封装。因此,我们必须找到解决问题的“模式”方法...嗯..该怎么做...

在代码中提供,我看到派生类只是为属性obj提供了它自己的逻辑您可以将其重写为自动初始化的属性

public abstract class MyClass{
     private SomeObject _obj ; 
     public SomeObject Obj {get { return  _obj ?? (_obj = InitializeObj() );}} //no setter needed
     protected abstract SomeObject InitializeObj();
}

public class MyRealClass:MyClass {
    protected override SomeObject InitializeObj(){
        return new VerySpecialSomeObject(this, other, another, 1, 2 , false, option: new Options());
    }   
}

在您的示例中,这种解决方案提供了一个可以胜出的“模式”-“多态”)),并获得了额外的好处-如果“ Obj”将不可用-永远不会创建)))))))))>

© www.soinside.com 2019 - 2024. All rights reserved.