Dofactory.com
Dofactory.com
 Back to list
Views:   36.8K
Replies:  7
Archived

Here is an example Design Pattern interview question

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
Ravi Shiggavi, May 08, 2012
Reply 1
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.
 
Sri C, Feb 20, 2013
Reply 2
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();
}
}

 
D S, Oct 19, 2012
Reply 3


Vijayanand R, Aug 19, 2012
Reply 4
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);
}

 
J Greggs, Aug 13, 2012
Reply 5
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 :)))
sreenivasa g ediga ediga, Aug 08, 2012
Reply 6
Ouch, that hurt my brain.
Michael Lopez, Aug 04, 2012
Reply 7
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
Tarriq Ferrose Khan, Jun 21, 2012
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.