c#设计模式·结构型模式

看的过程中,发现好多模式都用过,只是没有总结,或者是不知道叫这个名字吧···

这里列举结构型模式,适配器、桥接、过滤、组合、装饰器、外观、享元、代理,

适配器模式:将现存的对象放到新的环境里边去,但是接口不一样,其实就是添加一个类把新的接口包装一样

之前公司的wcf服务端就是这种模式,公司很多部门,不同部门不同的业务都有自己相应的服务,之前是分开的,用的服务多的时候开服务很麻烦,现在想把他们统一起来,就可以用这种方式,wcf服务以接口定义契约,在实现类中写具体业务,可以定义一个统一的空接口,然所有的wcf接口都继承该空接口,然后统一后的类通过适配器构造相应的服务对象,然后加载,适配器里边可以通过统一的空接口反射获取对象,也可以直接通过不同服务的程序集名及类名获取对象,这样连统一接口都不用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExercisePrj.Dsignmode
{
    //统一接口
    public interface IMediaPlayer
    {
         void Play(string audioType, string filename);
    }
    //播放接口
    public interface IAdvanceMediaPlayer
    {
        void PlayVlc(string filename);
        void PlayMp4(string filename);
    }
    public class VlcPlayer :IAdvanceMediaPlayer
    {
        public void PlayVlc(string filename)
        {
            Console.WriteLine("play vlc");
        }
        public void PlayMp4(string filename)
        {

        }
    }
    public class Mp4Player:IAdvanceMediaPlayer
    {
        public void PlayVlc(string filename)
        {

        }
        public void PlayMp4(string filename)
        {
            Console.WriteLine("play mp4");
        }
    }
    //适配器类
    public class MediaAdapter : IMediaPlayer
    {
        IAdvanceMediaPlayer advancedMusicPlayer;
       public MediaAdapter(String audioType)
        {
            if (audioType=="vlc")
            {
                advancedMusicPlayer = new VlcPlayer();
            }
            else if (audioType=="mp4")
            {
                advancedMusicPlayer = new Mp4Player();
            }
        }

       public void Play(String audioType, String fileName)
        {
            if (audioType=="vlc")
            {
                advancedMusicPlayer.PlayVlc(fileName);
            }
            else if (audioType=="mp4")
            {
                advancedMusicPlayer.PlayMp4(fileName);
            }
        }
    }
    //实体类
    public class AutoPaly : IMediaPlayer
    {
        MediaAdapter mediaAdapter;
        public void Play(String audioType, String fileName)
        {
            if (audioType == "mp3")
            {
                Console.WriteLine("play mp3");
            }
            else if (audioType == "vlc" || audioType == "mp4")
            {
                mediaAdapter = new MediaAdapter(audioType);
                mediaAdapter.Play(audioType, fileName);
            }
            else
            {
                Console.WriteLine("invalid mediatype");
            }
        }
    }
}

桥接模式:将抽象部分和实现部分分离,使他们可以独立变化,就是吧具体操作再抽象成接口,然后实现该接口,通过关联关系吧操作和实体结合,构造实体的时候根据情况构造对应的操作的实现类,传给实体

这玩意对抽象的设计要求有点高,一不小心就得跪

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExercisePrj.Dsignmode
{
    //桥接接口
    public interface IDrawAPI
    {
        void DrawCircle(int radius, int x, int y);
    }
    //具体实现
    public class GreenCircle:IDrawAPI
    {
        public void DrawCircle(int radius,int x,int y)
        {
            Console.WriteLine("draw green circle");
        }
    }
    public class RedCircle : IDrawAPI
    {
        public void DrawCircle(int radius, int x, int y)
        {
            Console.WriteLine("draw red circle,x{0},y{1}",x,y);
        }
    }
    //实体抽象基类
    public abstract class ShapeEx
    {
        protected IDrawAPI drawAPI;
        protected ShapeEx(IDrawAPI drawAPI)
        {
            this.drawAPI = drawAPI;
        }
        public abstract void Draw();
    }
    //继承实体实现类
    public class CircleEx : ShapeEx
    {
        public int x { get; set; }
        public int y { get; set; }
        public int radius { get; set; }
        private string color;
        //演示实现享元模式的构造函数
        public CircleEx(string color):base(null)
        {
            this.color = color;
            drawAPI = new RedCircle();
        }
        public CircleEx(int x, int y, int radius,IDrawAPI drawapi ):base(drawapi)
        {
            this.x = x;
            this.y = y;
            this.radius = radius;
        }

        public override void Draw()
        {
            drawAPI.DrawCircle(radius, x, y);
        }
    } 

}

