posted at 2023.4.9 09:50 by 风信子
Design Patterns 设计模式分类
Abstract Factory创建:如何应对这种变化?绕过常规的对象创建方法(new),提供一种"封装机制"来避免紧耦合。提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。封装变化点-----哪里变化,封装哪里,潜台词:如果没有变化,当然不需要额外的封装。
abstract class AbstractFactory{ public abstract AbstractProductA CreateProductA(); public abstract AbstractProductB CreateProductB();}abstract class AbstractProductA{ … … }abstract class AbstractProductB{ … … } class Client{ private AbstractProductA AbstractProductA; private AbstractProductB AbstractProductB; // Constructor public Client(AbstractFactory factory) { AbstractProductB = factory.CreateProductB(); AbstractProductA = factory.CreateProductA(); } public void Run(){ AbstractProductB.Interact(AbstractProductA); }} class Program{ static void Main(string[] args) { AbstractFactory factory1 = new ConcreteFactory1(); Client c1 = new Client(factory1); c1.Run(); Console.Read(); } }
Adapter结构:将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
interface IStack {void Push(object item);
}//对象适配器
public class Adapter : IStack //适配对象 {
ArrayList adaptee;//被适配的对象
public Adapter() { adaptee = new ArrayList(); } public void Push(object item) { adaptee.Add(item); }}//类适配器public class Adapter :ArrayList, IStack { public void Push(object item) { this.Add(item); }}
Bridge结构:将抽象部分与它的实现部分分离,使它们都可以独立地变化。类不使用继承,而是颗粒化。例如将蜡笔拆分为毛笔和颜色。
abstract class Brush { protected Color c; public abstract void Paint();
public void SetColor(Color c) { this.c = c; } }class BigBrush : Brush { public override void Paint() {Console.WriteLine("Using big brush and color {0} painting", c.color); }
}class Color { public string color;
} class Red : Color { public Red() { this.color = "red"; }
}class Program {
public static void Main() {
Brush b = new BigBrush();
b.SetColor(new Red());
b.Paint();}}
Builder创建:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
public abstract class Builder {
public abstract void BuildDoor();
public abstract void BuildWall();
public abstract House GetHouse();
} public class Director {
public void Construct(Builder builder)
{
builder.BuildWall();
builder.BuildDoor();
}
} public class ChineseBuilder:Builder {
... … }
Chain of Responsibility行为:为解除请求的发送者和接收者之间耦合,而使多个对象都有机会处理这个请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它。
abstract class Handler { protected Handler successor; public void SetSuccessor(Handler successor){ this.successor = successor; } public abstract void HandleRequest(int request); } class ConcreteHandler1 : Handler{ public override void HandleRequest(int request) { if (request >= 0 && request < 10) { Console.WriteLine("{0} 处理请求 {1}", this.GetType().Name, request); } else if (successor != null) { successor.HandleRequest(request); } } } class ConcreteHandler2 : Handler{ public override void HandleRequest(int request) { if (request >= 10 && request < 20) { Console.WriteLine("{0} 处理请求 {1}", this.GetType().Name, request); } else if (successor != null) { successor.HandleRequest(request); } } } class Program { static void Main(string[] args){ Handler h1 = new ConcreteHandler1(); Handler h2 = new ConcreteHandler2(); h1.SetSuccessor(h2); int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 }; foreach (int request in requests) { h1.HandleRequest(request); } Console.Read(); } }
Command行为:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可取消的操作。
class Program{ static void Main(string[] args) { Receiver r = new Receiver(); Command c = new ConcreteCommand(r); Invoker i = new Invoker(); // Set and execute command i.SetCommand(c); i.ExecuteCommand(); Console.Read(); } } abstract class Command{ protected Receiver receiver; public Command(Receiver receiver) { this.receiver = receiver; } abstract public void Execute(); } class ConcreteCommand : Command{ public ConcreteCommand(Receiver receiver) : base(receiver) { } public override void Execute(){ receiver.Action(); } } class Receiver{ public void Action(){ Console.WriteLine("执行请求!"); } } class Invoker { private Command command; public void SetCommand(Command command) { this.command = command; } public void ExecuteCommand(){ command.Execute(); } }
Composite结构:将对象组合成树形结构以表示“部分-整体”的层次结构。使得客户对单个对象和复合对象的使用具有一致性。
abstract class Company{ protected string name; public Company(string name) { this.name = name; } public abstract void Add(Company c);//增加 public abstract void Remove(Company c);//移除 public abstract void Display(int depth);//显示 public abstract void LineOfDuty();//履行职责 } class ConcreteCompany : Company { … …}
d9a68634-f597-49d1-b77c-a047b2180663|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags: 方法, 接口, 类
IT技术