---
title: "ASP.NET设计模式读书笔记（1）---分离关注点"
author: "dotcpp"
date: "2013-05-27T21:38:19Z"
tags: []
categories: ["企业架构"]
slug: "asp-net-1"
draft: false
lastmod: "2013-05-27T21:38:19Z"
id: 1876148
url: "https://dotcpp.iteye.com/blog/1876148"
---

以前在学习设计模式的时候觉得很多模式都是纸上谈兵，在实际使用时根本就无从下手。就算是在ASP.NET MVC中这种感觉也很强烈，直到看了《ASP.NET设计模式》这本书后，才对设计模式的认识有了质的提高。第 一篇就记录一下3.1.2节的分离关注点这个例子吧！

**对于Layered.Model结构图如下：**

![](http://dl2.iteye.com/upload/attachment/0085/0201/6f37fd9f-bcc2-3083-8bb7-8cc2a3ae8eef.jpg)  
   
 

【StarUML】不能使用表示静态类，悲剧！！！

 

Product类表示商品信息；

Price类用于商品价格计算，包含建议零售价（RRP），实际售价（SellingPrice），折扣信息（Discount）；

IStrategy接口用于处理折扣策略；

StrategyFactory工厂类根据用户的类型，生成对应的折扣策略；

CustomerType表示客户类型，Standard表示没有折扣，Trade表示可以使用折扣；

IRepository接口为数据库访问提供接口；

ProductService为上层应用取商品列表提供一个接口；

ProductListExtensionMethod类是为了实现扩展方法，将IRepository中取得的IList&lt;Product&gt;全部应用上折扣策略

 

**Layered.Service的结构图如下：**

![](http://dl2.iteye.com/upload/attachment/0085/0864/f09486b1-69f3-380e-b945-910a1fae12e8.jpg)  
 ProductViewModel：用于显示的强类型视图；

ProductListRequest：用于模拟用户请求；

ProductListResponse：用户回复；

ProductServiceExtensionMethod：实现Product与List&lt;Product&gt;的扩展方法，转变为ProductViewModel；

ProductService：为Presentat提供调用接口；

 

**Layered.Presentation结构图**

   
![](http://dl2.iteye.com/upload/attachment/0085/0869/b76926af-b756-3854-9401-2f7097ff3003.jpg)  
 ProductListPresenter：为WebUI提供调用接口；

IProductListView：Web页面必须继承此接口，实现Display方法