0
点赞
收藏
分享

微信扫一扫

设计模式 桥接模式( bridge )


在桥接模式 这一片发现总是百思不得其解 道理都懂 但是总是也想不通

今天发现 我总是想着在那些功能上用到这个模式 而不是想这个模式带来的思想   所以总是想的很杂 现在跳出来这后 有点恍然大悟之感

它们都是设计模式 用来做凝定现实 的工具 不要具体到功能

桥接模式

用来对应 多维度的变化 实现抽象与实现分离

设计模式 桥接模式( bridge )_设计模式

 

打个比方 如果写一个log日志 有数据库记录 有文本记录  但是这个日志还要在net 和java 下 运行 那么这个怎么设计 

首先 我们要想俩个部分

一个部分 log 日志  实现方式 文本记录 和数据库  

二个部分 实现net 和java  平台

在把 这俩个部分关联起来

设计模式 桥接模式( bridge )_设计模式_02

 

实现Log案例

平台

public abstract class Implog
{
public abstract void execute(string msg);
}

class BaseLog:Log
{
public override void write(string log)
{
Console.WriteLine("DataBase 记录" + log);
imp.execute(log);
}
}

class TxTLog : Log
{
public override void write(string log)
{
Console.WriteLine("Textlog 记录" +log);
imp.execute(log);
}
}

 

数据库与text文本 存储

public abstract class Log
{

protected Implog imp;

public Implog Imp
{

set { imp = value; }
}

public virtual void write(string log)
{
imp.execute(log);
}

}

class BaseLog:Log
{
public override void write(string log)
{
Console.WriteLine("DataBase 记录" + log);
imp.execute(log);
}
}

class TxTLog : Log
{
public override void write(string log)
{
Console.WriteLine("Textlog 记录" +log);
imp.execute(log);
}
}

 

 

static void Main(string[] args)
{
Implog imp = new NImpLog();
Log l = new TxTLog();
l.Imp = imp;
l.write("这就是一个测试");
Console.ReadKey();
}

设计模式 桥接模式( bridge )_数据库_03

 

 

举报

相关推荐

0 条评论