抽象工厂是用于产品横向扩展。

例如:你的系统,要消费多个产品,假如你的产品,目前只支持SQLSERVER。客户需要用到MYSQL,类似这种变化的需求。就应该用抽象工厂来解决。

使用端:

        static void Main(string[] args)
        {

            IFactory sql = new SqlFactory();
            ITable Sqluser = sql.User();
            Sqluser.Insert();
            Sqluser.Update();
            Sqluser.Delete();
            Sqluser.Query<string>();

            Console.WriteLine("-----------------------------------------");
            IFactory mysql = new MysqlFactory();
            ITable Mysqluser = mysql.User();
            Mysqluser.Insert();
            Mysqluser.Update();
            Mysqluser.Delete();
            Mysqluser.Query<string>();

            Console.WriteLine("-----------------------------------------");
            IFactory db2sql = new Db2Factory();
            ITable db2user = db2sql.User();
            db2user.Insert();
            db2user.Update();
            db2user.Delete();
            db2user.Query<string>();

            Console.WriteLine("-----------------------------------------");
            IFactory Oracelsql = new OracleFactory();
            ITable Oraceluser = Oracelsql.User();
            Oraceluser.Insert();
            Oraceluser.Update();
            Oraceluser.Delete();
            Oraceluser.Query<string>();

            Console.ReadLine();
        }

工厂接口:

    public abstract class IFactory
    {
        public abstract ITable User();
        public abstract ITable Admin();
        public abstract ITable Order();
    }

产品接口:

    public interface ITable
    {
        int Delete();

        int Insert();

        int Update();

        List<T> Query<T>();

    }

工厂:

    public class MysqlFactory : IFactory
    {
        public override ITable Admin()
        {
            return new Mysql.Admin();
        }

        public override ITable Order()
        {
            return new Mysql.Order();
        }

        public override ITable User()
        {
            return new Mysql.User();
        }
    }

产品:

    public class Admin : ITable
    {
        public int Delete()
        {
            Console.WriteLine("{0},删除数据", this.GetType());
            return 1;
        }

        public int Insert()
        {
            Console.WriteLine("{0},增加数据", this.GetType());
            return 1;
        }

        public List<T> Query<T>()
        {
            Console.WriteLine("{0},修改查询", this.GetType());
            return default(List<T>);
        }

        public int Update()
        {
            Console.WriteLine("{0},修改数据", this.GetType());
            return 1;
        }
    }

 

posted on 2016-05-30 10:48  梦回过去  阅读(207)  评论(0编辑  收藏  举报