本文共 4191 字,大约阅读时间需要 13 分钟。
索引
意图
预期对象的接口将在未来被扩展。通过额外的接口来定义扩展对象。
Anticipate that an object’s interface needs to be extended in the future.
Additional interfaces are defined by extension objects..
结构
参与者
Subject
Extension
ConcreteSubject
AbstractExtension
ConcreteExtension
适用性
当以下情况成立时可以使用 Extension Object 模式:
效果
相关模式
实现
实现方式(一):使用示例结构实现 Extension Object。
1 namespace ExtensionObjectPattern.Implementation1 2 { 3 public abstract class Subject 4 { 5 public abstract void Operation1(); 6 public abstract Extension GetExtension(string extensionType); 7 } 8 9 public abstract class Extension10 {11 protected Subject _owner;12 13 public Extension(Subject owner)14 {15 _owner = owner;16 }17 18 public abstract void DoSomething();19 }20 21 public abstract class AbstractExtension : Extension22 {23 public AbstractExtension(Subject owner)24 : base(owner)25 {26 }27 }28 29 public class ConcreteExtension : AbstractExtension30 {31 public ConcreteExtension(Subject owner)32 : base(owner)33 {34 }35 36 public override void DoSomething()37 {38 // do something39 _owner.Operation1();40 }41 }42 43 public class ConcreteSubject : Subject44 {45 private ConcreteExtension _extension;46 47 public ConcreteSubject()48 {49 _extension = new ConcreteExtension(this);50 }51 52 public override void Operation1()53 {54 // do something55 }56 57 public override Extension GetExtension(string extensionType)58 {59 if (extensionType == "some type")60 {61 return this._extension;62 }63 64 return null;65 }66 }67 68 public class Client69 {70 public void TestCase1()71 {72 Subject subject = new ConcreteSubject();73 Extension extension = subject.GetExtension("some type");74 extension.DoSomething();75 }76 }77 }
实现方式(二):使用泛型实现 IExtensibleObject<T> 接口。
1 namespace ExtensionObjectPattern.Implementation2 2 { 3 public abstract class Subject : IExtensibleObject4 { 5 public abstract void Operation1(); 6 public abstract IEnumerable > Extensions { get; } 7 } 8 9 public interface IExtensibleObject 10 where T : class, IExtensibleObject 11 {12 IEnumerable > Extensions { get; }13 }14 15 public interface IExtension 16 where T : class, IExtensibleObject 17 {18 void Attach(T owner);19 void Detach(T owner);20 }21 22 public abstract class Extension : IExtension 23 {24 protected List _owners;25 26 public void Attach(Subject owner)27 {28 _owners.Add(owner);29 }30 31 public void Detach(Subject owner)32 {33 _owners.Remove(owner);34 }35 36 public abstract void DoSomething();37 }38 39 public class ConcreteExtension : Extension40 {41 public override void DoSomething()42 {43 // do something44 foreach (var item in _owners)45 {46 item.Operation1();47 }48 }49 }50 51 public class ConcreteSubject : Subject52 {53 private List _extensions = new List ();54 55 public ConcreteSubject()56 {57 Extension extension = new ConcreteExtension();58 extension.Attach(this);59 60 _extensions.Add(extension);61 }62 63 public override void Operation1()64 {65 // do something66 }67 68 public override IEnumerable > Extensions69 {70 get71 {72 return _extensions;73 }74 }75 }76 77 public class Client78 {79 public void TestCase1()80 {81 Subject subject = new ConcreteSubject();82 83 foreach (var extension in subject.Extensions)84 {85 (extension as Extension).DoSomething();86 }87 }88 }89 }
转载地址:http://bzosx.baihongyu.com/