最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构

项目介绍:整个系统分为三大平台(这里用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工厂模式架构的更多相关文章

  1. NET工厂模式架构

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

  2. php 23种设计模型 - 抽象工厂模式

    抽象工厂模式(AbstractFactory) 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创 ...

  3. PHP设计——单例模式与工厂模式

    一.单例模式又称为职责模式,它用来在程序中创建一个单一功能的访问点,通俗地说就是实例化出来的对象是唯一的.所有的单例模式至少拥有以下三种公共元素:1. 它们必须拥有一个构造函数,并且必须被标记为pri ...

  4. GOF业务场景的设计模式-----工厂模式

    定义:定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 工厂方法模式 基本代码 interface IProduct { public void produ ...

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

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

  6. C#设计模式(3):抽象工厂模式(Abstract Factory)(转载)

    概述 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时由于需求的变化,往往存在着更多系列对象的创建工作.如何应对这种变化?如何绕过常规的对象的创建方法(new),提供一种“封装机制”来 ...

  7. .NET设计模式(3):抽象工厂模式(Abstract Factory)(转)

    概述 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时由于需求的变化,往往存在着更多系列对象的创建工作.如何应对这种变化?如何绕过常规的对象的创建方法(new),提供一种“封装机制”来 ...

  8. .NET设计模式(3):抽象工厂模式(Abstract Factory)

    ):抽象工厂模式(Abstract Factory) 抽象工厂模式(Abstract Factory) --探索设计模式系列之三 Terrylee,2005年12月12日 转载:http://terr ...

  9. Java反射+简单工厂模式总结

    除了 new 之外的创建对象的方法 通过 new 创建对象,会使得程序面向实现编程,先举个例子,某个果园里现在有两种水果,一种是苹果,一种是香蕉,有客户想采摘园子里的水果,要求用get()方法表示即可 ...

随机推荐

  1. 亚马逊免费服务器搭建Discuz!论坛过程(二)

    1:  登录服务器 在实例页面,点击连接连接,按照如下步骤,即可登录服务器. 登录成功之后,如下所示.你就可以随意玩耍了. 2: 系统优化 证书登录当然安全,但是不太方便并且麻烦,本人还是习惯用户名密 ...

  2. 图片base64格式转为file文件类型上传方法

    日常使用文件上传方式,都是通过input type='file'的文件选择框进行文件上传.但是会通过其他交互方式等到图片的base64格式进行上传.具体情况如下示意: 在项目开发中,需要进行照片采集, ...

  3. NumPy常见的元素操作函数

    ceil(): 向上最接近的整数,参数是 number 或 array floor(): 向下最接近的整数,参数是 number 或 array rint(): 四舍五入,参数是 number 或 a ...

  4. 2019-04-02 cast and covert

    convert 专用于SQLServer,cast对于其它数据库的兼容性更好 convert 处理日期和时间值更厉害 语法不一样: cast(itemvalue as decimal(19,6)) c ...

  5. java的几种对象(PO,VO,DAO,BO,POJO)解释 (转)

    java的几种对象(PO,VO,DAO,BO,POJO)解释 一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中 ...

  6. composer 安装教程

    https://getcomposer.org/download/ 邓士鹏 1.先检查php.ini是否开启ssl ;extension=php_openssl.dll   2. php -r &qu ...

  7. 基于Mysql-Proxy 实现MariaDB 读写分离

    一.Mysql-Proxy 简单介绍 MySQL-Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包 ...

  8. csu1395模拟

    #include<stdio.h> #include<string.h> #define N  10 char s[N][N][N]={{"***",&qu ...

  9. [bzoj1002][FJOI2007]轮状病毒_递推_高精度

    轮状病毒 bzoj-1002 FJOI-2007 Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2 ...

  10. 【Storm】storm安装、配置、使用以及Storm单词计数程序的实例分析

    前言:阅读笔记 storm和hadoop集群非常像.hadoop执行mr.storm执行topologies. mr和topologies最关键的不同点是:mr执行终于会结束,而topologies永 ...