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 MVC

NET工厂模式架构的更多相关文章

  1. 根据业务自己设计的.NET工厂模式架构

    最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构 项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有appid来区分) 因为 ...

  2. C# 框架是什么?MVC是什么 ?工厂模式是什么?设计模式是什么?三层架构是什

    C# 框架是什么?MVC是什么 ?工厂模式是什么?设计模式是什么?三层架构是什么?如果要学我该从何学起??? C# 框架看这里http://download.csdn.net/source/25784 ...

  3. 使用传统的三层架构出现的问题.引入Spring底层实现原理来解决(工厂模式+反射+XML配置文件/注解)

    以前写的代码 mapper层 public interface PersonMapper { void selectPersonList(); } public class PersonMapperI ...

  4. 从接口、抽象类到工厂模式再到JVM来总结一些问题

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 涉及到的知识点总结如下: 为什么使用接口? 接口和抽象类的区别 简单工厂模式总结 Java中new和newInstance的区别 J ...

  5. .NET设计模式(2):1.2 抽象工厂模式(Abstract Factory)

    概述 抽象工厂模式(Abstract Factory)是所有形态的工厂模式中最为抽象和最具一般性的一种形态.抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式.抽象工厂模式可以向客户端提供一个接口 ...

  6. 网络电视精灵~分析~~~~~~简单工厂模式,继承和多态,解析XML文档,视频项目

    小总结: 所用技术: 01.C/S架构,数据存储在XML文件中 02.简单工厂模式 03.继承和多态 04.解析XML文档技术 05.深入剖析内存中数据的走向 06.TreeView控件的使用 核心: ...

  7. 【设计模式 - 1】之工厂模式(Factory)

    1      模式简介 1.1    工厂模式作用 工厂模式解决的是"使用new关键字获取对象造成松耦合"的问题. 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏 ...

  8. Java设计模式---工厂模式(简单工厂、工厂方法、抽象工厂)

    工厂模式:主要用来实例化有共同接口的类,工厂模式可以动态决定应该实例化那一个类.工厂模式的形态工厂模式主要用一下几种形态:1:简单工厂(Simple Factory).2:工厂方法(Factory M ...

  9. 工厂模式IDAL具体解释

    IDAL 一. IDAL主要功能: 1.这全然是"工厂模式"的一部分实现而已 2.这是一组接口类,当中包含了每一个要公开的数据訪问方法.为每一个数据库产品单独编写的DAL(数据訪问 ...

随机推荐

  1. IMAP和POP3有什么差别?

    servCode=6010376">POP3协议同意电子邮件client下载server上的邮件,可是在client的操作(如移动邮件.标记已读等),不会反馈到server上.比方通过 ...

  2. SharePoint 2013 &quot;通知我&quot;简单的功能

    简单的功能 "通知我"内部列表或文档库中的主要项目.加入/删除/修改等操作,用户的E- mail通知设定功能:设置列表或文档库通知的能力,有可能设置通知为一个单一的项目.这是Sha ...

  3. 基于visual Studio2013解决C语言竞赛题之1052求根

       题目 解决代码及点评 /* 功能:用简单迭代法解方程 e^x - x - 2 = 0 它有两个根(如图),其迭代公式为: 1) x[n+1]= e^x*n-2 (初值X<0时) ...

  4. 单选按钮易忽略的Group属性

    Group就其意思就是一组的意思.就是说用于选择多个控件组合,选了TRUE后,你就可以为这组新建一个变量.把一组控件当一个控件来使用.例如多个单选按钮用group属性,这样你就可以用一个变量来管理这些 ...

  5. javascript 中对this关键字的一些理解

    var self = this 可能会觉得奇怪为什么我会定义一个_self的变量, 因为在js里,this不用对于其他的对象语言,他的解析过程与运行过程中this会改变的.这里简单说说js里this的 ...

  6. 性能测试之LoadRunner11 破解

    1. 下载破解文件lm70.dll和mlr5lprg.dll     lm70.dll文件,覆盖x:\Program Files\Mercury\LoadRunner\bin下文件即可.     ml ...

  7. SVM(支持向量机)(一)

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) SVM(Support Vector Machines)系列会循序 ...

  8. linux 解压操作命令

    http://apps.hi.baidu.com/share/detail/37384818 download ADT link http://dl.google.com/android/ADT-0. ...

  9. Jetty支持Windows认证

    WAFFLE是什么? Jetty增加WAFFLE支持 DEMO 小结 WAFFLE是什么? WAFFLE是一个Windows认证框架,支持Negotiate, NTLM和Kerberos认证.WAFF ...

  10. App 运营 推广相关

    基本要素 1.定位和产品 2.取个好名字,一目了然+下载冲动 3.设计一个好图标,有感性和直观的认识 4.做好产品的说明.关键字,截图(前1-2行是重点) 5.做市场的排名(相关因素如下)   (1) ...