網站首頁 編程語言 正文
在我們設計C/C++ 程序的時候,有時需要兩個類或者兩個模塊相互“認識”,或者兩個模塊間函數互相調用,假設我們正在開發一個網上商店,代表的網店客戶的類必須要知道相關的賬戶。
UML圖如下,這被稱為環依賴,這兩個類直接或間接地相互依賴。
一般我們都會采用結構體或類前置聲明的方式,在解決此問題。
customer.h
#ifndef CUSTOMER_H_
#define CUSTOMER_H_
class Account;
class Customer
{
// ...
void setAccount(Account* account);
{
customerAccount = account;
}
//...
private:
Account* customerAccount;
};
#endif
account.h
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
class Customer;
class Account
{
public:
//...
void setOwer(Customer* customer)
{
ower = customer;
}
//...
private:
Customer* ower;
}
#endif
老實說上面的方式確實可以消除編譯器的錯誤,但是這種解決方案不夠好。
下面的調用示例,存在一個問題,當刪除Account的實例,Customer的實例仍然存在,且內部的指向Account的指針為空。使用或解引用此指正會導致嚴重的問題。
#include "account.h"
#include "customer.h"
//...
Account *account = new Accout{};
Customer *customer = new Customer{};
account->setOwer(customer);
customer->setAccount(account);
那有沒有更好的做法呢,依賴倒置原則可以很好的解決此類問題。
依賴倒置原則
第一步是我們不在兩個類中的其中一個訪問另一個,相反,我們只通過接口進行訪問。我們從Customer中提取Ower的接口,作為示例,Ower接口中聲明一個純虛函數,該函數必須由此接口類覆蓋。
ower.h
#ifndef OWNER_H_
#define OWNER_H_
#include <memory>
#include <string>
class owner
{
public:
virtual ~owner()=default;
virtual std::string getName() const = 0;
};
using OwnerPtr = std::shared_ptr<Owner>;
#endif
Customer.h
#ifndef CUSTOMER_H_
#define CUSTOMER_H_
#include "Owner.h"
#include "Account.h"
class Customer:public Owner
{
public:
void setAccount(AccountPtr account)
{
customerAccount = account;
}
virtual std::string getName() const override{
//return string
}
private:
Account customerAccount;
};
using CustomerPtr = std::shared_ptr<Customer>;
#endif
account.h
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include "Owner.h"
class Account
{
public:
void setOwner(OwnerPtr owner)
{
this->owner = owner;
}
private:
OwnerPtr owner;
};
using AccountPtr = std::shared_ptr<Account>;
現在設計完發現這兩個模塊間消除了環依賴。
C/C++ 通過回調函數和信號槽的方式降低模塊的耦合性
為了降低模塊功能代碼的耦合性,我們經常采用回調函數或者信號槽的方式來聯系兩個模塊。
回調函數的方式:
a.h
#pragma once
#ifndef A_H_
#define A_H_
#include <stdio.h>
int test(int c);
#endif
a.c
#include "a.h"
int test(int c)
{
c = 0x11112;
return c;
}
b.h
#pragma once
#ifndef B_H_
#define B_H_
#include <stdio.h>
typedef int (*PtrFunA)(int);
int testb(PtrFunA, int c);
#endif
b.c
#include "b.h"
int testb(PtrFunA a, int c)
{
int ret = a(c);
printf("%d\n", ret);
return ret;
}
main.c
#include <stdio.h>
#include "a.h"
#include "b.h"
int main()
{
testb(test,123);
}
這樣做的好處降低代碼的耦合性,是A模塊的功能代碼只寫在A.C中。B.C的功能代碼只寫在B.C中,改進的后的回調函數可以用void*進行傳參,在a.c中對void*進行判斷,這是很多代碼常做的操作。
信號槽的話則更加靈活性和代碼的耦合度再次降低,后期在說,先寫在這了。
原文鏈接:https://blog.csdn.net/wanglei_11/article/details/128793675
相關推薦
- 2022-09-03 Docker進階之構建自定義鏡像實戰指南_docker
- 2023-04-18 Android粒子線條效果實現過程與代碼_Android
- 2022-06-16 C語言深入分析遞歸函數的實現_C 語言
- 2022-05-28 C語言結構體詳細圖解分析_C 語言
- 2022-04-05 Python?Opencv基于透視變換的圖像矯正_python
- 2022-02-26 SpringSecurity 自定義JwtAuthorFilter基于JWT的Token驗證
- 2022-09-07 C語言函數調用堆棧詳情分析_C 語言
- 2022-05-13 系統分區卷GUID
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支