将父类的依赖与子类的依赖解耦

huangapple go评论59阅读模式
英文:

Decouple parent class dependencies from child's dependencies

问题

我有一个父类,让我们称之为BaseClass

我有多个子类扩展了这个BaseClass。这些子类覆盖了BaseClass的一些方法。

public class BaseClass {

}

public class ChildA extends BaseClass{

}
....

public class ChildN extends BaseClass{

}

现在,我在BaseClass中有一些经常变化的逻辑。这通常导致为BaseClass添加依赖项 - 这个BaseClass的构造函数参数不断增加。

public class BaseClass {
  public BaseClass(BaseDependencyOne one, BaseDependencyTwo two...., BaseDependencyN n) {
  }
}

public class ChildA {
  public ChildA (BaseDependencyOne one, BaseDependencyTwo two,..., BaseDependencyN n){
    super(one, two,..., n);
  }
}

由于BaseClass构造函数被修改以容纳更多的依赖项,子类也必须容纳这些依赖项。
这涉及对所有子类进行更改,而子类的数量非常大。这又导致了在BaseClass中添加一个依赖项时发生巨大的变化。

我考虑过将父类BaseClass作为所有子类的依赖项。这将解耦父类与子类之间的依赖关系。
但是,这将破坏父子关系,因此我将失去子类的覆盖行为。

英文:

I have a parent class, let's call it BaseClass

And I have multiple child classes extending this BaseClass. These child classes overrides some of the methods of the BaseClass.

public class BaseClass {

}

public class ChildA extends BaseClass{

}
....

public class ChildN extends BaseClass{

}

Now, I have logic in BaseClass which keeps on changing quite often. This usually leads to adding dependencies for the BaseClass - the constructor arguments of this BaseClass keeps on increasing.

public class BaseClass {
  public BaseClass(BaseDependencyOne one, BaseDependencyTwo two...., BaseDependencyN n) {
  }
}

public class ChildA {
  public ChildA (BaseDependencyOne one, BaseDependencyTwo two,..., BaseDependencyN n){
    super(one, two,..., n);
  }
}

Since, the BaseClass constructor is modified to accommodate more dependencies, the child classes also have to accommodate these dependencies.
This involves making change in all child classes, which are very large in number. This in turn results in a huge change for a single addition of dependency in BaseClass

I thought of making the parent BaseClass as a dependency for all child classes. It will decouple the dependency of parent from child.
But, this will break the parent-child relation ship, therefore, I won't have the overriding behaviour of child.

答案1

得分: 1

以下是翻译的内容:

可以注入一个包含你的类所有依赖项的接口。它可以称为一个收集器。你可以通过阅读Mark Seemann的这篇出色文章来深入了解这个优秀的技巧。

让我通过C#来举个例子。

public interface IFooCollector
{
    DependencyOne One { get; set; }

    DependencyTwo Two { get; set; }

    DependencyThree Three { get; set; }
}

然后是你的依赖类:

public class DependencyOne {}

public class DependencyTwo { }

public class DependencyThree { }

接着你的基类只会有如下依赖项:

public class BaseClass
{
    IFooCollector _myDependencies;

    public BaseClass(IFooCollector myDependencies)
    {
        _myDependencies = myDependencies;
    }
}

可以看到,如果你向IFooCollector中添加新的依赖项,就无需编辑所有BaseClass的派生类中的依赖项。

英文:

It is possible to inject an interface which has all dependencies of your classes. It can be called a collector. You can immerse in this great technique by reading this outstanding article by Mark Seemann.

Let me show an example via C#.

public interface IFooCollector
{
	DependencyOne One { get; set; }

	DependencyTwo Two { get; set; }

	DependencyThree Three { get; set; }
}

Andd your dependency classes:

public class DependencyOne {}

public class DependencyTwo { }

public class DependencyThree { }

And then your BaseClass will have just the following dependency:

public class BaseClass
{
	IFooCollector _myDependencies;

	public BaseClass(IFooCollector myDependencies)
	{
		_myDependencies = myDependencies;
	}
}

It can be seen that there is no need to edit dependencies in all derived classes of BaseClass if you add new dependency to IFooCollector

huangapple
  • 本文由 发表于 2023年6月12日 22:36:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定