小菜的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 ...
随机推荐
- Spring + Spring MVC + Hibernate
Spring + Spring MVC + Hibernate项目开发集成(注解) Posted on 2015-05-09 11:58 沐浴未来的我和你 阅读(307) 评论(0) 编辑 收藏 在自 ...
- 【高德地图API】从零开始学高德JS API(五)路线规划——驾车|公交|步行
原文:[高德地图API]从零开始学高德JS API(五)路线规划——驾车|公交|步行 先来看两个问题:路线规划与导航有什么区别?步行导航与驾车导航有什么区别? 回答: 1.路线规划,指的是为用户提供3 ...
- Asp.net MVC + EF + Spring.Net 项目实践(二)
这一篇主要介绍数据库和entity framework集成 1. 先创建数据库,很简单的几张表,但也涉及了一些主外键关系.联合主键等,关系图如下(DB脚本在最后) 2. 打开VS,现在建立Model实 ...
- 使用Jenkins来构建Docker容器
使用Jenkins来构建Docker容器(Ubuntu 14.04) 当开发更新了代码,提交到Gitlab上,然后由测试人员触发Jenkins,于是一个应用的新版本就被构建了.听起来貌似很简单,dua ...
- Objective-C的动态特性
最近几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语言转过来的,如Java或C#,当然也有直接以Objective-C作为入门语言 ...
- IE6下jquery ajax报error的原因
用jquery ajax()方法,在其他浏览都通过,IE7以上都通过,唯独在ie6不行. 我这边的解决方案是:必须保证ajax里面的所有数字为小写,ie6对大小写敏感. 错误: $.ajax({ ur ...
- WaitHandle、AutoResetEvent、ManualResetEvent
多线程中的锁系统(三)-WaitHandle.AutoResetEvent.ManualResetEvent 介绍 本章主要说下基于内核模式构造的线程同步方式,事件,信号量. 目录 一:理论 二:Wa ...
- MAC中在eclipse luna上搭建移动平台自动化测试框架(UIAutomator/Appium/Robotium/MonkeyRunner)关键点记录
这几天因为原来在用的hp laptop的电池坏掉了,机器一不小心就断电,所以只能花时间在自己的macbook pro上重新搭建整套环境,大家都知道搭建环境是个很琐碎需要耐心的事情,特别是当你搭建的安卓 ...
- linux下C语言中的flock函数使用方法 .
表头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依參数operation所指 ...
- AngularJS学习笔记(一)——一些基本知识
(一)Hello Angular index.html <!DOCTYPE html> <html ng-app> <head> <title>Test ...