NET工厂模式架构
NET工厂模式架构
最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构
项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有appid来区分)
因为每个平台的业务处理几乎不一样,而属于同一个平台的app客户端业务又几乎一样,但也有要特殊情况处理的。
直接代码展示:
首先是接口和抽象类

/// <summary>
/// 商品接口类
/// </summary>
public interface IProductBLL
{
string GetBLLName(int appid); string GetAction();
} /// <summary>
/// 抽象类
/// </summary>
public abstract class BaseProductBLL : IProductBLL
{ public static IProductBLL GetProductBll(int appid)
{
//定义appid属于哪个平台,这里可以用config配置或者枚举定义
int[] A_appid = new int[] {10001,10003,10005 };
int[] B_appid = new int[] { 10002, 10004, 10006 };
int[] C_appid = new int[] { 10007, 10008, 100009 };
IProductBLL bll = null; if (A_appid.Contains(appid))//此appid属于A平台
{
bll = BaseProduct_A_BLL.GetProductBll(appid);
}
else if (B_appid.Contains(appid))//此appid属于B平台
{
bll = BaseProduct_B_BLL.GetProductBll(appid);
}
else if (C_appid.Contains(appid))//此appid属于C平台
{
bll = BaseProduct_C_BLL.GetProductBll(appid);
} return bll;
} public abstract string GetBLLName(int appid);
public abstract string GetAction();
}

接下来是各平台的通用类

/// <summary>
/// A平台通用类
/// </summary>
public class BaseProduct_A_BLL : BaseProductBLL
{
private static IProductBLL _instanceA = null;
public static IProductBLL InstanceA
{
get
{
if (_instanceA == null)
{ _instanceA = new BaseProduct_A_BLL();
}
return _instanceA;
}
}
public static IProductBLL GetProductBll(int appid)
{
IProductBLL bll = null; switch (appid)
{
case 10003:
bll=Product_10003_BLL.Instance10003;
break;
default:
bll = BaseProduct_A_BLL.InstanceA;
break;
} return bll;
} public override string GetBLLName(int appid)
{
return "" + appid + "调用了BaseProduct_A_BLL.GetBLLName";
} public override string GetAction()
{
return "调用了BaseProduct_A_BLL.GetAction";
} } /// <summary>
/// B平台通用类
/// </summary>
public class BaseProduct_B_BLL : BaseProductBLL
{
private static IProductBLL _instanceB = null;
public static IProductBLL InstanceB
{
get
{
if (_instanceB == null)
{ _instanceB = new BaseProduct_B_BLL();
}
return _instanceB;
}
}
public static IProductBLL GetProductBll(int appid)
{
IProductBLL bll = null; switch (appid)
{
default:
bll = BaseProduct_B_BLL.InstanceB;
break;
} return bll;
} public override string GetBLLName(int appid)
{
return "" + appid + "调用了BaseProduct_B_BLL.GetBLLName";
} public override string GetAction()
{
return "调用了BaseProduct_B_BLL.GetAction";
} } /// <summary>
/// C平台通用类
/// </summary>
public class BaseProduct_C_BLL : BaseProductBLL
{
private static IProductBLL _instanceC = null;
public static IProductBLL InstanceC
{
get
{
if (_instanceC == null)
{ _instanceC = new BaseProduct_C_BLL();
}
return _instanceC;
}
}
public static IProductBLL GetProductBll(int appid)
{
IProductBLL bll = null; switch (appid)
{
default:
bll = BaseProduct_C_BLL.InstanceC;
break;
} return bll;
} public override string GetBLLName(int appid)
{
return "" + appid + "调用了BaseProduct_C_BLL.GetBLLName";
} public override string GetAction()
{
return "调用了BaseProduct_C_BLL.GetAction";
}
}

假如appid为10003的需要拓展,需在A平台通用类中添加case 10003 的逻辑,并创建对应的拓展类Product_10003_BLL并继承A平台通用类BaseProduct_A_BLL

/// <summary>
/// appid为10003拓展类
/// </summary>
public class Product_10003_BLL : BaseProduct_A_BLL
{
private static IProductBLL _instance10003 = null;
public static IProductBLL Instance10003
{
get
{
if (_instance10003 == null)
{ _instance10003 = new Product_10003_BLL();
}
return _instance10003;
}
} public Product_10003_BLL()
{
} public override string GetBLLName(int appid)
{
return "" + appid + "调用了Product_10003_BLL.GetBLLName";
}
}

如上appid为10003的拓展类重写了GetBLLName方法。
接下来就是在Controlle层中调用

/// <summary>
/// 基类控制类
/// </summary>
public class BasesController : Controller
{
public BasesController()
{
} protected int Appid
{
get { return Convert.ToInt32(HttpContext.Request["appid"]); }
} public IProductBLL ProductBll
{
get
{
return BaseProductBLL.GetProductBll(Appid);
}
}
} public class ProductController : BasesController
{
//
// GET: /Product/
public ActionResult Index()
{
string bllname = ProductBll.GetBLLName(Appid);
string actionName = ProductBll.GetAction();
ViewData["bllname"] = bllname;
ViewData["actionName"] = actionName;
return View();
} }

