小菜的SharePoint Tips
前文用C#代码实现了Facade模式、Adapter模式、Strategy模式、Bridge模式和Abstract Factory模式解决实际业务需求。本文将继续以C#代码为例,实现我在前面介绍的11个模式中剩下的6个模式:Decorator模式、Observer模式、Template Method模式、Singleton模式、Factory Method模式和Prototype模式。按照实现方法的不同,该11个模式可分成三类:创建型模式(Abstract Factory, Singleton, Factory Method, Prototype)、结构型模式(Adapter, Bridge, Decorator)和行为型模式(Strategy, Observer, Template Method)。
一.Decorator模式
业务场景:在中国销售的一块手表,基本配置是显示北京时间,但也提供带有高级功能的配置。如:中端配置支持显示当前时区下的日期、高端配置支持第二时区(具体可以用户设置)。采用Decorator模式实现的代码如下:
namespace PartternCase
{
public abstract class AbstractWatch
{//Component in UML
public abstract void StartWork();
} public class Watch : AbstractWatch
{//ConcreteComponent in UML
public override void StartWork()
{
//Show Beijing time
}
} public abstract class WatchDecorator : AbstractWatch
{//Decorator in UML
protected AbstractWatch Watch { get; set; }
protected WatchDecorator(AbstractWatch watch)
{
Watch = watch;
}
} public class ShowDateDecorator : WatchDecorator
{//ConcreteDecoratorA in UML
public ShowDateDecorator(AbstractWatch watch)
: base(watch)
{
} public override void StartWork()
{
Watch.StartWork();
//Show date of current time zone
}
} public class ShowSecTimeZoneDecorator : WatchDecorator
{//ConcreteDecoratorB in UML
public ShowSecTimeZoneDecorator(AbstractWatch watch)
: base(watch)
{
} public override void StartWork()
{
Watch.StartWork();
// Show time of sencond timezone
}
} public class Client
{//How to use decorator pattern
public void ChooseWatch()
{
var watch = new Watch();//Basic function
var middleWatch = new ShowDateDecorator(watch);//Middle: contain show date function
var advanceWatch = new ShowSecTimeZoneDecorator(middleWatch);//Advance: contain show date and second time zone function
}
}
}
二.Observer模式
业务场景:盛天公司内部的报销有严格的审批流程,为提高审批效率,当员工提交报销单时,需要向具有审批权限的领导自动发送申请审批的邮件(当然,除报销单外,领导还会收到其他类型的审批邮件)。采用Observer模式实现的代码如下:
namespace PartternCase
{
public abstract class Report
{//Subject in UML
public string Status { get; set; }
protected IList<Employee> Employees { get; set; }
public void Attach(Employee employee)
{
Employees = Employees ?? new List<Employee>();
Employees.Add(employee);
} public void Detach(Employee employee)
{
if (Employees == null) { return; }
Employees.Remove(employee);
} public void Notify(Report report)
{
if (Employees == null) { return; }
foreach (var employee in Employees)
{
employee.HandleReportSubmit(report);
}
}
} public class ExpenseReport : Report
{//ConcreteSubject in UML
public void Submit()
{
Status = "Report Submited";
Notify(this);
}
} public abstract class Employee
{//Observer in UML
public abstract void HandleReportSubmit(Report report);
} public class Boss : Employee
{//ConcreteObserver in UML
public string Status { get; set; }
public override void HandleReportSubmit(Report report)
{
// Send email to this boss by smtp server
Status = report.Status;
}
} public class Client
{//How to use observer pattern
public void Observer()
{
var boss1 = new Boss();
var boss2 = new Boss();
var expense = new ExpenseReport();
expense.Attach(boss1);
expense.Attach(boss2);
expense.Submit();
}
}
}
三.Template Method模式
业务场景:圣象饮料公司生产牛奶和矿泉水两种饮料。运营过程都分为生产和销售两个环节,但两种饮料的生产和销售方式不相同。如销售方式:牛奶每天通过送货员送货上门,矿泉水通过各大超市销售。采用Template Method模式实现的代码如下:
namespace PartternCase
{
public abstract class DrinkOperation
{//AbstractClass in UML
public void Perform()
{//Interface for client call
Product();
Sales();
} protected abstract void Product();
protected abstract void Sales();
} public class MilkOperation : DrinkOperation
{//ConcreteClass in UML
protected override void Product()
{
//Get raw material from milk station then process
} protected override void Sales()
{
//Sell by deliveryman
}
} public class WaterOperation : DrinkOperation
{//ConcreteClass in UML
protected override void Product()
{
//Get raw material from mountain then process
} protected override void Sales()
{
//Sell by supermarket
}
}
}
四. Singleton模式
业务场景:投资组合管理系统中经常会用到一些复杂的模型对投资组合的价值进行估值。这些计算模型往往都放在一个类中,互相之间没有依赖关系。采用Singleton模式实现的代码如下:
namespace PartternCase
{
public class CalculationEngine
{//Singleton in UML
protected CalculationEngine() { } private static readonly CalculationEngine instance = new CalculationEngine();
public static CalculationEngine GetInstance { get { return instance; } }// Interface for client call //Many calculation methods
}
}
五. Factory Method模式
业务场景:圣天基金公司旗下的基金在向合伙人提款时,需根据合伙人类型不同(普通合伙人:GP、有限合伙人:LP)将提款数据存放在相应的合伙人类型中,然后保存。采用Factory Method模式实现的代码如下:
namespace PartternCase
{
public abstract class CapitalCall
{//Creator in UML
public abstract Partner BuildPartner(); public void Perform()
{
var partner = BuildPartner();
//Calculate amount of partner then save partner
}
} public class GpCapitalCall : CapitalCall
{//ConcreteCreator in UML
public override Partner BuildPartner()
{
return new GP();
}
} public class Partner
{//Product in UML
public decimal Amount { get; set; }
} public class GP : Partner
{//ConcreteProduct in UML
}
}
六. Prototype模式
业务场景:圣象饮料公司在查询牛奶和矿泉水的季度销量时,会分别针对Milk和Water数据库表进行查询,查询条件除时间外从外部传入,内部只设置时间条件,但不能改变传入的查询条件。下面将采用Prototype模式实现该业务场景:
namespace PartternCase
{
public abstract class DrinkCriteria
{//Prototype in UML
public abstract string QueryTable { get; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
//Other query fields public abstract DrinkCriteria Clone();
} public class MilkCriteria : DrinkCriteria
{//Concrete Prototype1 in UML
public override string QueryTable { get { return "Milk"; } } public override DrinkCriteria Clone()
{//This is shallow copy, in other case maybe need deep copy.
return MemberwiseClone() as MilkCriteria;
}
} public class WaterCriteria : DrinkCriteria
{//Concrete Prototype2 in UML
public override string QueryTable { get { return "Water"; } } public override DrinkCriteria Clone()
{
return MemberwiseClone() as WaterCriteria;
}
} public class Client
{//Client in UML
//Set relative criteria at runtime
private DrinkCriteria Criteria { get; set; }
public void HowToUse()
{
var newCritera = Criteria.Clone();
//newCritera.StartDate = 2013-1-1;
//newCritera.EndDate = 2013-3-31;
//Start to query user new criteria
}
}
}
SharePoint
小菜的SharePoint Tips的更多相关文章
- Office 365 - SharePoint Tips & Tricks
1. Recycle Bin 地址: //管理员 /_layouts/15/AdminRecycleBin.aspx //普通用户 /_layouts/15/RecycleBin.aspx 2.
- 【转】必需知道的 SharePoint 权限 Tips
SharePoint Tips about Permissions: What you need to know I have been writing tips about Shar ...
- SharePoint “File not found” 错误
Troubleshooting the SharePoint "File not found" Error Have you ever come across a "Fi ...
- 修改SharePoint 2013中Search Topology时遇到的一些问题以及一些Tips
这次操作在笔者的场中涉及到5台服务器, CA, APP2, APP3, APP4, APP5. 原本只有CA运行着Search Service Applicaiton, 现在想让APP2-5运行这项服 ...
- SharePoint 2010自定义母版页小技巧——JavaScript和CSS引用
通常在我们的项目中,都会涉及到母版页的定制.并且必不可少的,需要配合以一套自己的JavaScript框架和CSS样式.你有没有遇到过这样的情况呢,在开发环境和UAT时都还算顺利,但是当最终部署到生产服 ...
- SharePoint配置搜索服务和指定搜索范围
转载:http://constforce.blog.163.com/blog/static/163881235201201211843334/ 一.配置SharePoint Foundation搜索 ...
- How to Get SharePoint Client Context in SharePoint Apps (Provider Hosted / SharePoint Access ) in CSOM (Client Side Object Model)
http://www.codeproject.com/Articles/581060/HowplustoplusGetplusSharePointplusClientplusContex Downlo ...
- Creating a SharePoint BCS .NET Connectivity Assembly to Crawl RSS Data in Visual Studio 2010
from:http://blog.tallan.com/2012/07/18/creating-a-sharepoint-bcs-net-assembly-connector-to-crawl-rss ...
- SharePoint 开发小结
目标:将sharepoint网站对接Office 365 最直接的API:How to: Add Office 365 APIs to a Visual Studio project http://m ...
随机推荐
- 使用PHP顶替JS有趣DOM
較简单,我须要把一个导航页的数据整理好写入数据库.一个比較直观的方法是对html文件进行分析.通用的方法是用php的正則表達式来匹配.可是这样做开发和维护都非常困难,代码可读性非常差. 导航页的数据都 ...
- Eclipse的安装及汉化图解
Eclipse的安装及汉化图解 Eclipse的安装 有了JDK,你可以编译Java源码,运行Java程序,但是还没有代码编辑器,没有版本管理工具,也不能方便的管理工程文件,不能与团队协作.安装Ecl ...
- javascript权威指南(6) - 对象
JavaScript对象可以从一个称为原型的对象继承属性,这种"原型式继承"(prototypal inheritance)是JavaScript的核心特征.除了字符串.数字.tr ...
- 【转】Appium 服务器端从启动到case完成的活动分析
原文地址:http://blog.csdn.net/zhubaitian/article/details/39474151 此文的目的主要是通过分析Appium Server打印出来的log,加深对A ...
- CSS3+HTML5特效2 - 翻转效果
先看效果,鼠标移上去看看. back front 1. 本实例需要以下元素 a. 容器BOX b. 默认显示元素FRONT c. 翻转显示元素BACK 2. 容器BOX的Height为200px,Wi ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- AWK增强的文本处理shell特征--AWK完全手册
AWK这是一个很好的文字处理工具. 它不仅 Linux 中也是不论什么环境中现有的功能最强大的数据处理引擎之中的一个. 本文主要摘录池中龙写的Unixawk使用手冊(第二版),对当中内容略微修改.感谢 ...
- CKPlayer从Cookie里读取上次播放记录的一个demo
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- WCF与Web API 区别
WCF与Web API 区别(应用场景) Web api 主要功能: 支持基于Http verb (GET, POST, PUT, DELETE)的CRUD (create, retrieve, ...
- Asp.Net MVC路由调试好帮手RouteDebugger
Asp.Net MVC路由调试好帮手RouteDebugger 1.获取方式 第一种方法: 在程序包控制台中执行命令 PM> Install-Package routedebugger 安装成功 ...