Here is an example Design Pattern interview question

 
60   95.3
May 08, 2012
 
The question is as follows:

Create a boy and girl factory which have properties and methods of a Child class.
Child which has properties and method of Father and Mother.
However, when creating boy object this object should have only father, child property and methods but not any property and method of monther, vis-a-vis for girl object

How would you implement this using Abstract Factory Pattern and Factory Pattern or any other best practice Design Pattern?

Thanks & Regards,
Ravi Shiggavi



80   96.3
Jun 21, 2012
Hi,

I am not really clear on this question.

What I understand is,
Boy and Girl are abstract classes which calls or uses properties/methods (members) of Child Class (?)
Child Class uses/calls members of Father Class(?) and Mother Class (?)

When I call Boy class , it should only expose Father and / or Child class?

Am I correct ?

Thanks,
Tarriq

65   95.6
Aug 04, 2012
Ouch, that hurt my brain.

Aug 08, 2012
Let me give it a shot....

Create MotherChild and FatherChild classes. Both implement / Inherit from Mother and Father Interface / Class (Python supports multiple inheritance). FatherChild will hide Properties and Methods of Mother from Derived classes. And MotherChild will hide properties and Methods of Father class from derived class (I believe there is a way to do this in all object oriented languages). Boy class inherits FatherChid and Girl Class inherits from Mother Child. 

Hope i made some sense :)))

250   96.9
Aug 13, 2012
C#. Create an abstract Child class (with property Name for example). Boy and Girl both inherit from Child. Create an abstract class creator Parent. Parent has a method ChildFactoryMethod(). You can have BoysParent inherit from Parent and override ChildFactoryMethod to create a boy and GirlsParent doing likewise to create a girl. The concrete implementation of Parent can both instantiate a new Boy/Girl and set the name of the Father/Mother.

You could also do it without an abstract Parent and just have the parent instantiate a boy/girl based on input (eg., FactoryMethod(b), FactoryMethod(g), or CreateChild(b)...)

Make sense?
//I have not run this code as is so may not compile at first, but this is the gist of it.

public abstract class Child
{
     public string Name
    { get; set; }
}

public class Boy:Child
{

}

public class Girl:Child
{
}

public abstract class Parent
{
     public Child CreateChild();
     public string Name
      { get; set;}
}

public class Father:Parent
{
  public override Child CreateChild()
  {
      Boy b  = new Boy();
      b.Name = "Johnny";
      return b;
  }
}

public class Mother:Parent
{
  public override Child CreateChild()
  {
      Girl g = new Girl();
      g.Name = "Jane";
      return g;
  }

}

Main()
{
   Mother m = new Mother{Name = "Janet"};
   Father f = new Father{Name = "John"};

   Child g = m.CreateChild();
   Console.Writeline(m.Name + "'s daughter is" + g.Name);

   Child b = f.CreateChild();
   Console.Writeline(f.Name + "'s daughter is" + b.Name);
}

 

50   50
Aug 19, 2012


 1 comment
 
BoyFactory should return an instance of Boy object. GirlFactory should return an instance of Girl object. The entire design has been made on the assumption that Child has properties of Father and Mother. Child has-a Father Child has-a Mother Boy implements Father interface, so should hide methods of mother Girl implements Mother interface, so should hide methods of father --- Vijayanand R  Aug 19, 2012

D S
50   50
Oct 19, 2012
Just a thought:

Order of Inheritance:
Child - Boy - Father
Child - Girl - Mother

Call the below method:


ChildCreation.Demo();

Code:


 
 
enum Gender { MALE, FEMALE }
abstract class Child
{
public string Name { get; set; }
private Gender _Gender;
public Gender Gender { get { return _Gender; } }
public string FatherName { get; set; }
public string MotherName { get; set; }
public Child(string name, string fatherName, string motherName, Gender gender)
{
this.Name = name;
this.FatherName = fatherName;
this.MotherName = motherName;
this._Gender = gender;
}
public string Details()
{
StringBuilder detail = new StringBuilder();
detail.Append("Name: ");
detail.Append(this.Name);
detail.AppendLine();
detail.Append("Father's Name: ");
detail.Append(this.FatherName);
detail.AppendLine();
detail.Append("Mother's Name: ");
detail.Append(this.MotherName);
detail.AppendLine();
detail.Append("Gender: ");
detail.Append(this.Gender.ToString());
detail.AppendLine();
return detail.ToString();
}
}
interface Parent
{
bool Parent { get; set; }
}
class Boy : Child
{
public Boy(string name, string fatherName, string motherName)
: base(name, fatherName, motherName, Gender.MALE)
{
}
}
class Girl : Child
{
public Girl(string name, string fatherName, string motherName)
: base(name, fatherName, motherName, Gender.FEMALE)
{
}
}
class Mother : Girl, Parent
{
#region Parent Members

public Mother(string name, string fatherName, string motherName)
: base(name, fatherName, motherName)
{
this.Parent = true;
}

#endregion

#region Parent Members

public bool Parent { get; set; }


#endregion
}
class Father : Boy, Parent
{
public Father(string name, string fatherName, string motherName)
: base(name, fatherName, motherName)
{
this.Parent = true;
}
#region Parent Members

public bool Parent { get; set; }

#endregion
}
abstract class ChildFactory
{
abstract public Child CreateChild(string name, Father A, Mother B);
}
class BoyFactory : ChildFactory
{
public override Child CreateChild(string name, Father A, Mother B)
{
return new Boy(name, A.Name, B.Name);
}
}
class GirlFactory : ChildFactory
{
public override Child CreateChild(string name, Father A, Mother B)
{
return new Girl(name, A.Name, B.Name);
}
}
class Mankind
{
private Child _Child;
public Mankind(ChildFactory childFactory, string name, Father A, Mother B)
{
_Child = childFactory.CreateChild(name, A, B);
}
public void ShowRelations()
{
Console.WriteLine(_Child.Details());
}

}
class ChildCreation
{
public static void Demo()
{
// Abstract boy factory
ChildFactory boyFactory = new BoyFactory();
Mankind Mankind1 = new Mankind(boyFactory, "Joy", new Father("Deepak", "Dilip", "Kantha"), new Mother("Leela", "Ram", "Seeta"));
Mankind1.ShowRelations();

// Abstract girl factory
ChildFactory girlFactory = new GirlFactory();
Mankind Mankind2 = new Mankind(girlFactory, "June", new Father("Pintoo", "Philip", "Mary"), new Mother("Julie", "Albert", "Nancy"));
Mankind2.ShowRelations();

Console.ReadKey();
}
}

 

50   50
Feb 20, 2013
Just to answer the part  "However, when creating boy object this object should have only father, child property and methods but not any property and method of monther, vis-a-vis for girl object"


abstract class Child
        { }

        interface IFather
        { }

        interface IMother
        { }

        class Boy : Child, IFather
        { }

        class Girl : Child, IMother
        { }
  

Ain't this a simple implementation !! I don't see any advantage of using abstract factory here.