public abstract class OrderLog{
public string Content {get;set;}
public OrderLog(string content){
Content = content;
}
}
public class PlaceOrderLog :OrderLog{
public PlaceOrderLog(string content,string orderedBy):base(content)
{
OrderedBy = orderedBy;
}
public string OrderedBy {get;set;}
}
public class MakePaymentLog :OrderLog{
public MakePaymentLog(string content, string paymentGateway, string payedBy):base(content)
{
PaymentGateway = paymentGateway;
PayedBy = payedBy;
}
public string PaymentGateway {get;set;}
public string PayedBy {get;set;}
}
public class OrderCompleteLog : OrderLog{
public OrderCompleteLog(string content, DateTime completeDate):base(content)
{
OrderCompleteDate = completeDate;
}
public DateTime OrderCompleteDate {get;set;}
}
public class OrderLogger{
public static OrderLogger Do
{
get
{
return new OrderLogger();
}
}
public IEnumerable<OrderLog> GetLogs(){
return new List<OrderLog>{
new PlaceOrderLog("place order log","ordered by"),
new MakePaymentLog("make payment log","paypal", "payedBy"),
new OrderCompleteLog("order complete log",DateTime.Now)
};
}
}
var logs = OrderLogger.Do.GetLogs();
public abstract T Visit<T>(ILogVisitor<T> visitor);
public override T Visit<T>(ILogVisitor<T> visitor){
return visitor.Visit(this);
}public interface ILogVisitor<T>{
T Visit(PlaceOrderLog log);
T Visit(MakePaymentLog log);
T Visit(OrderCompleteLog log);
}public enum OrderLogColor{Red,Green}
public class OrderLogColorVisitor:ILogVisitor<OrderLogColor>{
public OrderLogColor Visit(PlaceOrderLog log){
return OrderLogColor.Red;
}
public OrderLogColor Visit(MakePaymentLog log){
return OrderLogColor.Green;
}
public OrderLogColor Visit(OrderCompleteLog log){
return OrderLogColor.Green;
}
}var logs = OrderLogger.Do.GetLogs();
////show colors
var colorVisitor = new OrderLogColorVisitor();
foreach(var log in logs){
Console.WriteLine(log.Visit<OrderLogColor>(colorVisitor));
}Red Green Green
public class OrderLogFormattedVisitor : ILogVisitor <string> {
public string Visit(PlaceOrderLog log){
return "this is place order log formatted information ";
}
public string Visit(MakePaymentLog log){
return "this is make payment log formatted information ";
}
public string Visit(OrderCompleteLog log){
return "this is order complete log formatted information ";
}
}
var logs = OrderLogger.Do.GetLogs();
var formatVisitor = new OrderLogFormattedVisitor();
foreach(var log in logs){
Console.WriteLine(log.Visit<string>(formatVisitor));
}
void Main()
{
var logs = OrderLogger.Do.GetLogs();
////show colors
var colorVisitor = new OrderLogColorVisitor();
foreach(var log in logs){
Console.WriteLine(log.Visit<OrderLogColor>(colorVisitor));
}
////show formatted logs
var formatVisitor = new OrderLogFormattedVisitor();
foreach(var log in logs){
Console.WriteLine(log.Visit<string>(formatVisitor));
}
}
////team A job
public abstract class OrderLog{
public string Content {get;set;}
public OrderLog(string content){
Content = content;
}
public abstract T Visit<T>(ILogVisitor<T> visitor);
}
public class PlaceOrderLog :OrderLog{
public PlaceOrderLog(string content,string orderedBy):base(content)
{
OrderedBy = orderedBy;
}
public string OrderedBy {get;set;}
public override T Visit<T>(ILogVisitor<T> visitor){
return visitor.Visit(this);
}
}
public class MakePaymentLog :OrderLog{
public MakePaymentLog(string content, string paymentGateway, string payedBy):base(content)
{
PaymentGateway = paymentGateway;
PayedBy = payedBy;
}
public string PaymentGateway {get;set;}
public string PayedBy {get;set;}
public override T Visit<T>(ILogVisitor<T> visitor){
return visitor.Visit(this);
}
}
public class OrderCompleteLog : OrderLog{
public OrderCompleteLog(string content, DateTime completeDate):base(content)
{
OrderCompleteDate = completeDate;
}
public DateTime OrderCompleteDate {get;set;}
public override T Visit<T>(ILogVisitor<T> visitor){
return visitor.Visit(this);
}
}
public class OrderLogger{
public static OrderLogger Do
{
get
{
return new OrderLogger();
}
}
public IEnumerable<OrderLog> GetLogs(){
return new List<OrderLog>{
new PlaceOrderLog("place order log","ordered by"),
new MakePaymentLog("make payment log","paypal", "payedBy"),
new OrderCompleteLog("order complete log",DateTime.Now)
};
}
}
////now team B want a API , return different log color based on different type
////team A give team B a visitor interface , ask them to implement
public interface ILogVisitor<T>{
T Visit(PlaceOrderLog log);
T Visit(MakePaymentLog log);
T Visit(OrderCompleteLog log);
}
////team B come out with the implementations
public enum OrderLogColor{Red,Green}
public class OrderLogColorVisitor:ILogVisitor<OrderLogColor>{
public OrderLogColor Visit(PlaceOrderLog log){
return OrderLogColor.Red;
}
public OrderLogColor Visit(MakePaymentLog log){
return OrderLogColor.Green;
}
public OrderLogColor Visit(OrderCompleteLog log){
return OrderLogColor.Green;
}
}
////in future
////if team B want more , just implement the ILogVisitor interface
////for example : formatted log
public class OrderLogFormattedVisitor : ILogVisitor <string> {
public string Visit(PlaceOrderLog log){
return "this is place order log formatted information ";
}
public string Visit(MakePaymentLog log){
return "this is make payment log formatted information ";
}
public string Visit(OrderCompleteLog log){
return "this is order complete log formatted information ";
}
}
////whats next
////whenever team B want , he just add more visitor to accept different log types returned from teamA
原文:http://blog.csdn.net/lan_liang/article/details/42373201