过滤器模式:允许开发人员使用不同的标准过滤一组对象,通过逻辑运算以解耦的方式把他们连接起来,讲道理没看懂这模式有啥卵用,就是整了个过滤接口·然后用不同的实现继承该接口提一堆对象出来···这提了干啥··

这例子里边的直接用C#的linq扩展方法简单几句话就可以实现额。。。搞不懂··

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExercisePrj.Dsignmode
{
    //实体
    public class Person
    {
        public string Name { get; }
        public string Gender { get; }
        public string MaritalStatus { get; }
        public Person(string name,string gender,string maritalstatus)
        {
            Name = name;
            Gender = gender;
            MaritalStatus = maritalstatus;
        }
    }
    //过滤标准接口
    public interface ICriteria
    {
        List<Person> MeetCriteria(List<Person> persons);
    }
    //不同标准实现
    public class CriteriaMale:ICriteria
    {
        public List<Person> MeetCriteria(List<Person> persons)
        {
            List<Person> maleCriterial = new List<Person>();
            foreach(var p in persons)
            {
                if(p.Gender=="male")
                {
                    maleCriterial.Add(p);
                }
            }
            return maleCriterial;
        }
    }
    public class CriteriaFemale : ICriteria
    {
        public List<Person> MeetCriteria(List<Person> persons)
        {
            List<Person> femaleCriterial = new List<Person>();
            foreach (var p in persons)
            {
                if (p.Gender == "female")
                {
                    femaleCriterial.Add(p);
                }
            }
            return femaleCriterial;
        }
    }
    public class CriteriaSingle : ICriteria
    {
        public List<Person> MeetCriteria(List<Person> persons)
        {
            List<Person> Criterialsingle = new List<Person>();
            foreach (var p in persons)
            {
                if (p.MaritalStatus ==  "Single")
                {
                    Criterialsingle.Add(p);
                }
            }
            return Criterialsingle;
        }
    }
    public class AndCriteria : ICriteria
    {
        private ICriteria criteria;
        private ICriteria otherCriteria;

        public AndCriteria(ICriteria criteria, ICriteria otherCriteria)
        {
            this.criteria = criteria;
            this.otherCriteria = otherCriteria;
        }
        public List<Person> MeetCriteria(List<Person> persons)
        {
            List<Person> firstCriteriaPersons = criteria.MeetCriteria(persons);
            return otherCriteria.MeetCriteria(firstCriteriaPersons);
        }
    }

    public class OrCriteria :ICriteria
    {
       private ICriteria criteria;
        private ICriteria otherCriteria;

        public OrCriteria(ICriteria criteria, ICriteria otherCriteria)
        {
            this.criteria = criteria;
            this.otherCriteria = otherCriteria;
        }

       public List<Person> MeetCriteria(List<Person> persons)
        {
            List<Person> firstCriteriaItems = criteria.MeetCriteria(persons);
            List<Person> otherCriteriaItems = otherCriteria.MeetCriteria(persons);
            foreach (Person p  in  otherCriteriaItems)
            {
                if (!firstCriteriaItems.Contains(p))
                {
                    firstCriteriaItems.Add(p);
                }
            }
            return firstCriteriaItems;
        }
    }
}

组合模式:又叫部分整体模式,有些实体存在可能的树形结构,就用这种模式,这个其实用的比较多,比如用仪器测数据,有些仪器一次就一个仪器就可以了,有些仪器可能需要几个仪器组合起来才能测,还有公司里的雇员结构也是这样的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExercisePrj.Dsignmode
{
    public class Employee
    {
        private String name;
        private String dept;
        private int salary;
        private List<Employee> subordinates;
        public Employee(String name, String dept, int sal)
        {
            this.name = name;
            this.dept = dept;
            this.salary = sal;
            subordinates = new List<Employee>();
        }

        public void add(Employee e)
        {
            subordinates.Add(e);
        }

        public void remove(Employee e)
        {
            subordinates.Remove(e);
        }

        public List<Employee> getSubordinates()
        {
            return subordinates;
        }

        public override string  ToString()
        {
            return ("Employee :[ Name : " + name+ ", dept : " + dept + ", salary :" + salary + " ]");
        }
    }
}

