抽象工厂

抽象工厂是所有工厂模式中的大哥大了,我是这么认为的。它是从工厂方法扩展过来的。抽象工厂通过’依赖倒转‘原则,使程序更加稳定的运行。那就先让我们来看看它的类图吧。

,第一次看到这个类图,你会不会误以为是工厂方法的类图呢?我第一次就有这种感觉。其实抽象工厂相比于工厂方法就是多了产品系列。在工厂方法中产品就是一个系列的,而抽象工厂则实现了多个系列。现在就来看看它的简单实现代码吧--C#:

抽象工厂类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    public interface IFactory
    {
        IProduct1 factory1();

        IProduct2 factory2();
    }
}

具体工厂类A:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    class Factory_A : IFactory
    {
        #region IFactory 成员

        public IProduct1 factory1()
        {
            return new Product_A1();
        }

        public IProduct2 factory2()
        {
            return new Product_A2();
        }

        #endregion
    }
}

  

具体工厂类B:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    class Factory_B : IFactory
    {
        #region IFactory 成员

        public IProduct1 factory1()
        {
            return new Product_B1();
        }

        public IProduct2 factory2()
        {
            return new Product_B2();
        }

        #endregion
    }
}

  

抽象产品类1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    public interface IProduct1
    {
        void operation();                
    }
}

具体产品类A1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    class Product_A1 : IProduct1
    {
        #region IProduct1 成员

        public void operation()
        {
            Console.Write("Product_A1\n");
        }

        #endregion
    }
}

具体产品类B1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    class Product_B1 : IProduct1
    {
        #region IProduct1 成员

        public void operation()
        {
            Console.Write("Product_B1\n");
        }

        #endregion
    }
}

 

抽象产品类2: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    public interface IProduct2
    {
        void operation();
    }
}

  

具体产品类A2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    class Product_A2 :IProduct2
    {
        #region IProduct2 成员

        public void operation()
        {
            Console.Write("Product_A2\n");
        }

        #endregion
    }
}

  

具体产品类B2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Design_Mode.Abstract_Factory
{
    class Product_B2 : IProduct2
    {
        #region IProduct2 成员

        public void operation()
        {
            Console.Write("Product_B2\n");
        }

        #endregion
    }
}

客户端测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//using Design_Mode.Simple_Factory;
//using Design_Mode.Factory_Method;
using Design_Mode.Abstract_Factory;

namespace Design_Mode
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 简单工厂 例子

            //IProduct product = SimpleFactory.factory("A");
            
            //if(product != null)
            //    product.operation();

            #endregion

            #region 工厂方法例子

            //IFactory factory = new Factory_A();
            //IProduct product = factory.factory();
            //if (product != null)
            //    product.operation();

            #endregion

            #region 抽象工厂例子

            IFactory factory = new Factory_B();
            IProduct2 product = factory.factory2();
            if (product != null)
                product.operation();

            #endregion

        }
    }
}

  

优点:抽象工厂和工厂方法优点差不多,都是在新增具体产品类时,只要增加具体工厂类就可以了。达到了‘开放封闭’原则。因为都是依赖于接口的,所以程序比较稳定。

缺点:抽象工厂在增加产品系列时,要修改每一个工厂类,这时就违背了‘开放封闭’原则了。如果产品系列多了,工厂类的方法就多了,类就显得比较肥胖。

展望:抽象工厂可以结合简单工厂+反射技术。把‘开放封闭’达到极致。

下章就来讨论讨论抽象工厂结合简单工厂+反射技术的使用以及代码。

posted @ 2011-09-08 15:01  小虾Joe  阅读(743)  评论(0编辑  收藏  举报