这篇文章主要为大家详细介绍了C#设计模式之行为型模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
正文
C#设计模式之行为型模式详解
这里列举行为型模式·到此23种就列完了···这里是看着菜鸟教程来实现··,他里边列了25种,其中过滤器模式和空对象模式应该不属于所谓的23种模式
责任链模式:为请求创建一个接收者对象的链,对请求的发送者和接收者进行解耦,大部分用于web中吧。。
Task中的continuewith和微软的tpl数据流应该是类似这种模式的实现吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
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的的命令应该就是以这种方式执行的·二次开发的时候通过特性标识和继承他的接口来添加命令,非常方便
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
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#的各种表达式就是这种实现吧··这玩意跟富文本编辑器一样是个大坑吧··,做好了确实很好使,一不小心就得跪
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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) { this .expr1 = expr1; this .expr2 = expr2; } public bool Interpret(String context) { return expr1.Interpret(context) && expr2.Interpret(context); } } } |
迭代器模式(Iterator Pattern):.NET自带接口···,直接实现就行了··注意又泛型接口和非泛型接口··非泛型接口迭代对象返回的是object,泛型接口返回的直接就是对象了,还有通过yield的简化写法不用额外去实现IEnumerator接口
发表评论