根据业务自己设计的.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[] {,, };
int[] B_appid = new int[] { , , };
int[] C_appid = new int[] { , , };
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 :
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工厂模式架构
NET工厂模式架构 最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构 项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有ap ...
- php 23种设计模型 - 抽象工厂模式
抽象工厂模式(AbstractFactory) 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创 ...
- PHP设计——单例模式与工厂模式
一.单例模式又称为职责模式,它用来在程序中创建一个单一功能的访问点,通俗地说就是实例化出来的对象是唯一的.所有的单例模式至少拥有以下三种公共元素:1. 它们必须拥有一个构造函数,并且必须被标记为pri ...
- GOF业务场景的设计模式-----工厂模式
定义:定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 工厂方法模式 基本代码 interface IProduct { public void produ ...
- 从接口、抽象类到工厂模式再到JVM来总结一些问题
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 涉及到的知识点总结如下: 为什么使用接口? 接口和抽象类的区别 简单工厂模式总结 Java中new和newInstance的区别 J ...
- C#设计模式(3):抽象工厂模式(Abstract Factory)(转载)
概述 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时由于需求的变化,往往存在着更多系列对象的创建工作.如何应对这种变化?如何绕过常规的对象的创建方法(new),提供一种“封装机制”来 ...
- .NET设计模式(3):抽象工厂模式(Abstract Factory)(转)
概述 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时由于需求的变化,往往存在着更多系列对象的创建工作.如何应对这种变化?如何绕过常规的对象的创建方法(new),提供一种“封装机制”来 ...
- .NET设计模式(3):抽象工厂模式(Abstract Factory)
):抽象工厂模式(Abstract Factory) 抽象工厂模式(Abstract Factory) --探索设计模式系列之三 Terrylee,2005年12月12日 转载:http://terr ...
- Java反射+简单工厂模式总结
除了 new 之外的创建对象的方法 通过 new 创建对象,会使得程序面向实现编程,先举个例子,某个果园里现在有两种水果,一种是苹果,一种是香蕉,有客户想采摘园子里的水果,要求用get()方法表示即可 ...
随机推荐
- 为 Elasticsearch 添加中文分词,对比分词器效果
转自:http://keenwon.com/1404.html 为 Elasticsearch 添加中文分词,对比分词器效果 Posted in 后端 By KeenWon On 2014年12月12 ...
- 操作符重载、继承(day08)
二十 操作符重载 函数操作符"()" 功能:让对象当做函数来使用 注:对参数的个数.返回类型没有限制 eg: class A{...}; A a; //a.operator()(1 ...
- POJ3253 Fence Repair【贪心】
我们的小伙伴Bingo真的很调皮,他在上课的路上看到树上有个鸟窝,他就想去把他捅下来,但是鸟窝很高他够不到,于是他就到处找木棍,想把这些木棍接在一起,然后去捅鸟窝.他一共找了N跟木棍 (1 ≤ N ≤ ...
- cent OS官网上下载老版本系统镜像的正确打开方式
当时的情况是这样的: 客户需要给服务器安装cent OS 7.3操作系统,我打开官网https://www.centos.org/,点击“GET CENTOS”——>“Minimal ISO”, ...
- 继续聊WPF——为ListView的行设置样式
<Window x:Class="Wpf_GridHeaderStyle_sample.Window1" xmlns="http://schemas.microso ...
- d正则表达式相关
控件看Delphi正则表达式使用方法(TPerlRegEx)的介绍: 使用正则表达式获取Sql查询语句各项(表名.字段.条件.排序) 希望在线测试正则表达式 在线正则表达式测试 有许多常规表达式 希 ...
- js动态创建表格------Day59
刚刚不知道怎么回事,CSDN博客一直打不开,就在博客园完毕了今天的记录,结果临关机,登录了下.发现又好了,就再多花个几分钟转下吧,也无论到底在意的是什么了,权当强迫症了... 前几天记录了动态的加入一 ...
- HDU 5218 The E-pang Palace (简单几何—2014广州现场赛)
题目链接:pid=5128">http://acm.hdu.edu.cn/showproblem.php? pid=5128 题面: The E-pang Palace Time Li ...
- SQL SERVER读书笔记:nolock
让查询语句不去申请共享锁,从而消除死锁,效果立竿见影. 缺点: 1.脏读 2.只能解决共享锁(S锁)参与的问题 3.需要修改语句本身才能使用
- To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31 ...