2

在我的项目中,我有 4 层演示文稿、BL、DL 和 dataObjects。我想实现抽象工厂模式来获得我想要的对象(医生/工程师)。下面的代码是否实现了工厂模式?

 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 回答 2

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()
        {}
    }
}
于 2012-05-17T06:15:03.363 回答
0

它似乎具有该模式的所有元素,但是您的 IProfession 是空的。我将假设这只是一个占位符,您将用一些代表所有职业共有的行为的方法来填充它。与 Allen Holub 的书中给出的示例进行对比, 他提到 Collection 作为 AbstractFactory,Iterator 作为抽象产品,Tree 作为具体工厂,迭代器作为具体产品返回。

于 2012-05-22T15:01:16.003 回答