抽象工厂
Posted ly-03-04
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽象工厂相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Dapper; using System.Data.SqlClient; using System.Collections; namespace WebApplication4.Controllers { public class UserInfoController : Controller { // GET: UserInfo public ActionResult Index() { return View(); } [HttpPost] public void Index(UserModel m) { Factory factory = Factory.GetFactory(); IUser user = factory.GetUsers(); int n = user.GetUsers(m); if (n>0) { } } /// <summary> /// Model /// </summary> public class UserModel { public int Id { get; set; } public string UName { get; set; } public string UPwd { get; set; } } /// <summary> /// 接口 /// </summary> public interface IUser { int GetUsers(UserModel m); } /// <summary> /// 实现方法 /// </summary> public class SQLUser : IUser { public int GetUsers(UserModel m) { using (SqlConnection conn = new SqlConnection("")) { return Convert.ToInt32(conn.ExecuteScalar($"select count(1) from User where UName=‘@UName‘ and UPwd=‘@UPwd‘",new { UName = m.UName, UPwd = m.UPwd })); } } } /// <summary> /// 获取 实体业务工厂 对象 /// </summary> /// <returns></returns> public abstract class Factory { public static Factory GetFactory() { Factory fac= new Factory_A(); return fac; } public abstract IUser GetUsers(); } /// <summary> /// 实现SQL工厂 /// </summary> public class Factory_A : Factory { public override IUser GetUsers() { return new SQLUser(); } } } }
以上是关于抽象工厂的主要内容,如果未能解决你的问题,请参考以下文章