在我的項目中,我有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
}
是的,它似乎...此外,爲什麼你需要確定?模式的目的是解決問題。如果設計解決了你的問題,那就沒問題。 – ivowiblo
我會同意@ivowiblo,閱讀這篇文章http://gleichmann.wordpress.com/2007/11/21/are-you-pattern-happy/ – Habib
我對設計模式知之甚少。它正在解決我的問題..因爲我第一次使用它,爲什麼要確認。 – Annie