装饰器模式,向现有的对象添加新的功能,不改变原有结构,这玩意就是C#的扩展方法的应用场景额。。。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExercisePrj.Dsignmode
{
    //装饰抽象类
    public abstract class ShapeDecorator : IShape
    {
       protected IShape decoratedShape;

        public ShapeDecorator(IShape decoratedShape)
        {
            this.decoratedShape = decoratedShape;
        }

        public virtual void Draw()
        {
            decoratedShape.Draw();
        }
    }
    //装饰实现类
    public class RedShapeDecorator : ShapeDecorator
    {

       public RedShapeDecorator(IShape decoratedShape):base(decoratedShape)
        {

        }
       public override void Draw()
        {
            decoratedShape.Draw();
            setRedBorder(decoratedShape);
        }

        private void setRedBorder(IShape decoratedShape)
        {
            Console.WriteLine("Border Color: Red");
        }
    }
    //用扩展方法的方式直接实现
    public static class ShapeExpend
    {
        public static void SetColor(this IShape shape)
        {
            Console.WriteLine("border Color: Red");
        }
    }

}

外观模式:就是用个类把一些复杂的功能包装一下··很多地方都有这个的体现··mvc传统的三层架构都是这玩意的体现···

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExercisePrj.Dsignmode
{
    //外观类
    public class ShapeMaker
    {
        private IShape circle;
        private IShape line;

        public ShapeMaker()
        {
            circle = new Circle();
            line = new Line();
        }

        public void drawCircle()
        {
            circle.Draw();
        }
        public void drawLine()
        {
            line.Draw();
        }
    }
}

享元模式:用于减少创建对象,减少内存和提高性能,就是缓存用过的对象,下次要用就在里边找,没有就新建添加··,没用过···,可能是没做过大型的项目···

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExercisePrj.Dsignmode
{
    //享元模式的工厂类
    public class ShapeFlyweight
    {
        private static readonly Dictionary<string, ShapeEx> circleMap = new Dictionary<string, ShapeEx>();

        public static ShapeEx getCircle(string color)
        {
            CircleEx circle;

            if (!circleMap.Keys.Contains(color))
            {
                circle = new CircleEx(color);
                circleMap.Add(color, circle);
                Console.WriteLine("Creating circle of color : " + color);
                return circle;
            }
            else
            {
                circle=(CircleEx)circleMap[color];
            }
            return circle;
        }
    }
}

代理模式:相当于在类外边加壳,各种代理都是该模式的体现···主要是为了控制外界对类的操作吧。

博客园这个添加代码窗体里边的那个提交按钮没有做多次点击处理···点快点可以提交几次·····

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExercisePrj.Dsignmode
{
    public interface IImage
    {
        void Display();
    }
    public class RealImage:IImage
    {
        private string filename;
        public RealImage(string filename)
        {
            this.filename = filename;
            loadfile(filename);
        }
        public void Display()
        {
            Console.WriteLine("display:"+filename);
        }
        private void loadfile(string filename)
        {
            Console.WriteLine("loadfile");
        }
    }
    //代理类
    public class ProxyImage : IImage
    {

       private RealImage realImage;
        private String fileName;

        public ProxyImage(String fileName)
        {
            this.fileName = fileName;
        }

       public void Display()
        {
            if (realImage == null)
            {
                realImage = new RealImage(fileName);
            }
            realImage.Display();
        }
    }
}
时间: 2024-08-06 20:08:43

c#设计模式·结构型模式的相关文章

设计模式——结构型模式

