我想问的是,类Genotypes和Individual的实现是否违反了依赖反转原则?如果是的话,如何解决呢?

以下是代码:
public interface IGenotype
{
//some code...
}
public abstract class AIndividual
{
// ... some code
public IGenotype Genotype { get; set;} // DIP likes that !
}
public class Individual : AIndividual
{
public Individual()
{
// some code ...
this.Genotype = new Genotype(); // Is this ok in case of DIP?
}
}
public class Genotype : IGenotype
{
// ... some code
}发布于 2016-04-29 10:45:46
我希望这能有所帮助(请阅读评论)。
public interface IGenotype
{
//some code...
}
public class Genotype : IGenotype
{
// ... some code
}
public class Individual
{
// Here, instead of depending on a implementation you can inject the one you want
private readonly IGenotype genotype;
// In your constructor you pass the implementation
public Individual(IGenotype genotype)
{
this.genotype = genotype;
}
}
// Genotype implements IGenotype interface
var genotype = Genotype();
// So here, when creating a new instance you're injecting the dependecy.
var person = Individual(genotype);你不需要抽象类
发布于 2016-04-29 10:49:41
https://stackoverflow.com/questions/36935471
复制相似问题