标签:工厂模式 加工 pps 动态切换 -- 增加 额外 记录 return
namespace 抽象工厂02
{
/// <summary>
/// 抽象产品
/// </summary>
public interface IUser
{
User GetUser(int id);
void InsertUser(User entity);
}
}
namespace 抽象工厂02
{
/// <summary>
/// 用户表
/// </summary>
public class User
{
public int ID { get; set; }
public string Name { get; set; }
}
}
using System;
namespace 抽象工厂02
{
/// <summary>
/// 具体产品
/// </summary>
public class SqlServerUser : IUser
{
public User GetUser(int id)
{
Console.WriteLine("在SQL Server中根据ID得到User表中一条记录");
return null;
}
public void InsertUser(User entity)
{
Console.WriteLine("在SQL Server中给User表中增加一条记录");
}
}
}
namespace 抽象工厂02
{
/// <summary>
/// 抽象工厂
/// </summary>
public interface IFactory
{
IUser CreateUser();
}
}
namespace 抽象工厂02
{
/// <summary>
/// SqlServer方式具体工厂
/// </summary>
public class SqlServerFactory : IFactory
{
public IUser CreateUser()
{
return new SqlServerUser();
}
}
}
using System;
namespace 抽象工厂02
{
/// <summary>
/// Access方式访问数据--具体产品
/// </summary>
public class AccessUser : IUser
{
public User GetUser(int id)
{
Console.WriteLine("在Access中根据ID得到User表中一条记录");
return null;
}
public void InsertUser(User entity)
{
Console.WriteLine("在Access中给User表中增加一条记录");
}
}
}
namespace 抽象工厂02
{
/// <summary>
/// Access方式的工厂
/// </summary>
public class AccessFactory : IFactory
{
public IUser CreateUser()
{
return new AccessUser();
}
}
}
using System;
namespace 抽象工厂02
{
/// <summary>
/// 运用工厂方法模式实现(不同数据库的快速切换)
/// </summary>
class Program
{
static void Main(string[] args)
{
//1.创建具体工厂
IFactory factory = new SqlServerFactory(); //如果用Access数据库只需要将SqlServerFactory改为AccessFactory就可以了
//2.根据工厂生产具体
IUser user= factory.CreateUser();
user.InsertUser(new User() { Name="成吉思汗" });
user.GetUser(3);
Console.ReadKey();
}
}
}
namespace 抽象工厂03
{
/// <summary>
/// 部门表
/// </summary>
public class Department
{
public int ID { get; set; }
/// <summary>
/// 部门名称
/// </summary>
public string Name { get; set; }
}
}
namespace 抽象工厂03
{
/// <summary>
/// 部门抽象接口
/// </summary>
public interface IDepartment
{
Department GetDepartment(int id);
void InsertDepartment(Department entity);
}
}
using System;
namespace 抽象工厂03
{
public class AccessDepartment : IDepartment
{
public Department GetDepartment(int id)
{
Console.WriteLine("在Access中根据ID得到Department表中一条记录");
return null;
}
public void InsertDepartment(Department entity)
{
Console.WriteLine("在Access中给Department表中增加一条记录");
}
}
}
namespace 抽象工厂03
{
/// <summary>
/// 抽象工厂 (应该包含所有产品创建的抽象方法)
/// </summary>
public interface IFactory
{
IUser CreateUser();
IDepartment CreateDepartment();
}
}
namespace 抽象工厂03
{
/// <summary>
/// Access方式的工厂(具体工厂创建具有特定实现的产品对象)
/// </summary>
public class AccessFactory : IFactory
{
public IDepartment CreateDepartment()
{
return new AccessDepartment();
}
public IUser CreateUser()
{
return new AccessUser();
}
}
}
using System;
namespace 抽象工厂03
{
/// <summary>
/// SqlServer部门产品
/// </summary>
public class SqlServerDepartment : IDepartment
{
public Department GetDepartment(int id)
{
Console.WriteLine("在SqlServer中根据ID得到Department表中一条记录");
return null;
}
public void InsertDepartment(Department entity)
{
Console.WriteLine("在SqlServer中给Department表中增加一条记录");
}
}
}
namespace 抽象工厂03
{
/// <summary>
/// SqlServer方式具体工厂
/// </summary>
public class SqlServerFactory : IFactory
{
public IDepartment CreateDepartment()
{
return new SqlServerDepartment();
}
public IUser CreateUser()
{
return new SqlServerUser();
}
}
}
using System;
namespace 抽象工厂03
{
/// <summary>
/// 运用抽象工厂模式实现(不同数据库的快速切换)
/// </summary>
class Program
{
static void Main(string[] args)
{
//1.创建具体工厂
IFactory factory = new AccessFactory();
//2.通过工厂生成具体产品
IDepartment department = factory.CreateDepartment();
//3.根据id获取部门
department.GetDepartment(3);
Console.ReadKey();
}
}
}
namespace 抽象工厂04
{
/// <summary>
/// 增加工厂类改造抽象工厂
/// </summary>
public class DataAccess
{
private static readonly string db = "SqlServer";
//private static readonly string db = "Access";
public static IUser CreateUser()
{
IUser result = null;
switch (db)
{
case "SqlServer":
result = new SqlServerUser();
break;
case "Access":
result = new AccessUser();
break;
default:
break;
}
return result;
}
public static IDepartment CreateDepartment()
{
IDepartment result = null;
switch (db)
{
case "SqlServer":
result = new SqlServerDepartment();
break;
case "Access":
result = new AccessDepartment();
break;
default:
break;
}
return result;
}
}
}
namespace 抽象工厂04
{
/// <summary>
/// 用简单工厂改造抽象工厂
/// </summary>
class Program
{
static void Main(string[] args)
{
IUser user = DataAccess.CreateUser(); //直接得到数据库实例,不存在任何依赖
user.GetUser(3);
IDepartment deparment = DataAccess.CreateDepartment();
deparment.InsertDepartment(new Department() { Name="研发部" });
}
}
}
using System.Configuration;
using System.Reflection;
namespace 抽象工厂05
{
/// <summary>
/// 通过反射创建对象
/// </summary>
public class DataAccess
{
private static readonly string AssemblyName = "抽象工厂05";
private static readonly string db = ConfigurationManager.AppSettings["db"];
/// <summary>
/// 创建用户抽象产品的工厂方法
/// </summary>
/// <returns></returns>
public static IUser CreateUser()
{
string className= AssemblyName + "." + db + "User";
return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);
}
/// <summary>
/// 创建部门抽象产品的工厂方法
/// </summary>
/// <returns></returns>
public static IDepartment CreateDepartment()
{
string className = AssemblyName + "." + db + "Department";
return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className);
}
}
}
using System;
namespace 抽象工厂05
{
/// <summary>
/// 反射+抽象工厂方式实现数据访问
/// </summary>
class Program
{
static void Main(string[] args)
{
IUser iUser = DataAccess.CreateUser();
iUser.InsertUser(new User() { Name="赵一曼" });
IDepartment iDepartment = DataAccess.CreateDepartment();
iDepartment.GetDepartment(3);
Console.ReadKey();
}
}
}
标签:工厂模式 加工 pps 动态切换 -- 增加 额外 记录 return
原文地址:https://www.cnblogs.com/wgx0428/p/12862757.html