设计模式的另一大类型为结构型.共收录了7个模式,分别为适配器模式.桥接模式.组合模式.装饰模式.外观模式.享元模式.代理模式.下面从特点和使用两方面小探,欢迎交流!      适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.[大话设计模式]        特点:需要交流的两方,在数据和行为都匹配,但接口不符时,我们应该考虑用适配器,目的是促使交流和复用.可以理解为入乡随俗,相同的意思,不同的表达

设计模式-结构型模式,适配器模式(4)

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能.举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器.您将内存卡插入读卡器,再将读卡器插入笔记本,这样就可以通过笔记本来读取内存卡. #!/usr/bin/env python # encoding: utf-8 class Target(object): def request(self

设计模式-结构型模式,python组合模式

设计模式上大的方向上分继承和组合,就是类模式和对象模式.此篇的组合模式非继承和组合概念中的组合.桥接 策略 代理 装饰者都用了组合,此组合非彼组合. 组合模式 组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象.组合模式依据树形结构来组合对象,用来表示部分以及整体层次.这种类型的设计模式属于结构型模式,它创建了对象组的树形结构. 这种模式创建了一个包含自己对象组的类.该类提供了修改相同对象组的方式. 我们通过下面的实例来演示组合模式的用法.

看透设计模式-结构型模式

这里我们主要讨论 结构型模式 适配器模式,: ● Target(目标抽象类):目标抽象类定义客户所需接口,可以是一个抽象类或接口,也可以是具体类. ● Adapter(适配器类):适配器可以调用另一个接口,作为一个转换器,对Adaptee和Target进行适配,适配器类是适配器模式的核心,在对象适配器中,它通过继承Target并关联一个Adaptee对象使二者产生联系. ● Adaptee(适配者类):适配者即被适配的角色,它定义了一个已经存在的接口,这个接口需要适配,适配者类一般是一个具体类,

设计模式 结构型模式

定义 定义 结构型模式,讨论的是如何将类和对象组合,形成更为复杂的结构.采用继承机制来组合接口或实现(类结构型模式),或者通过组合一些对象,从而实现新的功能(对象结构型模式). 类和对象构成更复杂的结构就像搭积木的过程,通过不同组合,形成不同形状. 主导思想 1.组合类和对象以形成更复杂的结构. 2.尽量使用关联关系来代替继承关系. 分类 结构型模式又分为对象结构型模式和类结构型模式. 对象结构型模式把多个类组合成一个更复杂的系统,在类结构模式中一般只存在继承和实现关系. 类结构型模式通过关联.

设计模式——结构型模式之适配器模式(二)

模式的定义与特点 适配器模式(Adapter)的定义如下:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作.适配器模式分为类结构型模式和对象结构型模式两种,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些. 该模式的主要优点如下. 客户端通过适配器可以透明地调用目标接口. 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类. 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题.

JDK 源码 阅读 - 3 - 设计模式 - 结构型模式

1.适配器(Adapter) java.util.Arrays$ArrayList(java.util.Arrays#asList()):  这里要特别注意,这里的ArrayList是Arrays的内部类不是java.util.ArrayList.这里的ArrayList是外部不可见的,用来把一个array适配成List.   Sample: String a[] = new String[]{"123","abc","xyz","dd

设计模式--结构型模式--装饰模式

装饰者模式: 我们可以通过继承和组合的方式来给一个对象添加行为,虽然使用继承能够很好拥有父类的行为,但是它存在几个缺陷: 一.对象之间的关系复杂的话,系统变得复杂不利于维护. 二.容易产生“类爆炸”现象. 三.是静态的.在这里我们可以通过使用装饰者模式来解决这个问题. 装饰者模式,动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更加有弹性的替代方案.虽然装饰者模式能够动态将责任附加到对象上,但是他会产生许多的细小对象,增加了系统的复杂度. uml类图如下: 装饰者Decorator与被

java设计模式--结构型模式--组合模式

什么是组合模式,这个有待研究,个人觉得是各类组合而形成的一种结构吧. 组合模式: 1 组合模式 2 概述 3 将对象组合成树形结构以表示"部分-整体"的层次结构."Composite使得用户对单个对象和组合对象的使用具有一致性." 4 5 6 适用性 7 1.你想表示对象的部分-整体层次结构. 8 9 2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象. 10 11 12 参与者 13 1.Component 14 为组合中的对象声明接