常用C#设计模式(上)

DP是coding中经常谈到的问题,虽然DP种类繁多,但是常用的DP不过几种,这里将浅析C#常用的设计模式。

【单例模式】

描述:最“简单”的设计模式,顾名思义,这个类型只有一个实例,不能创建其他实例。这个类型提供一个公共的访问点让用户操作这个实例。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Single
{
public void Print()
{
System.Console.Write("I am a singleton.");
}

private Single()
{

}

private static Single mySingle = new Single();

public static Single MySingle
{
get { return mySingle = new Single(); }
}

}

实现单例的操作首先是将这个类的构造函数私有化,然后通过一个静态的变量初始化构造函数。这样只有这个静态的变量才能作为这个类型的唯一对象存在。当我们需要调用Single类的对象及方法时,只需要
用下面的代码就可以了。

Single.MySingle.Print();

【简单工厂模式】

描述:所谓工厂就是一个加工东西的地方,我们不管这个东西是怎么加工的,只需要提供原料,然后等待成品。在DP中,简单工厂模式提供一个封装好的创建对象的方法,根据我们传入的参数,来返回不同的对象实例。

例子:

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
//定义
public interface Fruit
{
void Print();
}

public class Apple : Fruit
{
public void Print()
{
Console.Write("Apple");
}
}

public class Pear : Fruit
{
public void Print()
{
Console.Write("Pear");
}
}

public class Factory
{
public static Fruit creator(int which)
{
if (which == 1)
return new Apple();
else
return new Pear();
}
}

//调用
Fruit fruit = Factory.creator(1);
fruit.Print();

简单工厂能够返回具有相同方法的类的实例,但是如果我们再派生几个类,就需要修改Factory的creator方法来增加判断条件了,这违背了“开闭原则”。把众多的功能(如判断)塞进一个工厂里是违背“高内聚低耦合”原则。所以说当派生的类不多,情况简单时用简单工厂模式就可以,但是如果派生类繁多,就不得不说到简单工厂的进化版–工厂方法模式。

【工厂方法模式】

描述:工厂类Factory不再负责产品的创建,仅负责具体工厂子类必须实现的接口,将实际创建工作推迟到子类当中。

例子:

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
//定义
public interface Fruit
{
void Print();
}

public class Apple : Fruit
{
public void Print()
{
Console.Write("Apple");
}
}

public class Pear : Fruit
{
public void Print()
{
Console.Write("Pear");
}
}

public abstract class Factory
{
public abstract Fruit Creat();
}

public class AppleFactory : Factory
{
public override Fruit Creat()
{
return new Apple();
}
}

public class PearFactory : Factory
{
public override Fruit Creat()
{
return new Pear();
}
}

//调用
Factory myfactory = new AppleFactory();
Fruit fruit = myfactory.Creat();
fruit.Print();

我们可以看到相对于简单工厂模式来说,工厂方法模式把工厂抽象化,再细分成不同类的小工厂来应对复杂问题。同时满足“开闭原则”,“高内聚低耦合”原则。但是也存在一个问题,每当增加一个类型的派生类时,就需要增加与之类型对应的工厂。所以使用工厂方法模式应当合理控制派生类型。

【抽象工厂模式】

描述:抽象工厂模式是为了应对更为复杂的情况从工厂方法模式变更而来。这个复杂情况可以总结如下

工厂方法模式: 一个抽象产品类,派生多个具体产品类。
一个抽象工厂类,派生多个具体工厂类。
每个具体工厂类创建一个具体产品类的实例。

抽象工厂模式: 多个抽象产品类,派生多个具体产品类。
一个抽象工厂类,派生多个具体工厂类。
每个具体工厂类创建多个具体产品类的实例。

例子:

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
79
//定义
public interface Fruit
{
void Print();
}

public class Apple : Fruit
{
public void Print()
{
Console.Write("Apple");
}
}

public class Pear : Fruit
{
public void Print()
{
Console.Write("Pear");
}
}

public interface Meat
{
void Print();
}

public class Beaf : Meat
{
public void Print()
{
Console.Write("Beaf");
}
}

public class Lamb : Meat
{
public void Print()
{
Console.Write("Lamb");
}
}

public interface Factory
{
Fruit CreatFruit();
Meat CreatMeat();
}

public class FactoryA : Factory
{
public Fruit CreatFruit()
{
return new Apple();
}

public Meat CreatMeat()
{
return new Lamb();
}
}

public class FactoryB : Factory
{
public Fruit CreatFruit()
{
return new Pear();
}

public Meat CreatMeat()
{
return new Beaf();
}
}

//调用
Factory myfactory = new FactoryA();
Meat meat = myfactory.CreatMeat();
meat.Print();

常用C#设计模式(上)

https://wurang.net/csharp_design_pattern_a/

作者

Wu Rang

发布于

2014-03-07

更新于

2021-12-06

许可协议

评论