设为首页 加入收藏

TOP

c#设计模式·结构型模式(一)
2017-10-10 13:38:20 】 浏览:6013
Tags:设计模式 结构 模式

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

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

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

之前公司的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 = r
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇autofac初识 下一篇智联卓聘IM演进过程

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目