2012-05-17 57 views
2

在我的項目中,我有4層呈現BL,DL和dataObjects。我想實現抽象工廠模式來獲得我想要的對象(醫生/工程師)。下面的代碼是否實現了工廠模式?工廠模式與4層架構

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 
} 
+2

是的,它似乎...此外,爲什麼你需要確定?模式的目的是解決問題。如果設計解決了你的問題,那就沒問題。 – ivowiblo

+1

我會同意@ivowiblo,閱讀這篇文章http://gleichmann.wordpress.com/2007/11/21/are-you-pattern-happy/ – Habib

+0

我對設計模式知之甚少。它正在解決我的問題..因爲我第一次使用它,爲什麼要確認。 – Annie

回答

1

你的代碼確實實現了模式,但不是充分的程度,其C#允許,換句話說 - 你不使用C#語言的重要優勢。

這裏是你如何可以做的更好的例子:

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() 
     {} 
    } 
} 
+0

這只是一目瞭然,但有一個從ProfessionFactory繼承的醫生聽起來不對。 – Mathias

+0

醫生既是一個專業,也是一個創建醫生的小工廠。 Acalally我會修改任何專業也是一個專業工廠。 –

+0

海事組織,「這個職業是兩件事」(一個職業和一個工廠)的說法有點味道,並且往往表明它不遵循單一責任原則。 – Mathias

0

它似乎有圖案的所有元素,但是你IProfession是空的。我將假定這只是一個佔位符,並且您將用一些代表所有職業共同行爲的方法來填充它。 與Allen Holub的book 中給出的例子相反,他提到Collection作爲AbstractFactory,Iterator作爲抽象產品,Tree作爲具體工廠以及作爲具體產品返回的迭代器。