gpt4 book ai didi

c# - 具有 4 层架构的工厂模式

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:26 25 4
gpt4 key购买 nike

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

 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
}

最佳答案

您的代码确实实现了该模式,但没有达到 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()
{}
}
}

关于c# - 具有 4 层架构的工厂模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10630008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com