没有合适的资源?快使用搜索试试~ 我知道了~
这里列举行为型模式·到此23种就列完了···这里是看着菜鸟教程来实现··,他里边列了25种,其中过滤器模式和空对象模式应该不属于所谓的23种模式 责任链模式:为请求创建一个接收者对象的链,对请求的发送者和接收者进行解耦,大部分用于web中吧。。 Task中的continuewith和微软的tpl数据流应该是类似这种模式的实现吧 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //责任链模式 namespac
资源详情
资源评论
资源推荐
C#设计模式之行为型模式详解设计模式之行为型模式详解
这里列举行为型模式·到此23种就列完了···这里是看着菜鸟教程来实现··,他里边列了25种,其中过滤器模式和空对象模式应该
不属于所谓的23种模式
责任链模式:为请求创建一个接收者对象的链,对请求的发送者和接收者进行解耦,大部分用于web中吧。。
Task中的continuewith和微软的tpl数据流应该是类似这种模式的实现吧
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//责任链模式
namespace ExercisePrj.Dsignmode
{
public abstract class AbstractLogger
{
public static int INFO = 1;
public static int DEBUG = 2;
public static int ERROR = 3;
protected int level;
//责任链中的下一个对象
protected AbstractLogger nextLogger;
public void SetNextLogger(AbstractLogger next)
{
nextLogger = next;
}
public void LogMessage(int level,string message)
{
if(this.level<=level)
{
Write(message);
}
if(nextLogger!=null)
{
nextLogger.LogMessage(level, message);
}
}
protected abstract void Write(string message);
}
public class ConsoleLogger : AbstractLogger
{
public ConsoleLogger(int level)
{
this.level = level;
}
protected override void Write(string message)
{
Console.WriteLine("Standard Console::Logger: " + message);
}
}
public class ErrorLogger : AbstractLogger
{
public ErrorLogger(int level)
{
this.level = level;
}
protected override void Write(String message)
{
Console.WriteLine("Error Console::Logger: " + message);
}
}
public class FileLogger : AbstractLogger
{
public FileLogger(int level)
{
this.level = level;
}
protected override void Write(String message)
{
Console.WriteLine("File::Logger: " + message);
}
}
}
命令模式(Command Pattern):请求以命令的形式执行,CAD的的命令应该就是以这种方式执行的·二次开发的时候通过特性
标识和继承他的接口来添加命令,非常方便
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//命令模式
namespace ExercisePrj.Dsignmode
{
public interface IOrder
{
void Execute();
}
public class Stock
{
private string name = "ABC";
private int quantity = 10;
public void Buy()
{
Console.WriteLine("Stock name:{0},quantity:{1},bought",name,quantity);
}
public void Sell()
{
Console.WriteLine("Stock name:{0},quantity:{1}sold", name, quantity);
}
}
//请求类
public class BuyStock : IOrder
{
private Stock abcStock;
public BuyStock(Stock abcStock)
{
this.abcStock = abcStock;
}
public void Execute()
{
abcStock.Buy();
}
}
//继承接口的实体
public class SellStock : IOrder
{
private Stock abcStock;
public SellStock(Stock abcStock)
{
this.abcStock = abcStock;
}
public void Execute()
{
abcStock.Sell();
}
}
//命令调用类
public class Broker
{
private List<IOrder> orderList = new List<IOrder>();
public void takeOrder(IOrder order)
{
orderList.Add(order);
}
public void placeOrders()
{
foreach (IOrder order in orderList)
{
order.Execute();
}
orderList.Clear();
}
}
}
解释器模式:就是实现一种表达式接口,C#的各种表达式就是这种实现吧··这玩意跟富文本编辑器一样是个大坑吧··,做好了
确实很好使,一不小心就得跪
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//解释器模式
namespace ExercisePrj.Dsignmode
{
public interface Expression
{
bool Interpret(string context);
}
public class TerminalExpression : Expression
{
private string data;
public TerminalExpression(string data)
{
this.data = data;
}
public bool Interpret(string context)
{
if (context.Contains(data))
{
return true;
}
return false;
}
}
public class OrExpression : Expression
{
private Expression expr1 = null;
private Expression expr2 = null;
public OrExpression(Expression expr1, Expression expr2)
{
this.expr1 = expr1;
this.expr2 = expr2;
}
public bool Interpret(String context)
{
return expr1.Interpret(context) || expr2.Interpret(context);
}
}
public class AndExpression : Expression
{
private Expression expr1 = null;
private Expression expr2 = null;
public AndExpression(Expression expr1, Expression expr2)
{
剩余11页未读,继续阅读
weixin_38652196
- 粉丝: 1
- 资源: 939
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0