以上参数appid是必传,以上代码没有对不传参数且参数值得正确性做判断的异常处理,这里主要是记录对于这个系统的交互设计,另外数据访问层DAL也没有写出来,可以按照上面的设计思路进行创建
下面是我简单测试打印出来的
ViewData["bllname"]
ViewData["actionName"] 的值
访问http://localhost:37842/product/index?appid=10005
也就是参数appid为10005 时打印出 :
10005调用了BaseProduct_A_BLL.GetBLLName
调用了BaseProduct_A_BLL.GetAction
当appid为10003时打印出:
10003调用了Product_10003_BLL.GetBLLName
调用了BaseProduct_A_BLL.GetAction
当appid为10002时打印出:
10002调用了BaseProduct_B_BLL.GetBLLName
调用了BaseProduct_B_BLL.GetAction
此文仅记录笔记,各位大神可以多多指教!
NET工厂模式架构的更多相关文章
- 根据业务自己设计的.NET工厂模式架构
最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构 项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有appid来区分) 因为 ...
- C# 框架是什么?MVC是什么 ?工厂模式是什么?设计模式是什么?三层架构是什
C# 框架是什么?MVC是什么 ?工厂模式是什么?设计模式是什么?三层架构是什么?如果要学我该从何学起??? C# 框架看这里http://download.csdn.net/source/25784 ...
- 使用传统的三层架构出现的问题.引入Spring底层实现原理来解决(工厂模式+反射+XML配置文件/注解)
以前写的代码 mapper层 public interface PersonMapper { void selectPersonList(); } public class PersonMapperI ...
- 从接口、抽象类到工厂模式再到JVM来总结一些问题
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 涉及到的知识点总结如下: 为什么使用接口? 接口和抽象类的区别 简单工厂模式总结 Java中new和newInstance的区别 J ...
- .NET设计模式(2):1.2 抽象工厂模式(Abstract Factory)
概述 抽象工厂模式(Abstract Factory)是所有形态的工厂模式中最为抽象和最具一般性的一种形态.抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式.抽象工厂模式可以向客户端提供一个接口 ...
- 网络电视精灵~分析~~~~~~简单工厂模式,继承和多态,解析XML文档,视频项目
小总结: 所用技术: 01.C/S架构,数据存储在XML文件中 02.简单工厂模式 03.继承和多态 04.解析XML文档技术 05.深入剖析内存中数据的走向 06.TreeView控件的使用 核心: ...
- 【设计模式 - 1】之工厂模式(Factory)
1 模式简介 1.1 工厂模式作用 工厂模式解决的是"使用new关键字获取对象造成松耦合"的问题. 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏 ...
- Java设计模式---工厂模式(简单工厂、工厂方法、抽象工厂)
工厂模式:主要用来实例化有共同接口的类,工厂模式可以动态决定应该实例化那一个类.工厂模式的形态工厂模式主要用一下几种形态:1:简单工厂(Simple Factory).2:工厂方法(Factory M ...
- 工厂模式IDAL具体解释
IDAL 一. IDAL主要功能: 1.这全然是"工厂模式"的一部分实现而已 2.这是一组接口类,当中包含了每一个要公开的数据訪问方法.为每一个数据库产品单独编写的DAL(数据訪问 ...
随机推荐
- Java与C#的语法区别(不断更新中...)
1.static关键字: 在java中静态成员能够被对象和类名调用: 在C#中,静态成员只能被类调用不能被对象调用. 2.for循环: 在java中可以在for前面添加标记,然后在for循环中可以br ...
- delphi与汇编
我一直认为Delphi功能与C++相比毫不逊色,提供了丰富的控件和类.全部API以及嵌入的汇编.最近小弟在把C版的Huffman压缩改用Delphi写时,顺便“研究”了一下Delphi的位操作和嵌入式 ...
- Windows Service的安装卸载 和 Service控制
原文 Windows Service的安装卸载 和 Service控制 本文内容包括如何通过C#代码安装Windows Service(exe文件,并非打包后的安装文件).判断Service是否存在. ...
- Basic4android:多功能的Android应用软件快速开发平台
Basic4android 是目前最简单.最强大的Android平台快速应用开发工具. ( "Basic4android is the simplest and most powerful ...
- JS - 循环添加 DropDownList(Select)
代码: <td style="padding-left: 10px;"> <select id="ddl_picture_3"> < ...
- Python 学习入门(22)—— 线程同步
Python主要通过标准库中的threading包来实现多线程.在当今网络时代,每个服务器都会接收到大量的请求.服务器可以利用多线程的方式来处理这些请求,以提高对网络端口的读写效率.Python是一种 ...
- 基于visual Studio2013解决面试题之0403串联字符串
题目
- An example usage of an encryption algorithm (AES, in this case) is:
pycrypto 2.6.1 : Python Package Index An example usage of an encryption algorithm (AES, in this case ...
- Oracle死锁、数据库链接问题
--查询数据库锁表的信息 select do.object_name,t2.username,t2.sid,t2.serial#,t2.logon_time,t2.MACHINE,t2.state,t ...
- 自己写一个jqery的拖拽插件
说实话,jQuery比原生的js好用多了,本来想用原生写的,也写出来的,仅仅是,感觉不像插件,所以用jQuery实现了一版. 实现的功能:能够指定拖拽的边界,在拖拽过程中,能够触发几个自己定义事件 先 ...