3

I was wondering if it makes sense to have objects inherit from a class that implements the interface when dealing with dependency injections example

public interface IPeople
    {
        string Name { get; set; }
        int Age { get; set; }
        string LastName { get; set; }

    }

public class CPeople : IPeople
    {..implemented IPeople Methods..}

This way i only need to implement the interface in one place. I'm just not sure if this would be considered loosly coupled.

public class Dad : CPeople
    {
    }



public class Mom : CPeople
    {
    }

so that inside my controller i would have

public class Parent

{

    IPeople objMom;
    IPeople objDad;
    Parents m_Parent;

    public void factoryMethod()
    {

        objMom = new Mom();
        objMom.Age = 32;
        objMom.Name = "Jane";
        objMom.LastName = "Doe";

        objDad = new Dad();
        objDad.Age = 25;
        objDad.Name = "John";
        objDad.LastName = "Doe";

        m_Parent = new Parents(objMom,objDad);

    }
    public override string ToString()
    {                      
        return m_Parent.Mom.Name + " " + m_Parent.Mom.LastName +  " is " + m_Parent.Mom.Age + " years of age, " + m_Parent.Dad.Name + " " + m_Parent.Dad.LastName + " aged " + m_Parent.Dad.Age.ToString();
    }
alpha
  • 487
  • 1
  • 6
  • 8
  • 13
    Please, please, please, do not use hungarian notation in C#. It makes no sense, since *everything* are classes and objects, and it just hurts to look at it. – Konamiman Dec 13 '09 at 20:16
  • If your Mom and Dad class are identical, why both creating two separate classes for it? You could achieve the same by having one class with a bool/enum saying whether it is the mother or father. Maybe your example is too simplified to be meaningful? – Mark Byers Dec 13 '09 at 20:17
  • 1
    @Konamiman: There are times when Hungarian notation is useful (apps Hungarian vs System Hungarian - http://en.wikipedia.org/wiki/Hungarian_notation#Systems_vs._Apps_Hungarian). I agree though that this is not one of them. – Mark Byers Dec 13 '09 at 20:20
  • 1
    @Mark Byers,@Konamiman - 99% of people misuse Hungarian notation unfortunately. A lot of people use it to show type in statically typed languages when the original use of Hungarian notation was to show where it was being used (App Hungarian notation). – AutomatedTester Dec 13 '09 at 20:27
  • 4
    Easy, folks. Hungarian notation is one of those things that goes in and out of favor every few years and gets endlessly discussed as if it's important. – Mike Dunlavey Dec 13 '09 at 20:37
  • You may want to take a look at the discussion of [this related question](http://stackoverflow.com/questions/1231985/when-to-use-interfaces-or-abstract-classes-when-to-use-both). – Mark Seemann Dec 13 '09 at 20:31

1 Answers1

3

Yes, this is considered loosely coupled since the controller does not need to have any knowledge of the inner objects beyond the interface definition.

If you care that Mom/Dad be kept separate, you could implement interfaces for just those (even if they are empty) and use those to ensure that parents is both a IMom and an IDad.

AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
GrayWizardx
  • 19,561
  • 2
  • 30
  • 43