单例模式:DbContextFactory.cs

 using CZBK.ItcastOA.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks; namespace CZBK.ItcastOA.DAL
{
public class DbContextFactory
{
/// <summary>
/// 保证EF上下文实例线程内唯一.
/// </summary>
/// <returns></returns>
public static DbContext CreateDbContext()
{
DbContext dbContext = (DbContext)CallContext.GetData("db");
if (dbContext == null)
{
dbContext = new OAEntities();
CallContext.SetData("db", dbContext);
}
return dbContext;
}
}
}
 public class Singleton
{
private static Singleton _instance = null;
private Singleton(){}
public static Singleton CreateInstance()
{
if(_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}

工厂模式:SimpleFactory.cs

 using CZBK.Shop.IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CZBK.Shop.DalFactory
{
/// <summary>
/// 工厂模式:解决的就是对象的创建(将对象的创建封装起来)
/// </summary>
public class SimpleFactory
{
public static IUserInfoDal CreateUserInfoDal()
{
return new SqlServerDal.UserInfoDal();
}
}
}

抽象工厂模式:AbstractFactory.cs

 using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace CZBK.Shop.DalFactory
{
/// <summary>
/// 抽象工厂:本质上与工厂是一样的,都是负责创建对象,但是创建的方式不一样。
/// </summary>
public class AbstractFactory
{
private static readonly string DalAssembly = ConfigurationManager.AppSettings["DalAssembly"];
private static readonly string DalNameSpace = ConfigurationManager.AppSettings["DalNameSpace"];
public static IDal.IUserInfoDal CreateUserInfoDal()
{
//通过反射的形式来创建.
string fullClassName = DalNameSpace + ".UserInfoDal";
return CreateInstance(fullClassName) as IDal.IUserInfoDal;
}
private static object CreateInstance(string fullClassName)
{
var assmebly = Assembly.Load(DalAssembly);//加载程序集.
return assmebly.CreateInstance(fullClassName);
}
}
}

003-单例OR工厂模式的更多相关文章

  1. Redis 单例、主从模式、sentinel 以及集群的配置方式及优缺点对比(转)

    摘要: redis作为一种NoSql数据库,其提供了一种高效的缓存方案,本文则主要对其单例,主从模式,sentinel以及集群的配置方式进行说明,对比其优缺点,阐述redis作为一种缓存框架的高可用性 ...

  2. PHP设计模式之工厂/单例/注册者模式

    工厂模式 简单工厂模式 [静态工厂方法模式](Static Factory Method)是类的创建模式 工厂模式的几种形态: 1.简单工厂模式(Simple Factory)又叫做 静态工厂方法模式 ...

  3. JavaScript 创建对象之单例、工厂、构造函数模式

    01单例模式 首先看一个问题,我们要在程序中描述两个人,这两个人都有姓名和年龄,可能刚刚开始学习js的时候会写成这样: var name1 = 'iceman'; var age1 = 25; var ...

  4. oop的三种设计模式(单例、工厂、策略)

    参考网站 单例模式: 废话不多说,我们直接上代码: <?php /** 三私一公 *私有的静态属性:保存类的单例 *私有的__construct():阻止在类的外部实例化 *私有的__clone ...

  5. java面试记录一:跳表、判断二叉树相同、冒泡排序、cookie和session的区别、设计模式(单例、工厂、模板方法、原型、代理、策略)、抽象类与接口的区别

    1.什么是跳表? 跳表实际上就是多层链表 跳表可用在让链表的元素查询接近线性时间 代码结构及java实现参考博客园随笔 2.判断两棵二叉树是否相同?(结构相同,内容相同) 思路:(1)先定义树节点Tr ...

  6. Swift百万线程攻破单例(Singleton)模式

    一.不安全的单例实现 在上一篇文章我们给出了单例的设计模式,直接给出了线程安全的实现方法.单例的实现有多种方法,如下面: class SwiftSingleton { class var shared ...

  7. JavaScript设计模式 样例一 —— 工厂模式

    工厂模式(Factory Pattern): 定义:定义一个创建对象的接口,但让实现这个接口的类来决定实例化哪个类.工厂方法让类的实例化推迟到子类中进行. 目的:工厂模式是为了解耦,把对象的创建和使用 ...

  8. Redis单例、主从模式、sentinel以及集群的配置方式及优缺点对比

    https://my.oschina.net/zhangxufeng/blog/905611

  9. PHP设计模式-工厂模式、单例模式、注册模式

    本文参考慕课网<大话PHP设计模式>-第五章内容编写,视频路径为:http://www.imooc.com/video/4876 推荐阅读我之前的文章:php的设计模式 三种基本设计模式, ...

随机推荐

  1. fillder---工具栏隐藏/显示

    显示隐藏工具栏方法:view---show toolbar

  2. python 用xlwt包把数据导出到excel表中

    def write_excel(): f = xlwt.Workbook() #创建工作簿 ''' 创建第一个sheet: sheet1 ''' sheet1 = f.add_sheet(u'shee ...

  3. B. Nirvana Codeforces Round #549 (Div. 2) (递归dfs)

    ---恢复内容开始--- Kurt reaches nirvana when he finds the product of all the digits of some positive integ ...

  4. 微信小程序免费Https获取以及Ubuntu Nginx配置

    先贴上Nginx的配置文件 user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections ...

  5. 1#Two Sum(qsort用法)

    void*空类型指针,就好像暂时还没有确定类型,任何类型都可以赋给它.但是具体操作时一定要确定类型(如下,比较时先转Node) cmp返回一定是int,有-1,0,1三种,如果是1则第一个数要放在第二 ...

  6. CSS3_文本样式

    1. 文字阴影 text-shadow 使用: text-shadow:    水平方向偏移量    垂直方向偏移量    模糊程度    颜色; #box { text-shadow: 10px 1 ...

  7. 自定义 绑定响应函数 解除响应函数 .addEventListener 兼容 .attachEvent

    嗯哼.不多说,直接上代码. // 自定义 绑定响应函数 兼容性封装 Test Already. function bindEventFunc(obj, eventStr, func){ // cons ...

  8. JavaScript学习day2 (基本语法上)

    知识点 JavaScript 的变量 数据类型 运算符 JavaScript 的动态类型 变量:(变量的命名规则和其他语言类似) 由数字,字母,下划线组成,区分大小写 以字母开头 变量名不能有空格 不 ...

  9. JavaScript Dom级别

  10. c#拷贝整个文件夹到指定文件夹下(非递归)

    public static void CopyEntireDir(string sourcePath, string destPath) { //Now Create all of the direc ...