我遵循了工厂设计模式这个漂亮的文章:抽象工厂设计模式 - C#
http://dotnet.dzone.com/articles/design-patterns-c-factory
我理解这个例子非常好。能否请您作出
抽象工厂设计模式此相同的例子吗?
我想了解的基本概念抽象工厂设计模式。
感谢
我遵循了工厂设计模式这个漂亮的文章:抽象工厂设计模式 - C#
http://dotnet.dzone.com/articles/design-patterns-c-factory
我理解这个例子非常好。能否请您作出
抽象工厂设计模式此相同的例子吗?
我想了解的基本概念抽象工厂设计模式。
感谢
Abstract factory - 不一样的例子,但很容易理解。
我需要使用我提供的相同示例的抽象工厂设计模式,以便我能更好地理解它。您可以修改代码/扩展代码。 – user1054625
我知道界面保持不变。而这些类也是。我们可以让Programmer和SeniorProgrammer属于DEVELOPER和NetworkDBA类,而SQLServerDBA属于DBA类。现在我认为我们可以在此基础上创建工厂类。 – user1054625
通用构造是创建数据访问组件的抽象工厂,并且您为数据库供应商或其他存储库系统实现了每个特定工厂。从 “应用的Java模式”
using System;
namespace DZoneArticles.FactoryDesignPattern
{
public abstract class EmployeeFactory
{
public abstract Employee Create();
}
public class DBAFactory : EmployeeFactory
{
public override Employee Create() { return new DBA(); }
}
public class ManagerFactory : EmployeeFactory
{
public override Employee Create() { return new Manager(); }
}
}
适应:
public interface IEmployee
{
}
public class Developer : IEmployee
{
}
public class Tester : IEmployee
{
}
public interface IManager
{
}
public class HRManager : IManager
{
}
public class ProjectManager : IManager
{
}
public interface IFactory()
{
IEmployee CreateEmployee();
IManager CreateManager();
}
public class ConcreteFactory1 : IFactory
{
public IEmployee CreateEmployee()
{
return new Developer();
}
public IManager CreateManager()
{
return new HRManager();
}
}
public class ConcreteFactory2 : IFactory
{
public IEmployee CreateEmployee()
{
return new Tester();
}
public IManager CreateManager()
{
return new ProjectManager();
}
}
抽象工厂设计模式在C#中的简单的例子:
using System;
using System.IO;
enum TYPE_OPERATION
{
Design,
Work
}
//Abstract Factory
abstract class AbstractFactory
{
public static AbstractFactory PrepareOperation(TYPE_OPERATION type_operation)
{
switch (type_operation)
{
case TYPE_OPERATION.Design:
return new DesignTeam();
case TYPE_OPERATION.Work:
return new WorkTeam();
default:
throw new NotImplementedException();
}
}
public abstract void PerformOperation();
}
class DesignTeam : AbstractFactory
{
public override void PerformOperation()
{
Designers m = new Designers();
m.Designing();
}
}
class WorkTeam : AbstractFactory
{
public override void PerformOperation()
{
Workers k = new Workers();
k.Working();
}
}
// The concrete class
class Designers
{
public void Designing()
{
Console.WriteLine("The Design Team has completed its work!");
}
}
// The concrete class
class Workers
{
public void Working()
{
Console.WriteLine("The Work Team has finished its work!");
}
}
public class Client
{
public static void Main(String[] args)
{
AbstractFactory factory = AbstractFactory.PrepareOperation(TYPE_OPERATION.Design);
factory.PerformOperation();
factory = AbstractFactory.PrepareOperation(TYPE_OPERATION.Work);
factory.PerformOperation();
Console.ReadLine();
}
}
/* output:
The Design Team has completed its work!
The Work Team has finished its work!
*/
-1这应该是研究特别给出了答案,从而远远只是一个链接。 –