23種設計模式之(十)橋接模式(python_c++實現)
阿新 • • 發佈:2018-12-10
23種設計模式之(十)橋接模式(Bridge)
本文主要介紹23種設計模式之組合模式,附詳細python/c++示例程式碼。 - 概念 - 應用場景 - 注意事項 - 程式碼示例 - 總結 - 程式碼連結
橋接模式(Bridge)
概念
橋接模式,是構造型的設計模式之一。Bridge模式基於類的最小設計原則,通過使用封裝,聚合以及繼承等行為來讓不同的類承擔不同的責任。它的主要特點是把抽象(abstraction)與行為實現(implementation)分離開來,從而可以保持各部分的獨立性以及應對它們的功能擴充套件。
GoF對橋接模式的定義是:將抽象部分與它的實現部分分離,使它們都可以獨立地變化。
應用場景
(1)如果一個系統需要在構件的抽象化角色和具體化角色之間增加更多的靈活性,避免在兩個層次之間建立靜態的繼承聯絡,通過橋接模式可以使它們在抽象層建立一個關聯關係。
(2)對於那些不希望使用繼承或因為多層次繼承導致系統類的個數急劇增加的系統,橋接模式尤為適用。
(3)一個類存在兩個獨立變化的維度,且這兩個維度都需要進行擴充套件。
程式碼示例
C++程式碼示例
/************************************************************************/
/* 設計模式專題
/*
/* 組合模式
/*
/* Author : zzl
/*
/* 程式設計環境: window10 vs2010
/*
/* Date : 20180916
/************************************************************************/
#include <iostream>
#include "string"
#include "list"
class IFile
{
public:
virtual void display() = 0;
virtual int add(IFile *ifile) = 0;
virtual int remove(IFile *ifile) = 0;
virtual std::list<IFile *>* getChild() = 0;
};
//檔案結點
class File : public IFile
{
public:
File(std ::string name)
{
m_name = name;
}
virtual void display()
{
printf("%s\n",m_name.c_str());
}
virtual int add(IFile *ifile)
{
return -1;
}
virtual int remove(IFile *ifile)
{
return -1;
}
virtual std::list<IFile *>* getChild()
{
return NULL;
}
private:
std::string m_name;
};
//目錄 結點
class Dir : public IFile
{
public:
Dir(std::string name)
{
m_name = name;
m_list = new std::list<IFile *>;
m_list->clear();
}
virtual void display()
{
printf("%s\n",m_name.c_str());
}
virtual int add(IFile *ifile)
{
m_list->push_back(ifile);
return 0;
}
virtual int remove(IFile *ifile)
{
m_list->remove(ifile);
return 0;
}
virtual std::list<IFile *>* getChild()
{
return m_list;
}
private:
std::string m_name;
std::list<IFile *> *m_list;
};
// 遞迴的顯示樹
void showTree(IFile *root, int level)
{
int i = 0;
if (root == NULL)
{
return ;
}
for (i=0; i<level; i++)
{
printf("\t");
}
//1 顯示根 結點
root->display();
//2 若根結點 有孩子
//判讀孩子是檔案,顯示名字 )
//判斷孩子是目錄,showTree(子目錄)
std::list<IFile *> *mylist = root->getChild();
if (mylist != NULL) //說明是一個目錄
{
for (std::list<IFile *>::iterator it=mylist->begin(); it!=mylist->end(); it++)
{
if ( (*it)->getChild() == NULL )
{
for (i=0; i<=level; i++) //注意 <=
{
printf("\t");
}
(*it)->display();
}
else
{
showTree(*it, level+1);
}
}
}
}
void main()
{
Dir *root = new Dir("C");
//root->display();
Dir *dir1 = new Dir("111dir");
File *aaafile = new File("aaa.txt");
//獲取root結點下的 孩子集合
std::list<IFile *> *mylist = root->getChild();
root->add(dir1);
root->add(aaafile);
for ( std::list<IFile *>::iterator it=mylist->begin(); it!=mylist->end(); it++ )
{
(*it)->display();
}
Dir *dir222 = new Dir("222dir");
File *bbbfile = new File("bbb.txt");
dir1->add(dir222);
dir1->add(bbbfile);
printf("--------showTree---------\n");
showTree(root, 0);
}
python程式碼示例
# -*- coding: utf-8 -*-
###################################################################
# 設計模式專題
#
# 組合模式
#
# Author : zzl
#
# 程式設計環境: window10 python2.7
#
# Date : 20180916
##################################################################
class IFile(object):
def display(self):
pass
def add(self, ifile):
pass
def remove(self, ifile):
pass
def get_child(self):
pass
# 檔案結點
class File(IFile):
def __init__(self, name):
self.m_name = name
def display(self):
print(self.m_name)
def add(self, ifile):
return -1
def remove(self, ifile):
return -1
def get_child(self):
return None
# 目錄結點
class Dir(IFile):
def __init__(self, name):
self.m_name = name
self.m_list = []
m_name = name
def display(self):
print(self.m_name)
def add(self, ifile):
self.m_list.append(ifile)
def remove(self, ifile):
self.m_list.remove(ifile)
def get_child(self):
return self.m_list
def show_tree(root, level):
if not root:
return
for i in range(level):
print "\t",#python2.7 列印不換行
# 1、顯示根結點
root.display()
# 2、若根結點有孩子
# 判讀孩子是檔案, 顯示名字
# 判斷孩子是目錄, showTree(子目錄)
mylist = root.get_child()
if mylist: # 說明是一個目錄
for child in mylist:
if not child.get_child():
for i in range(level + 1):
print "\t", #python2.7 列印不換行
child.display()
else:
show_tree(child, level + 1)
if __name__ == "__main__":
root = Dir("C")
dir1 = Dir("111dir")
aaafile = File("aaa.txt")
# 獲取root結點下的孩子集合
mylist = root.get_child()
root.add(dir1)
root.add(aaafile)
for child in mylist:
child.display()
dir222 = Dir("222dir")
bbbfile = File("bbb.txt")
dir1.add(dir222)
dir1.add(bbbfile)
print("--------showTree---------")
show_tree(root, 0)