物件導向設計六原則之 介面隔離原則

2021-09-16 19:16:16 字數 1028 閱讀 3019

1. 含義

客戶不應該被強迫使用他們不需要的介面。

2. 解釋

該原則用於不滿足單一職責原則的情況,客戶只需要知道具有內聚介面的抽象父類即可。

3. 舉例說明

如果有個辦公一體機,具有印表機,影印機,掃瞄機的功能。對於客戶來說,不需要接觸每個機器,只要接觸辦公一體機。

inte***cesegregationprinciple.h
#ifndef inte***cesegregationprinciple_h

#define inte***cesegregationprinciple_h

#include using namespace std;

class integrator

;class operator

;#endif // inte***cesegregationprinciple_h

inte***cesegregationprinciple.cpp
#include "inte***cesegregationprinciple.h"

void integrator::print()

void operator::copy(integrator *integrator)

void operator::scanner(integrator *integrator)

main.cpp
#include "inte***cesegregationprinciple.h"

integrator *integrator = new integrator();

operator oper;

oper.copy(integrator);

oper.print(integrator);

oper.scanner(integrator);

物件導向的六原則之介面隔離原則

記得在操作io流時,在最後要關閉流的時候要try catch,流多的時候就有一大堆try catch,如下所示 private void put string path,bitmap bitmap catch filenotfoundexception e finally catch ioexcep...

設計模式 物件導向設計原則之介面隔離原則

類a通過介面i依賴類b,類c通過介面i依賴類d,如果介面i對於類a和類b來說不是最小介面,則類b和類d必須去實現他們不需要的方法。介面最好大小合適,不臃腫,也不過於細緻。適合於需求,卻不多於需求 不實現它們不需要的方法 客戶端不應該依賴它不需要的介面 乙個類對另乙個類的依賴應該建立在最小的介面上。建...

物件導向的原則之介面隔離原則

設計應用程式的時候,如果乙個模組包含多個子模組,那麼我們應該小心對該模組做出抽象。設想該模組由乙個類實現,我們可以把系統抽象成乙個介面。但是要新增乙個新的模組擴充套件程式時,如果要新增的模組只包含原系統中的一些子模組,那麼系統就會強迫我們實現介面中的所有方法,並且還要編寫一些啞方法。這樣的介面被稱為...