• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一个具有上进心的码农
因为一篇文章中有很多是从很多篇文章中摘取的,请恕我没有一一说明摘取出处,如果没有说明,则该文章默认是摘取,如有侵犯您的权益,请与我联系,将会马上删除。
博客园    首页    新随笔    联系   管理    订阅  订阅

桥接模式

此BLOG是我自已在学习当中的一些体会。 大家可能会看不懂。 因为我只是写着记着的。 我也不打算发到任何地方。

 

先来谈谈桥接模式的概念.

在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就要使用Bridge模式。

 

 

下面是代码. 我用了三维的。 

 

 

/// <summary>
    /// 这里开始把其中一个给抽像出来
    /// </summary>
    public abstract class AbstractCloth
    {
    public abstract void excute(string msg);
    }


    public class Cat : AbstractCloth
    {

        public override void excute(string msg)
        {
            Console.Write("cat1");
        }
    }

    public class Cat2 : AbstractCloth
    {

        public override void excute(string msg)
        {
            Console.Write("cat2");
        }
    }


    /// <summary>
    /// 这里开始把其中一个给抽像出来
    /// </summary>
    public abstract class AbstractCloth2
    {
        public abstract void excute(string msg);
    }


    public class Cat3 : AbstractCloth2
    {

        public override void excute(string msg)
        {
            Console.Write("Cat3");
        }
    }

    public class Cat4 : AbstractCloth2
    {

        public override void excute(string msg)
        {
            Console.Write("Cat4");
        }
    }


    public abstract class AbstractTure
    {
        /// <summary>
        /// 作为属性
        /// </summary>
        protected AbstractCloth oAbstractCloth;

        public AbstractCloth AbstractClothAttiube
        {
            set { oAbstractCloth = value; }
        }

        /// <summary>
        /// 作为属性
        /// </summary>
        protected AbstractCloth2 oAbstractCloth2;

        public AbstractCloth2 AbstractClothAttiube2
        {
            set { oAbstractCloth2 = value; }
        }

        public virtual void Write(string msg)    //如果这里是抽像方法时。不能有实现代码。
        {
           //这里开始执行AbstractCloth的方法
            oAbstractCloth.excute(msg);
            oAbstractCloth2.excute(msg);
        }
       
    }

    public class cloth1 : AbstractTure
    {
        public override void Write(string msg)
        {
            //这里开始执行AbstractCloth的方法
            oAbstractCloth.excute(msg);
            oAbstractCloth2.excute(msg);
        }
    }

    public class cloth2 : AbstractTure
    {
        public override void Write(string msg)
        {
            //这里开始执行AbstractCloth的方法
            oAbstractCloth.excute(msg);
            oAbstractCloth2.excute(msg);
        }
    }

 

 

调用:

            AbstractTure oAbstractTure = new cloth1();

            oAbstractTure.AbstractClothAttiube = new Cat();    //这里有两个属性。
            oAbstractTure.AbstractClothAttiube2 = new Cat3(); //这里有两个属性。代表着的是三维的

            oAbstractTure.Write("cat2");  //有点多。用不着

 

一个抽像类里可以存在抽像方法和虚拟方法。 抽像方法跟接口的方法一样。 不实现具体的实现。也就是不能有代码。

 

而虚拟方法却可以。  一个类继承于抽像类时。程序不会执行抽像类的代码。只会执行它的子类的代码。

 

抽像方法必须实现。

 

 

posted @ 2008-07-31 10:30  不若相忘于江湖  阅读(319)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3