2

In my project i have 4 layers presentation,BL,DL, and dataObjects. i want to implement abstract factory pattern to get the object i want(doctor/Engineer). Is the code below implementing factory pattern?

 public interface IProfessional //The Abstract Factory interface. 
{
    IProfession CreateObj();
}

// The Concrete Factory  class1.
public class DocFactory : IProfessional
{
    public IProfession CreateObj()
    {
        return new Doctor();
    }
}

// The Concrete Factory  class2.
public class EngFactory : IProfessional
{
    public IProfession CreateObj()
    {
        // IMPLEMENT YOUR LOGIC
        return new Engineer();
    }
}
// The Abstract Item class
public interface IProfession
{       
}

// The Item class.

public class Doctor : IProfession
{
  public int MedicalSpecialty
 {
  get; set; 
 }
  public int AreaofExpertise
 { 
  get; set; 
  }

}
// The Item class.
public class Engineer : IProfession
{
  public string Title{
     get;set;
}
  public int AreaofExpertise 
{ 
 get; set; 
}


}


// The Client class.
public class AssignProfession
{
    private IProfession _data;

    public AssignProfession(DataType dataType)
    {
        IProfessional factory;
        switch (dataType)
        {
            case DataType.Doc:
                factory = new EngFactory();
                _data = factory.CreateObj();//from here i will get engineer

                break;
            case DataType.Eng:
                factory = new DocFactory();
                _data = factory.CreateObj();//from here i will get doctor

                break;
        }
    }

    public IProfession GiveProfessional()
    {
        return _data;
    }
}

//The DataType enumeration.
public enum DataType
{
    Doc,
    Eng
}
4
  • 2
    Yep, It seems to... Besides that, why do you need to be sure? the purpose of the patterns is to solve problems. If the design solves your problem, then it's ok.
    – Ivo
    May 17, 2012 at 5:07
  • 1
    I will agree with @ivowiblo, read this article as well gleichmann.wordpress.com/2007/11/21/are-you-pattern-happy
    – Habib
    May 17, 2012 at 5:13
  • i know little about design patterns. it is solving my problem..as i am first time using it thts why want to confirm.
    – Annie
    May 17, 2012 at 5:14
  • @shalini please fix your switch statement, there's a logical bug. you are creating Eng against DataType.Doc and Doc against DataType.Eng
    – ABH
    May 17, 2012 at 5:20

2 Answers 2

1

Your code does implement the pattern but not to the full extent which C# allows, in other words - you are not using the important benefits of the C# language.

Here is an example of how you can do it better:

 class Program
{
    static void Main(string[] args)
    {
        var myEngineer = ProfessionFactory.CreateProffession<Engineer>();
        var myDoctor = ProfessionFactory.CreateProffession<Doctor>();
        myEngineer.EnginerringStuff();
        myDoctor.HealingPeople();

        var myEngineer2 = (Engineer)ProfessionFactory.CreateProffession("Engineer");
        //using the other method I still have to cast in order to access Engineer methods.
        //therefore knowing what type to create is essential unless we don't care about engineer specific methods,
        //in that case we can do:
        var myEngineer3 = ProfessionFactory.CreateProffession("Engineer");
        //which is useless unless we start involving reflections which will have its own price..
    }

    public interface IProfessionFactory
    {
        IProfession CreateObj(); 
    }

    public interface IProfession : IProfessionFactory
    {
        string ProfessionName { get; }
    }

    public abstract class ProfessionFactory : IProfessionFactory
    {
        public abstract IProfession CreateObj();

        public static T CreateProffession<T>() where T:IProfessionFactory, new()
        {
            return (T)new T().CreateObj();
        }

        public static IProfession CreateProffession(object dataObj)
        {
            if (dataObj == "Engineer")
                return CreateProffession<Engineer>();
            if (dataObj == "Doctor")
                return CreateProffession<Doctor>();
            throw new Exception("Not Implemented!");
        }
    }

    public class Engineer : IProfession
    {
        public string ProfessionName
        {
            get { return "Engineer"; }
        }

        public IProfession CreateObj()
        {
            return new Engineer();
        }

        public void EnginerringStuff()
        {}
    }

    public class Doctor : IProfession
    {
        public string ProfessionName
        {
            get { return "Doctor"; }
        }

        public IProfession CreateObj()
        {
            return new Doctor();
        }

        public void HealingPeople()
        {}
    }
}
4
  • This is just at a glance, but having a Doctor inherit from a ProfessionFactory doesn't sound right.
    – Mathias
    May 17, 2012 at 6:19
  • Doctor is both a profession and a little factory to create doctors. Acutally I'll revise as any profession is also a profession factory.
    – G.Y
    May 17, 2012 at 6:38
  • IMO, the statement "this class is two things" (a profession, and a factory) is a bit of a smell, and tends to indicate that it doesn't follow the single responsibility principle.
    – Mathias
    May 18, 2012 at 2:05
  • @Mathias the alternative is having a factory class that is able to return new of any profession which inflict to seperation of concerns. so in one side I agree the SRP rule is broken, but I prefer to break SRP rule than to break SOC rule. putting this to the side, i think it's a matter of angle, I can claim that this class is doing one thing: it handles a profession, or I can claim it do 3 diffrent things. if it has not reptitive code and it follow SOC it is good. imo SRP is more of a guideline to achieve SOC.
    – G.Y
    May 21, 2012 at 8:13
0

It does seem to have all elements of the pattern, however your IProfession is empty. I am going to assume that is just a placeholder and you are going to fill it in with some methods that represent a behavior that is common to all professions. Contrast that with the example given in Allen Holub's book He mentions Collection as the AbstractFactory, Iterator as the abstract product, Tree as the concrete factory and the iterators that are returned as concrete products.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.