博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
抽象工厂模式
阅读量:5076 次
发布时间:2019-06-12

本文共 3340 字,大约阅读时间需要 11 分钟。

抽象工厂模式:提供了创建一系列相关或互相依赖对象的接口,而无须指定它们具体的类。

 

 

using System;using System.Collections.Generic;using System.Text;namespace 抽象工厂模式{    class Program    {        static void Main(string[] args)        {            User user = new User();            Department dept = new Department();            //AbstractFactory factory = new SqlServerFactory();            IFactory factory = new AccessFactory();            IUser iu = factory.CreateUser();            iu.Insert(user);            iu.GetUser(1);            IDepartment id = factory.CreateDepartment();            id.Insert(dept);            id.GetDepartment(1);            Console.Read();        }    }    class User    {        private int _id;        public int ID        {            get { return _id; }            set { _id = value; }        }        private string _name;        public string Name        {            get { return _name; }            set { _name = value; }        }    }    class Department    {        private int _id;        public int ID        {            get { return _id; }            set { _id = value; }        }        private string _deptName;        public string DeptName        {            get { return _deptName; }            set { _deptName = value; }        }    }    interface IUser    {        void Insert(User user);        User GetUser(int id);    }    class SqlserverUser : IUser    {        public void Insert(User user)        {            Console.WriteLine("在Sqlserver中给User表增加一条记录");        }        public User GetUser(int id)        {            Console.WriteLine("在Sqlserver中根据ID得到User表一条记录");            return null;        }    }    class AccessUser : IUser    {        public void Insert(User user)        {            Console.WriteLine("在Access中给User表增加一条记录");        }        public User GetUser(int id)        {            Console.WriteLine("在Access中根据ID得到User表一条记录");            return null;        }    }    interface IDepartment    {        void Insert(Department department);        Department GetDepartment(int id);    }    class SqlserverDepartment : IDepartment    {        public void Insert(Department department)        {            Console.WriteLine("在Sqlserver中给Department表增加一条记录");        }        public Department GetDepartment(int id)        {            Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录");            return null;        }    }    class AccessDepartment : IDepartment    {        public void Insert(Department department)        {            Console.WriteLine("在Access中给Department表增加一条记录");        }        public Department GetDepartment(int id)        {            Console.WriteLine("在Access中根据ID得到Department表一条记录");            return null;        }    }    interface IFactory    {        IUser CreateUser();        IDepartment CreateDepartment();    }    class SqlServerFactory : IFactory    {        public IUser CreateUser()        {            return new SqlserverUser();        }        public IDepartment CreateDepartment()        {            return new SqlserverDepartment();        }    }    class AccessFactory : IFactory    {        public IUser CreateUser()        {            return new AccessUser();        }        public IDepartment CreateDepartment()        {            return new AccessDepartment();        }    }}

 

转载于:https://www.cnblogs.com/hongdada/archive/2013/04/01/2992793.html

你可能感兴趣的文章
javascript闭包
查看>>
@Column标记持久化详细说明
查看>>
创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备...
查看>>
mysql8.0.13下载与安装图文教程
查看>>
站立会议08(冲刺2)
查看>>
url查询参数解析
查看>>
http://coolshell.cn/articles/10910.html
查看>>
[转]jsbsim基础概念
查看>>
mysql编码问题
查看>>
决策树与规则引擎
查看>>
nekohtml转换html时标签变大写的问题
查看>>
Python随笔1
查看>>
夺命雷公狗---Redis---6-案例操作1(注册-登录)
查看>>
IE8不显示图片alt了.
查看>>
C++中声明与定义的区别
查看>>
U3d PlayerPrefs
查看>>
《Linux命令行与shell脚本编程大全》 第八章管理文件系统
查看>>
前端css常用的选择小汇
查看>>
python之路-Memcache
查看>>
Hadoop Streaming框架使用(一)
查看>>