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. Android实时获取音量(单位:分贝)

    基础知识 度量声音强度,大家最熟悉的单位就是分贝(decibel,缩写为dB).这是一个无纲量的相对单位,计算公式如下: 分子是测量值的声压,分母是参考值的声压(20微帕,人类所能听到的最小声压).因 ...

  2. EasyUI - 后台管理系统 - 增加,删除,修改

    效果: html代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ad ...

  3. fzu 1911 Construct a Matrix(矩阵快速幂+规律)

    题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...

  4. jsonp与cors跨域的一些理解

    浏览器的同源策略,即是浏览器之间要隔离不同域的内容,禁止互相操作. 比如,当你打开了多个网站,如果允许多个网站之间互相操作,那么其中一个木马网站就可以通过这种互相操作进行一系列的非法行为,获取你在各个 ...

  5. MFC获取rgb图像数据后动态显示及保存图片的方法

    该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw32.lib  链接库 //---------------- ...

  6. 数据层交换和高性能并发处理(开源ETL大数据治理工具--KETTLE使用及二次开发 )

    ETL是什么?为什么要使用ETL?KETTLE是什么?为什么要学KETTLE?        ETL是数据的抽取清洗转换加载的过程,是数据进入数据仓库进行大数据分析的载入过程,目前流行的数据进入仓库的 ...

  7. 基于visual Studio2013解决C语言竞赛题之1054抽牌游戏

       题目 解决代码及点评 /************************************************************************/ /* 54 ...

  8. 异常与诊断(74篇,内含许多WinDBG的文章)

    http://www.cnblogs.com/lidabo/category/542683.html

  9. poj 3211 Washing Clothes(背包)

    很不错的01背包!!! 不过有点疑问!!!(注释) #include <algorithm> #include<stdio.h> #include<string.h> ...

  10. LintCode 推断一个二叉树树是否是还有一个二叉树的子书

    有两个不同大小的二进制树: T1 有上百万的节点: T2 有好几百的节点. 请设计一种算法.判定 T2 是否为 T1的子树. /** * Definition of TreeNode: * class ...