C# 具有公共功能的方法的工厂模式
我有几个关于模式的问题,如果你们能帮忙,那就太好了C# 具有公共功能的方法的工厂模式,c#,design-patterns,abstract-factory,C#,Design Patterns,Abstract Factory,我有几个关于模式的问题,如果你们能帮忙,那就太好了 下面有一个工厂模式代码示例(底部代码) 我的问题是- 问题1例如,如果我需要为IPeople实现公共功能,该怎么办- bool GotLegs() { return true; //Always returns true for both People types } 所以,如果我想为村民和城市居民实施这种通用方法,还有其他模式可以实施吗 问题2 有没有一种方法可以直接创建一个IPeople类型,而不是实例化一个对象。例如- IPeo
下面有一个工厂模式代码示例(底部代码) 我的问题是-
bool GotLegs()
{
return true; //Always returns true for both People types
}
所以,如果我想为村民和城市居民实施这种通用方法,还有其他模式可以实施吗IPeople people = Factory.GetPeople(PeopleType.URBAN);
我知道静态不是接口的选项,只是检查一下是否有办法
实际C#控制台参考代码-
//Main Prog
class Program
{
static void Main(string[] args)
{
Factory fact = new Factory();
IPeople people = fact.GetPeople(PeopleType.URBAN);
}
}
//Empty vocabulary of Actual object
public interface IPeople
{
string GetName();
}
public class Villagers : IPeople
{
#region IPeople Members
public string GetName()
{
return "Village Guy";
}
#endregion
}
public class CityPeople : IPeople
{
#region IPeople Members
public string GetName()
{
return "City Guy";
}
#endregion
}
public enum PeopleType
{
RURAL,
URBAN
}
/// <summary>
/// Implementation of Factory - Used to create objects
/// </summary>
public class Factory
{
public IPeople GetPeople(PeopleType type)
{
IPeople people = null;
switch (type)
{
case PeopleType.RURAL:
people = new Villagers();
break;
case PeopleType.URBAN:
people = new CityPeople();
break;
default:
break;
}
return people;
}
}
//主程序
班级计划
{
静态void Main(字符串[]参数)
{
工厂事实=新工厂();
IPeople-people=fact.GetPeople(PeopleType.URBAN);
}
}
//实际对象的空词汇表
公共接口IPeople
{
字符串GetName();
}
公共阶层村民:IPeople
{
#区域人民成员
公共字符串GetName()
{
返回“村姑”;
}
#端区
}
公共类城市人口:IPeople
{
#区域人民成员
公共字符串GetName()
{
返回“城市人”;
}
#端区
}
公共枚举人员类型
{
农村,,
城市的
}
///
///工厂的实现-用于创建对象
///
公营工厂
{
公共IPeople GetPeople(PeopleType类型)
{
i人民=零;
开关(类型)
{
案例PeopleType.RURAL:
人民=新村民();
打破
案例PeopleType.URBAN:
人=新城市人();
打破
违约:
打破
}
还人,;
}
}
问题1:
有几种选择:
GotLegs
成为IPerson
的扩展方法:
public static class PersonExtensions
{
public static bool GotLegs(this IPerson person)
{
return true;
}
}
在这种情况下,IPerson
不应该定义GotLegs
本身GotLegs
添加到IPerson
接口,并创建一个基类PersonBase
,该基类实现此方法,并使CityPeople
和村民
从该基类派生GetPeople
和Factory
静态:
public static class Factory
{
public static IPeople GetPeople(PeopleType type)
{
...
}
}
使用方法将如您所示:
IPeople people = Factory.GetPeople(PeopleType.URBAN);
如果您想添加公共功能,可以使用抽象类而不是接口。 在您的情况下,它将是:
public abstract class People
{
public bool GotLegs()
{
return true; //Always returns true for both People types
}
public abstract string GetName();
}
public class CityPeople : People
{
#region IPeople Members
public override string GetName()
{
return "City Guy";
}
#endregion
}
public class Villagers : People
{
#region IPeople Members
public override string GetName()
{
return "Village Guy";
}
#endregion
}
因此,如果我实现问题1中的选项2,那么我必须将GotLegs()声明为virtual,然后在子类-Villages&CityPeople中重写,对吗?谢谢你的回复,丹尼尔@snakepitbean:如果确实希望在子类中重写它,则只需将其声明为虚拟。如果你不想覆盖它——这也是我从你的问题中理解的——那么虚拟化是没有必要的。对不起,丹尼尔,我的意思是相反的。方法GetName()[不常见]将是“PeopleBase”类中的一个抽象方法,并将在相应的类Village&CityPeople中实现,对吗?