[置顶]c# 设计模式(2)结构型
名称 |
Adapter |
结构 |
|
意图 |
将一个类的接口转换成客户希望的另外一个接口。A d a p t e r 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 |
适用性 |
|
Code Example |
namespace Adapter_DesignPattern { using System; class FrameworkXTarget { virtual public void SomeRequest(int x) { // normal implementation of SomeRequest goes here } } class FrameworkYAdaptee { public void QuiteADifferentRequest(string str) { Console.WriteLine("QuiteADifferentRequest = {0}", str); } } class OurAdapter : FrameworkXTarget { private FrameworkYAdaptee adaptee = new FrameworkYAdaptee(); override public void SomeRequest(int a) { string b; b = a.ToString(); adaptee.QuiteADifferentRequest(b); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { void GenericClientCode(FrameworkXTarget x) { // We assume this function contains client-side code that only // knows about FrameworkXTarget. x.SomeRequest(4); // other calls to FrameworkX go here // ... } public static int Main(string[] args) { Client c = new Client(); FrameworkXTarget x = new OurAdapter(); c.GenericClientCode(x); return 0; } } } |
名称 |
Bridge |
结构 |
|
意图 |
将抽象部分与它的实现部分分离,使它们都可以独立地变化。 |
适用性 |
|
Code Example |
namespace Bridge_DesignPattern { using System; class Abstraction { protected Implementation impToUse; public void SetImplementation(Implementation i) { impToUse = i; } virtual public void DumpString(string str) { impToUse.DoStringOp(str); } } class DerivedAbstraction_One : Abstraction { override public void DumpString(string str) { str += ".com"; impToUse.DoStringOp(str); } } class Implementation { public virtual void DoStringOp(string str) { Console.WriteLine("Standard implementation - print string as is"); Console.WriteLine("string = {0}", str); } } class DerivedImplementation_One : Implementation { override public void DoStringOp(string str) { Console.WriteLine("DerivedImplementation_One - don't print string"); } } class DerivedImplementation_Two : Implementation { override public void DoStringOp(string str) { Console.WriteLine("DerivedImplementation_Two - print string twice"); Console.WriteLine("string = {0}", str); Console.WriteLine("string = {0}", str); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { Abstraction SetupMyParticularAbstraction() { // we localize to this method the decision which abstraction and // which implementation to use. These need to be decided // somewhere and we do it here. All teh rest of the client // code can work against the abstraction object. Abstraction a = new DerivedAbstraction_One(); a.SetImplementation(new DerivedImplementation_Two()); return a; } public static int Main(string[] args) { Client c = new Client(); Abstraction a = c.SetupMyParticularAbstraction(); // From here on client code thinks it is talking to the // abstraction, and will not need to be changed as // derived abstractions are changed. // more client code using the abstraction goes here // . . . a.DumpString("Clipcode"); return 0; } } } |
名称 |
Composite |
结构 |
|
意图 |
将对象组合成树形结构以表示“部分-整体”的层次结构。C o m p o s i t e 使得用户对单个对象和组合对象的使用具有一致性。 |
适用性 |
|
Code Example |
namespace FactoryMethod_DesignPattern { using System; using System.Collections; abstract class Component { protected string strName; public Component(string name) { strName = name; } abstract public void Add(Component c); public abstract void DumpContents(); // other operations for delete, get, etc. } class Composite : Component { private ArrayList ComponentList = new ArrayList(); public Composite(string s) : base(s) {} override public void Add(Component c) { ComponentList.Add(c); } public override void DumpContents() { // First dump the name of this composite node Console.WriteLine("Node: {0}", strName); // Then loop through children, and get then to dump their contents foreach (Component c in ComponentList) { c.DumpContents(); } } } class Leaf : Component { public Leaf(string s) : base(s) {} override public void Add(Component c) { Console.WriteLine("Cannot add to a leaf"); } public override void DumpContents() { Console.WriteLine("Node: {0}", strName); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { Component SetupTree() { // here we have to create a tree structure, // consisting of composites and leafs. Composite root = new Composite("root-composite"); Composite parentcomposite; Composite composite; Leaf leaf; parentcomposite = root; composite = new Composite("first level - first sibling - composite"); parentcomposite.Add(composite); leaf = new Leaf("first level - second sibling - leaf"); parentcomposite.Add(leaf); parentcomposite = composite; composite = new Composite("second level - first sibling - composite"); parentcomposite.Add(composite); composite = new Composite("second level - second sibling - composite"); parentcomposite.Add(composite); // we will leaf the second level - first sibling empty, and start // populating the second level - second sibling parentcomposite = composite; leaf = new Leaf("third level - first sibling - leaf"); parentcomposite.Add(leaf); leaf = new Leaf("third level - second sibling - leaf"); parentcomposite.Add(leaf); composite = new Composite("third level - third sibling - composite"); parentcomposite.Add(composite); return root; } public static int Main(string[] args) { Component component; Client c = new Client(); component = c.SetupTree(); component.DumpContents(); return 0; } } } |
名称 |
Decorator |
结构 |
|
意图 |
动态地给一个对象添加一些额外的职责。就增加功能来说,D e c o r a t o r 模式相比生成子类更为灵活。 |
适用性 |
|
Code Example |
namespace Decorator_DesignPattern { using System; abstract class Component { public abstract void Draw(); } class ConcreteComponent : Component { private string strName; public ConcreteComponent(string s) { strName = s; } public override void Draw() { Console.WriteLine("ConcreteComponent - {0}", strName); } } abstract class Decorator : Component { protected Component ActualComponent; public void SetComponent(Component c) { ActualComponent = c; } public override void Draw() { if (ActualComponent != null) ActualComponent.Draw(); } } class ConcreteDecorator : Decorator { private string strDecoratorName; public ConcreteDecorator (string str) { // how decoration occurs is localized inside this decorator // For this demo, we simply print a decorator name strDecoratorName = str; } public override void Draw() { CustomDecoration(); base.Draw(); } void CustomDecoration() { Console.WriteLine("In ConcreteDecorator: decoration goes here"); Console.WriteLine("{0}", strDecoratorName); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { Component Setup() { ConcreteComponent c = new ConcreteComponent("This is the real component"); ConcreteDecorator d = new ConcreteDecorator("This is a decorator for the component"); d.SetComponent(c); return d; } public static int Main(string[] args) { Client client = new Client(); Component c = client.Setup(); // The code below will work equally well with the real component, // or a decorator for the component c.Draw(); return 0; } } } |
名称 |
Facade |
结构 |
|
意图 |
为子系统中的一组接口提供一个一致的界面,F a c a d e 模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。 |
适用性 |
|
Code Example |
namespace Facade_DesignPattern { using System; class SubSystem_class1 { public void OperationX() { Console.WriteLine("SubSystem_class1.OperationX called"); } } class SubSystem_class2 { public void OperationY() { Console.WriteLine("SubSystem_class2.OperationY called"); } } class SubSystem_class3 { public void OperationZ() { Console.WriteLine("SubSystem_class3.OperationZ called"); } } class Facade { private SubSystem_class1 c1 = new SubSystem_class1(); private SubSystem_class2 c2 = new SubSystem_class2(); private SubSystem_class3 c3 = new SubSystem_class3(); public void OperationWrapper() { Console.WriteLine("The Facade OperationWrapper carries out complex decision-making"); Console.WriteLine("which in turn results in calls to the subsystem classes"); c1.OperationX(); if (1==1 /*some really complex decision*/) { c2.OperationY(); } // lots of complex code here . . . c3.OperationZ(); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { public static int Main(string[] args) { Facade facade = new Facade(); Console.WriteLine("Client calls the Facade OperationWrapper"); facade.OperationWrapper(); return 0; } } } |
名称 |
Flyweight |
结构 |
|
意图 |
运用共享技术有效地支持大量细粒度的对象。 |
适用性 |
|
Code Example |
namespace Flyweight_DesignPattern { using System; using System.Collections; class FlyweightFactory { private ArrayList pool = new ArrayList(); // the flyweightfactory can crete all entries in the pool at startup // (if the pool is small, and it is likely all will be used), or as // needed, if the pool si large and it is likely some will never be used public FlyweightFactory() { pool.Add(new ConcreteEvenFlyweight()); pool.Add(new ConcreteUnevenFlyweight()); } public Flyweight GetFlyweight(int key) { // here we would determine if the flyweight identified by key // exists, and if so return it. If not, we would create it. // As in this demo we have implementation all the possible // flyweights we wish to use, we retrun the suitable one. int i = key % 2; return((Flyweight)pool[i]); } } abstract class Flyweight { abstract public void DoOperation(int extrinsicState); } class UnsharedConcreteFlyweight : Flyweight { override public void DoOperation(int extrinsicState) { } } class ConcreteEvenFlyweight : Flyweight { override public void DoOperation(int extrinsicState) { Console.WriteLine("In ConcreteEvenFlyweight.DoOperation: {0}", extrinsicState); } } class ConcreteUnevenFlyweight : Flyweight { override public void DoOperation(int extrinsicState) { Console.WriteLine("In ConcreteUnevenFlyweight.DoOperation: {0}", extrinsicState); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { public static int Main(string[] args) { int[] data = {1,2,3,4,5,6,7,8}; FlyweightFactory f = new FlyweightFactory(); int extrinsicState = 3; foreach (int i in data) { Flyweight flyweight = f.GetFlyweight(i); flyweight.DoOperation(extrinsicState); } return 0; } } } |
名称 |
Proxy |
结构 |
|
意图 |
为其他对象提供一种代理以控制对这个对象的访问。 |
适用性 |
|
Code Example |
namespace Proxy_DesignPattern { using System; using System.Threading; /// <summary> /// Summary description for Client. /// </summary> abstract class CommonSubject { abstract public void Request(); } class ActualSubject : CommonSubject { public ActualSubject() { // Assume constructor here does some operation that takes quite a // while - hence the need for a proxy - to delay incurring this // delay until (and if) the actual subject is needed Console.WriteLine("Starting to construct ActualSubject"); Thread.Sleep(1000); // represents lots of processing! Console.WriteLine("Finished constructing ActualSubject"); } override public void Request() { Console.WriteLine("Executing request in ActualSubject"); } } class Proxy : CommonSubject { ActualSubject actualSubject; override public void Request() { if (actualSubject == null) actualSubject = new ActualSubject(); actualSubject.Request(); } } public class Client { public static int Main(string[] args) { Proxy p = new Proxy(); // Perform actions here // . . . if (1==1) // at some later point, based on a condition, p.Request();// we determine if we need to use subject return 0; } } } |
转载于:https://www.cnblogs.com/wangzhanjianshe/archive/2011/07/14/2326413.html
[置顶]c# 设计模式(2)结构型相关推荐
- 设计模式 之 结构型模式
设计模式 之 结构型模式 模式 & 描述 包括 结构型模式 这些设计模式关注类和对象的组合.继承的概念被用来组合接口和定义组合对象获得新功能的方式. 适配器模式(Adapter Pattern ...
- Java设计模式之结构型:享元模式
一.什么是享元模式: 享元模式通过共享技术有效地支持细粒度.状态变化小的对象复用,当系统中存在有多个相同的对象,那么只共享一份,不必每个都去实例化一个对象,极大地减少系统中对象的数量.比如说一个文本系 ...
- Java设计模式之结构型:组合模式
前言: 我们对于上面两幅图片肯定非常熟悉,这两幅图片我们都可以看做是一个文件结构,对于这样的结构我们称之为树形结构.在数据结构中我们知道可以通过调用某个方法来遍历整个树,当我们找到某个叶子节点后,就可 ...
- Java设计模式之结构型:外观模式
一.什么是外观模式: 外观模式通过对客户端提供一个统一的接口,用于访问子系统中的一群接口.使用外观模式有以下几点好处: (1)更加易用:使得子系统更加易用,客户端不再需要了解子系统内部的实现,也不需要 ...
- Java设计模式之结构型:代理模式
前言: 我们一般在租房子时会去找中介,为什么呢?因为你对该地区房屋的信息掌握的不够全面,希望找一个更熟悉的人去帮你做:再比如我们打官司需要请律师,因为律师在法律方面有专长,可以替我们进行操作,表达我们 ...
- Java设计模式之结构型:装饰器模式
一.什么是装饰器模式: 当需要对类的功能进行拓展时,一般可以使用继承,但如果需要拓展的功能种类很繁多,那势必会生成很多子类,增加系统的复杂性,并且使用继承实现功能拓展时,我们必须能够预见这些拓展功能, ...
- Java设计模式之结构型:适配器模式
一.什么是适配器模式: 适配器模式主要用于将一个类的接口转化成客户端希望的目标类格式,使得原本不兼容的类可以在一起工作,将目标类和适配者类解耦:同时也符合"开闭原则",可以在不修改 ...
- 备战面试日记(3.3) - (设计模式.23种设计模式之结构型模式)
本人本科毕业,21届毕业生,一年工作经验,简历专业技能如下,现根据简历,并根据所学知识复习准备面试. 记录日期:2022.1.9 大部分知识点只做大致介绍,具体内容根据推荐博文链接进行详细复习. 文章 ...
- 设计模式之结构型模型
设计模式之结构型模型 桥接模式 尽可能不要使用类的继承,而尽可能使用 合成/聚合 描述: 继承方法子类与父类的高依赖性限制了复用和程序的灵活性. 选择不同的接口实现选择不同的业务类型 import o ...
- 组合模式(Bridge Pattern) – 设计模式之结构型模式
组合模式(Bridge Pattern) – 设计模式之结构型模式: 目录 组合模式(Component Pattern) 类图 例子1: 过程: 类图: 代码: 抽象组件:PlayerComposi ...
最新文章
- oracle+测试权限,Oracle测试题
- 【Python】30天进阶Python!这个Github项目你值得拥有!
- Kruskal实现最小生成树
- 装机之MBR和GPT
- bootstrap table传回的数据后端怎么获取_基于 Spring Cloud 开发的分布式系统,遇到爬虫、接口盗刷怎么办
- oss图片尺寸调用方式_CDN百科11 | 如何用CDN加速OSS源站资源
- POJ3421:X-factor Chains——题解
- 【数字信号调制】基于matlab GUI数字信号调制仿真平台【含Matlab源码 880期】
- 常数除以0的极限是什么_【高数总结求极限方法】百度作业帮
- 安装office2013报安装程序找不到OneNote.zh-cn、安装源不存在
- CVPR 2019 论文汇总(按方向进行论文划分)
- 【Rust日报】 2019-04-27
- 数字版权资源价值日益凸显
- 计算机网络概述 第二部分
- 【转载】基于Office Online Server 2016 的office在线编辑
- JAVA SE面试题(全)
- Unity3d Ugui 15 TextMeshPro
- 谷歌出品开发必备开源图片压缩工具,终于知道图片压缩了啥
- synergy官方linux使用教程,使用 Synergy 来进行协同工作
- 前端知识点汇总、深入浅出
热门文章
- 报文交换(串行)和分组交换(并行)
- 排序算法的总结与使用题型
- bzoj 3392: [Usaco2005 Feb]Part Acquisition 交易(最短路)
- Codecraft-18 and Codeforces Round #458: C. Travelling Salesman and Sp(组合数)
- bzoj 1639: [Usaco2007 Mar]Monthly Expense 月度开支(二分)
- HDU 6170 2017 多校训练:Two strings(DP)
- java读取、生成图片
- 【node】node连接mongodb操作数据库
- 解决请求筛选模块被配置为拒绝包含的查询字符串过长的请求
- swift可选隐式可选类型