设计模式
目录
Refactoring.Guru(设计模式学习) 网站地址:https://refactoringguru.cn/
创建型模式
工厂方法模式
工厂方法模式是一种创建型设计模式, 其在父类中提供一个创建对象的方法, 允许子类决定实例化对象的类型。
class Program
{
static void Main(string[] args)
{
new Client().Main();
Console.ReadKey();
}
public interface IProduct
{
string Operation();
}
class ConcreteProduct1 : IProduct
{
public string Operation()
{
return "result of ConcreteProduct1";
}
}
class ConcreteProduct2 : IProduct
{
public string Operation()
{
return "result of ConcreteProduct2";
}
}
abstract class Creator
{
public abstract IProduct FactorMethod();
public string SomeOperation()
{
var product = FactorMethod();
var result = product.Operation();
return result;
}
}
class ConcreteCreator1 : Creator
{
public override IProduct FactorMethod()
{
return new ConcreteProduct1();
}
}
class ConcreteCreator2 : Creator
{
public override IProduct FactorMethod()
{
return new ConcreteProduct2();
}
}
class Client
{
public void Main()
{
ClientCode(new ConcreteCreator1());
ClientCode(new ConcreteCreator2());
}
public void ClientCode(Creator creator)
{
Console.WriteLine(creator.SomeOperation());
}
}
}
抽象工厂模式
抽象工厂模式是一种创建型设计模式, 它能创建一系列相关的对象, 而无需指定其具体类。
class Program
{
static void Main(string[] srgs)
{
Console.WriteLine("Hello world");
new Client().Main();
}
}
public interface IAbstractProductA
{
string UsefulFunctionA();
}
class ConcreteProductA1 : IAbstractProductA
{
public string UsefulFunctionA()
{
return "result productA1";
}
}
class ConcretProductA2 : IAbstractProductA
{
public string UsefulFunctionA()
{
return "result productA2";
}
}
interface IAbstractProductB
{
string UsefulFunctionB();
string AnotherUsefulFunctionB(IAbstractProductA vcoll);
}
class ConcreteProductB1 : IAbstractProductB
{
public string UsefulFunctionB()
{
return "result productB1";
}
public string AnotherUsefulFunctionB(IAbstractProductA vcoll)
{
return vcoll.UsefulFunctionA() + "B1";
}
}
class ConcreteProductB2: IAbstractProductB
{
public string UsefulFunctionB()
{
return "result productB2";
}
public string AnotherUsefulFunctionB(IAbstractProductA vcoll)
{
return vcoll.UsefulFunctionA() + "B2";
}
}
interface IAbstractFactory
{
IAbstractProductA CreateProductA();
IAbstractProductB CreateProductB();
}
class ConcreteFactory1 : IAbstractFactory
{
public IAbstractProductA CreateProductA()
{
return new ConcreteProductA1();
}
public IAbstractProductB CreateProductB()
{
return new ConcreteProductB1();
}
}
class ConcreteFactory2: IAbstractFactory
{
public IAbstractProductA CreateProductA()
{
return new ConcretProductA2();
}
public IAbstractProductB CreateProductB()
{
return new ConcreteProductB2();
}
}
class Client
{
public void Main()
{
ClientCode(new ConcreteFactory1());
ClientCode(new ConcreteFactory2());
}
public void ClientCode(IAbstractFactory factory)
{
Console.WriteLine(factory.CreateProductB().UsefulFunctionB());
Console.WriteLine(factory.CreateProductB().AnotherUsefulFunctionB(factory.CreateProductA()));
}
}
生成器模式
原型模式
单例模式
单例模式是一种创建型设计模式, 让你能够保证一个类只有一个实例, 并提供一个访问该实例的全局节点。
单线程
public class Singleton
{
private Singleton()
{
}
private static Singleton _instance;
public static Singleton GetInstance()
{
return _instance ??= new Singleton();
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Singleton s1 = Singleton.GetInstance();
Singleton s2 = Singleton.GetInstance();
if (s1 == s2)
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}
}
}
多线程(线程安全的)
public class Singleton
{
private Singleton()
{
}
private static Singleton _instance;
private static readonly object _lock = new object();
public string Value { get; set; }
public static Singleton GetInstance(string value)
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new Singleton {Value = value};
}
}
}
return _instance;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Thread t1 = new Thread(() => { TestSingleton("foo"); });
Thread t2 = new Thread(() => { TestSingleton("bar"); });
t1.Start();
t2.Start();
t1.Join();
t2.Join();
}
private static void TestSingleton(string value)
{
Singleton singleton = Singleton.GetInstance(value);
Console.WriteLine(singleton.Value);
}
}
结构型模式
适配器模式
桥接模式
组合模式
装饰模式
外观模式
享元模式
代理模式
行为型模式
责任链模式
命令模式
迭代器模式
中介者模式
备忘录模式
观察者模式
观察者模式是一种行为设计模式, 允许你定义一种订阅机制, 可在对象事件发生时通知多个 “观察” 该对象的其他对象。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var subject = new Subject();
var observerA = new ConcreteObserverA();
subject.Attach(observerA);
var observerB = new ConcreteObserverB();
subject.Attach(observerB);
subject.SomeBusinessLogic();
subject.SomeBusinessLogic();
subject.Detach(observerB);
subject.SomeBusinessLogic();
}
}
public interface ISubject
{
void Attach(IObserver observer);
void Detach(IObserver observer);
void Notify();
}
public interface IObserver
{
void Update(ISubject subject);
}
public class Subject : ISubject
{
public int State { get; set; } = -0;
private List<IObserver> _observers = new List<IObserver>();
public void Attach(IObserver observer)
{
Console.WriteLine("Subject: Attached an observer.");
_observers.Add(observer);
}
public void Detach(IObserver observer)
{
Console.WriteLine("Subject: Detached an observer.");
_observers.Remove(observer);
}
public void Notify()
{
Console.WriteLine("Subject: Notifying observers...");
foreach (var item in _observers)
{
item.Update(this);
}
}
public void SomeBusinessLogic()
{
Console.WriteLine("\n Subject: I am doing something important.");
State = new Random().Next(0, 10);
Thread.Sleep(15);
Console.WriteLine("Subject: My state has just changed to: " + State);
Notify();
}
}
class ConcreteObserverA : IObserver
{
public void Update(ISubject subject)
{
if ((subject as Subject).State < 3)
{
Console.WriteLine("ConcreteObserverA: Reacted to the event.");
}
}
}
class ConcreteObserverB : IObserver
{
public void Update(ISubject subject)
{
if ((subject as Subject).State == 0 || (subject as Subject).State >= 2)
{
Console.WriteLine("ConcreteObserverB: Reacted to the event.");
}
}
}
状态模式
策略模式
模板方法模式
访问者模式
策略模式
策略模式:一个类的行为可以在运行时更改,策略模式将所有的行为一个个封装起来,让他们可以相互替换,而不影响调用者
使用场景
当一个功能在不同的场景下,需要不同的实现方式,类似这种情况就可以使用策略模式
优点
- 类的行为可以在运行时自由切换
- 降低了类的调用者和类的具体实现之间的耦合
- 避免了繁琐的条件判断,代码可读性更高
- 扩展性更好
缺点
- 策略类会增多