posted at 2023.7.2 12:15 by 风信子
Prototype创建:用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。
public abstract class FlyActor{
public abstract FlyActor clone();
}
public class FlyActorB:FlyActor{
public override FlyActor clone(){
return (FlyActor)this.MemberwiseClone();
}
}
Proxy结构:为其他对象提供一个代理以控制对这个对象的访问。分为:远程(Remote)代理、虚拟(Virtual)代理、保护(Protect or Access)代理、Cache代理、防火墙(Firewall)代理等。
abstract class Subject{ public abstract void Request(); }classRealSubject : Subject {public override void Request(){ Console.WriteLine("真实的请求"); } }class Proxy : Subject { }class Program{static void Main(string[] args) { Proxy proxy = new Proxy();proxy.Request();Console.Read(); } }
Singleton创建:保证一个类仅有一个实例,并提供一个访问它的全局访问点。有单线程、多线程、静态单例。
class Singleton{private static Singleton instance;private static readonly object syncRoot = new object();private Singleton(){ }public static Singleton GetInstance(){if (instance == null) {lock (syncRoot) {if (instance == null) { instance = new Singleton(); } } } return instance; } }
State行为:主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同状态的一系列的类当中,使复杂的判断逻辑简单化。允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它所属的类。
class Context{private State state;public Context(State state) {this.state = state; }public State State{get{return state; }set{state = value;Console.WriteLine("当前状态:" + state.GetType().Name); } }public void Request(){state.Handle(this); } }abstract class State{ public abstract void Handle(Context context); }classConcreteStateA : State{public override void Handle(Context context) {context.State = new ConcreteStateB(); } }classConcreteStateB : State{public override void Handle(Context context) {context.State = new ConcreteStateA(); }}class Program{static void Main(string[] args) { Context c = newContext(new ConcreteStateA());c.Request();c.Request();c.Request();c.Request();Console.Read(); } }
Strategy行为:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法的变化可独立于使用它的客户。
//抽象算法类abstract class Strategy{ //算法方法 public abstract void AlgorithmInterface(); } //具体算法AclassConcreteStrategyA : Strategy { //算法A实现方法public override void AlgorithmInterface(){Console.WriteLine("算法A实现"); } } //具体算法BclassConcreteStrategyB : Strategy{ //算法B实现方法public override void AlgorithmInterface(){Console.WriteLine("算法B实现"); } } //具体算法CclassConcreteStrategyC : Strategy{ //算法C实现方法public override void AlgorithmInterface(){Console.WriteLine("算法C实现"); } } //上下文class Context{ Strategy strategy;public Context(Strategy strategy) {this.strategy = strategy; } //上下文接口public void ContextInterface(){strategy.AlgorithmInterface(); } }class Program{static void Main(string[] args) { Context context;context = new Context(new ConcreteStrategyA());context.ContextInterface();context = new Context(new ConcreteStrategyB());context.ContextInterface();context = new Context(new ConcreteStrategyC());context.ContextInterface();Console.Read(); } }
Template method行为:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
abstract class AbstractClass{public abstract void PrimitiveOperation1();public abstract void PrimitiveOperation2();public void TemplateMethod(){PrimitiveOperation1();PrimitiveOperation2();Console.WriteLine("");} } classConcreteClassA : AbstractClass{public override void PrimitiveOperation1(){ Console.WriteLine("具体类A方法1实现"); }public override void PrimitiveOperation2(){ Console.WriteLine("具体类A方法2实现"); } }
Visitor行为:在软件构建过程中,由于需求的改变,某些类层次结构中常常需要增加新的行为(方法),如果直接在基类中做这样的更改,将会给子类带来很繁重的变更负担,甚至破坏原有设计。表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
class Program{static void Main(string[] args) {ObjectStructure o = new ObjectStructure();o.Attach(new ConcreteElementA());o.Attach(new ConcreteElementB()); ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2();o.Accept(v1);o.Accept(v2);Console.Read(); } }abstract class Visitor{public abstract void VisitConcreteElementA(ConcreteElementAconcreteElementA);public abstract void VisitConcreteElementB(ConcreteElementBconcreteElementB); }class ConcreteVisitor1 : Visitor{public override void VisitConcreteElementA(ConcreteElementAconcreteElementA) {Console.WriteLine("{0}被{1}访问", concreteElementA.GetType().Name, this.GetType().Name); }public override void VisitConcreteElementB(ConcreteElementBconcreteElementB) { Console.WriteLine("{0}被{1}访问", concreteElementB.GetType().Name, this.GetType().Name); } }class ConcreteVisitor2 : Visitor {public override void VisitConcreteElementA(ConcreteElementAconcreteElementA) {Console.WriteLine("{0}被{1}访问", concreteElementA.GetType().Name, this.GetType().Name); }public override void VisitConcreteElementB(ConcreteElementBconcreteElementB) {Console.WriteLine("{0}被{1}访问", concreteElementB.GetType().Name, this.GetType().Name); } }abstract class Element{ public abstract void Accept(Visitor visitor); }classConcreteElementA : Element{public override void Accept(Visitor visitor) {visitor.VisitConcreteElementA(this); }public void OperationA(){ } }classConcreteElementB : Element{public override void Accept(Visitor visitor) {visitor.VisitConcreteElementB(this); }public void OperationB(){ } }classObjectStructure{privateIList<Element>elements = new List<Element>();public void Attach(Element element) { elements.Add(element); }public void Detach(Element element) {elements.Remove(element); }public void Accept(Visitor visitor) {foreach (Element e in elements) {e.Accept(visitor); } }}
cfb4bc2d-fd10-4da5-99ce-cad9eba74487|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags: 车, 方法, 类
IT技术