asp.net mvc 学习笔记 - 单一实例设计模式
学习之前,先喊一下口号:每天进步一点,生活更好一点
首先声明一点,我也是新新新手一枚,崭新的新哦。如果文章有不合理的地方,也请各位博友多多指点,不要乱喷哦
我的文采很低调,低调到语文老师对我的期望是你什么时候能及格啊!!!▄█▀█●给跪了@@@ 所以我的文章都是直奔主题,没有华丽的装饰,没准可以美而言之『通俗易懂』呢ヾ(=゚・゚=)ノ喵♪
好了,可以开始了
我们声明两个类 Customer 和 Order
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
}
public class Order
{
public int OrderID { get; set; }
public string CustomerID { get; set; }
public int? EmployeeId { get; set; }
public DateTime? OrderDate { get; set; }
}
如果是刚开始学习编程,如果要对这两个类进行操作,就需要建立两个操作类 CustomerOperation 和 OrderOperation
public class CustomerOperation
{
public void Creat(Customer item)
{ } public void Delete(Customer item)
{ } public IEnumerable<Customer> Get()
{ } public void Update(Customer item)
{ }
}
public class OrderOperation
{
public void Creat(Order item)
{ } public void Delete(Order item)
{ } public IEnumerable<Order> Get()
{ } public void Update(Order item)
{ }
}
好了,关于两个类的CRUD(增查改删)就写好了,下面我们就可以使用了
static void Main(string[] args)
{
CustomerOperation customer = new CustomerOperation();
OrderOperation order = new OrderOperation();
customer.Get();
order.Get();
Console.ReadLine();
}
这样一来,我们就new了两个实例,如果我们有一百个类,岂不是要new一百遍!!o(>﹏<)o
那怎么样new一个实例,可以同时操作两个类呢,那还不简单,我们把两个操作类,写到一个里边
public class Factory
{
public void CustomerCreat(Customer item)
{ } public void CustomerDelete(Customer item)
{ } public IEnumerable<Customer> CustomerGet()
{ } public void CustomerUpdate(Customer item)
{ }
public void OrderCreat(Order item)
{ } public void OrderDelete(Order item)
{ } public IEnumerable<Order> OrderGet()
{ } public void OrderUpdate(Order item)
{ }
}
这时,我们再使用的时候,不就直接实例化一个就可以了
static void Main(string[] args)
{
Factory factory = new Factory();
Factory.CustomerGet();
Factory.OrderGet();
Console.ReadLine();
}
果然聪明,这就基本上可以称作是单一实例模式了
但是,如果将来有一百个类,你的Factory岂不是要爆掉,那怎么办呢,我已经修炼了第一层功力,我是这样做的
原来的两个操作类不变,在Factory里边进行实例化
public class Factory
{
private CustomerOperation _customerOperation = null;
private OrderOperation _orderOperation = null; public Customer Customers
{
get
{
if (_customerOperation == null)
{
_customerOperation = new CustomerOperation();
} return this._customerOperation;
}
} public Order Orders
{
get
{
if (_orderOperation == null)
{
_orderOperation = new OrderOperation();
} return this._orderOperation;
}
}
}
然后我们再去使用的时候
static void Main(string[] args)
{
Factory factory = new Factory();
factory .Orders.Get();
factory .Customers.Get();
Console.ReadLine();
}
这样操作起来是不是就直观很多了,代码量也减少了很多,这就是我理解的单一实例模式
asp.net mvc 学习笔记 - 单一实例设计模式的更多相关文章
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则
ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...
- ASP.NET MVC学习笔记-----Filter2
ASP.NET MVC学习笔记-----Filter(2) 接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用 ...
- ASP.NET MVC学习笔记-----Filter
ASP.NET MVC学习笔记-----Filter(1) Filter类型 接口 MVC的默认实现 Description Authorization IAuthorizationFilter Au ...
- ASP.NET MVC学习笔记-----Filter(2)
接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用,它需要实现IActionFilter接口: public ...
- ASP.NET MVC 学习笔记(1)
从头开始系统地学习ASP.NET MVC 为什么要学习ASP.NET MVC?原因很多,可以先来看一下最早的ASP.NET WebForm的一些缺点: 传说中面试经常要问到的ASP.NET WebFo ...
- ASP.NET MVC学习笔记-----使用自定义的View Engine
我们都知道在ASP.NET MVC中自带了Razor View Engine,Razor十分的强大,可以满足我们绝大部分的需要.但是ASP.NET MVC的高度可扩展性,使我们可以使用自定义的View ...
- ASP.NET MVC学习笔记-----ActionInvoker
还是这张图: 当ControllerFactory生成Controller实例后,这时就需要使用ActionInvoker来选择调用一个合适的Action执行.ASP.NET MVC提供的基类Cont ...
- ASP.NET MVC 学习笔记 1
1. 什么是ASP.Net MVC ASP.Net MVC是一种开发Web应用程序的工具(is a web application development framework),采用Model-Vie ...
随机推荐
- 如何在js里引用php变量
如何在js里面引用php的变量 php代码------------------------------------------- js代码------------------------------- ...
- sqlserver 自动创建作业执行备份数据库
declare @name varchar(250)set @name='I:\dydb_n\dydb_n'+convert(varchar(50),getdate(),112)+ left(righ ...
- node-webkit学习(1)hello world
)hello world 文/玄魂 目录 node-webkit学习(1)hello world 前言 1.1 环境安装 1.1.1 windows下的安装 1.1.2 linux环境下的安装 1 ...
- WinRAR试用过期决绝方法
一.WinRAR 试用过期决绝方法 直接去WINRAR官方下个版本装上然后这样 复制以下内容(红色)到记事本,保存为rarreg.key文件(即文件名是rarreg,扩展名是key),把这文件拷贝到W ...
- ServiceStack DateTime数据类型转Json出现的困扰
执行dotnet-new selfhost sstest 创建项目,然后打开解决方案 修改ssTest.ServiceModel中的Hello.cs,在HellopResponse中添加时间属性,然后 ...
- .net图表之ECharts随笔04-散点图
见图说话,修改参数option实现上图显示: 1. 共用参数title还有一个属性subtext,可以用来设置副标题 2. tooltip与toolbox也是共用属性 3. dataZoom是设置滚动 ...
- Android 四大组件之“ BroadcastReceiver ”
前言 Android四大组件重要性已经不言而喻了,今天谈谈的是Android中的广播机制.在我们上学的时候,每个班级的教室里都会装有一个喇叭,这些喇叭都是接入到学校的广播室的,一旦有什么重要的通知,就 ...
- Python3.5 学习二十二
回顾: 发送请求时:发送请求头和请求数据 request.META和request.request.body 响应请求时:响应头和响应返回数据 response.HEADER和response.bod ...
- spark踩坑——dataframe写入hbase连接异常
最近测试环境基于shc[https://github.com/hortonworks-spark/shc]的hbase-connector总是异常连接不到zookeeper,看下报错日志: 18/06 ...
- [vuejs] vue2.0-layer-mobile移动端弹层
vue2.0-layer-mobile移动端弹层 本次组件升级支持slot内容分发功能,实现高定制内容风格的弹层 安装方法 npm install vue2-layer-mobile -S 初始化 i ...