抽象工厂设计模式 - C#

时间:2011-11-21 15:37:19

标签: c# design-patterns

我已经关注 工厂设计模式 这篇好文章:

http://dotnet.dzone.com/articles/design-patterns-c-factory

我非常了解这个例子。你可以为

做同样的例子吗?

抽象工厂设计模式

我想了解 抽象工厂设计模式 的基本概念。

谢谢

4 个答案:

答案 0 :(得分:1)

Abstract factory - 不是同一个例子,但很容易理解。

答案 1 :(得分:1)

常见构造是一个创建数据访问组件的抽象工厂,您可以实现特定于数据库供应商或其他存储库系统的每个因素。

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(); }
    }
}

答案 2 :(得分:1)

“应用Java模式”的改编:

 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();
      }
 }

答案 3 :(得分:1)

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!
*/