美文网首页
桥接模式

桥接模式

作者: 指间_璇律 | 来源:发表于2015-07-26 20:06 被阅读35次

分离抽象和实现:

模拟场景—— 中杯,大杯,咖啡加奶,咖啡不加奶。其中,中杯和大杯为抽象,加奶和不加奶为行为:

class Program

{

public interface MakeCoffee

{

void Making();

}

///

/// 行为 - 牛奶咖啡(加奶)

///

public class WhiteCoffee : MakeCoffee

{

public void Making()

{

Console.WriteLine("牛奶咖啡");

}

}

///

/// 行为 - 原味咖啡(不加奶)

///

public class BlackCoffee : MakeCoffee

{

public void Making()

{

Console.WriteLine("原味咖啡");

}

}

///

/// 单件模式类用来加载当前MakeCoffee

///

public sealed class MakeCoffeeSingleton

{

private static MakeCoffee _instance;

public MakeCoffeeSingleton(MakeCoffee instance)

{

_instance = instance;

}

public MakeCoffee Instance()

{

return _instance;

}

}

///

/// 基类

///

public abstract class CupCoffee

{

private MakeCoffee _makeCoffee;

public CupCoffee(MakeCoffeeSingleton instance)

{

_makeCoffee = instance.Instance();

}

public MakeCoffee MakeCoffee

{

get { return _makeCoffee; }

}

public abstract void Make();

}

///

/// 抽象 - 中杯

///

public class MediumCoffee : CupCoffee

{

public MediumCoffee(MakeCoffeeSingleton makecoffee)

: base(makecoffee)

{

}

public override void Make()

{

Console.Write("中杯");

this.MakeCoffee.Making();

}

}

///

/// 抽象 - 大杯

///

public class LargeCoffee : CupCoffee

{

public LargeCoffee(MakeCoffeeSingleton makecoffee)

: base(makecoffee)

{

}

public override void Make()

{

Console.Write("中杯");

this.MakeCoffee.Making();

}

}

static void Main(string[] args)

{

MakeCoffeeSingleton whiteCoffeeSingleton = new MakeCoffeeSingleton(new WhiteCoffee());

// 中杯牛奶咖啡

MediumCoffee mediumWhiteCoffee = new MediumCoffee(whiteCoffeeSingleton);

mediumWhiteCoffee.Make();

// 大杯牛奶咖啡

LargeCoffee largeCupWhiteCoffee = new LargeCoffee(whiteCoffeeSingleton);

largeCupWhiteCoffee.Make();

MakeCoffeeSingleton blackCoffeeSingleton = new MakeCoffeeSingleton(new BlackCoffee());

// 中杯原味咖啡

MediumCoffee mediumBlackCoffee = new MediumCoffee(blackCoffeeSingleton);

mediumBlackCoffee.Make();

// 大杯牛奶咖啡

LargeCoffee largeCupBlackCoffee = new LargeCoffee(blackCoffeeSingleton);

largeCupBlackCoffee.Make();

Console.ReadKey();

}

}

相关文章

  • 设计模式-桥接模式

    设计模式-桥接模式 定义 桥接模式(Bridge Pattern)也称为桥梁模式、接口(Interface)模式或...

  • 结构型模式:桥接模式

    文章首发:结构型模式:桥接模式 七大结构型模式之二:桥接模式。 简介 姓名 :桥接模式 英文名 :Bridge P...

  • 设计模式之桥接模式

    设计模式之桥接模式 1. 模式定义 桥接模式又称柄体模式或接口模式,它是一种结构性模式。桥接模式将抽象部分与实现部...

  • 06-01-001 虚拟机的网络连接方式(转运整理)

    一、Bridged(桥接模式) 什么是桥接模式?桥接模式就是将主机网卡与虚拟机虚拟的网卡利用虚拟网桥进行通信。在桥...

  • 桥接模式与中介模式

    桥接模式-BRIDGE 对桥接模式感兴趣,是因为公司业务上需要桥接Html5和ReactNative两个平台。桥接...

  • 设计模式——桥接模式

    设计模式——桥接模式 最近公司组件分享设计模式,然而分配给我的是桥接模式。就在这里记录我对桥接模式的理解吧。 定义...

  • 桥接模式

    个人博客http://www.milovetingting.cn 桥接模式 模式介绍 桥接模式也称为桥梁模式,是结...

  • 桥接模式

    桥接模式 参考原文: https://zhuanlan.zhihu.com/p/62390221 定义 桥接模式 ...

  • 10-桥接模式

    桥接模式-Bridge Pattern【学习难度:★★★☆☆,使用频率:★★★☆☆】 处理多维度变化——桥接模式(...

  • Java设计模式——桥接模式

    Java设计模式之桥接模式 回顾 上一期分享了适配器模式,主要为了实现解耦 桥接模式 简介 桥接模式是对象的结构模...

网友评论

      本文标题:桥接模式

      本文链接:https://www.haomeiwen.com/subject/uejmqttx.html