網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
在我們?cè)O(shè)計(jì)C/C++ 程序的時(shí)候,有時(shí)需要兩個(gè)類或者兩個(gè)模塊相互“認(rèn)識(shí)”,或者兩個(gè)模塊間函數(shù)互相調(diào)用,假設(shè)我們正在開(kāi)發(fā)一個(gè)網(wǎng)上商店,代表的網(wǎng)店客戶的類必須要知道相關(guān)的賬戶。
UML圖如下,這被稱為環(huán)依賴,這兩個(gè)類直接或間接地相互依賴。
一般我們都會(huì)采用結(jié)構(gòu)體或類前置聲明的方式,在解決此問(wèn)題。
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
老實(shí)說(shuō)上面的方式確實(shí)可以消除編譯器的錯(cuò)誤,但是這種解決方案不夠好。
下面的調(diào)用示例,存在一個(gè)問(wèn)題,當(dāng)刪除Account的實(shí)例,Customer的實(shí)例仍然存在,且內(nèi)部的指向Account的指針為空。使用或解引用此指正會(huì)導(dǎo)致嚴(yán)重的問(wèn)題。
#include "account.h"
#include "customer.h"
//...
Account *account = new Accout{};
Customer *customer = new Customer{};
account->setOwer(customer);
customer->setAccount(account);
那有沒(méi)有更好的做法呢,依賴倒置原則可以很好的解決此類問(wèn)題。
依賴倒置原則
第一步是我們不在兩個(gè)類中的其中一個(gè)訪問(wèn)另一個(gè),相反,我們只通過(guò)接口進(jìn)行訪問(wèn)。我們從Customer中提取Ower的接口,作為示例,Ower接口中聲明一個(gè)純虛函數(shù),該函數(shù)必須由此接口類覆蓋。
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>;
現(xiàn)在設(shè)計(jì)完發(fā)現(xiàn)這兩個(gè)模塊間消除了環(huán)依賴。
C/C++ 通過(guò)回調(diào)函數(shù)和信號(hào)槽的方式降低模塊的耦合性
為了降低模塊功能代碼的耦合性,我們經(jīng)常采用回調(diào)函數(shù)或者信號(hào)槽的方式來(lái)聯(lián)系兩個(gè)模塊。
回調(diào)函數(shù)的方式:
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中,改進(jìn)的后的回調(diào)函數(shù)可以用void*進(jìn)行傳參,在a.c中對(duì)void*進(jìn)行判斷,這是很多代碼常做的操作。
信號(hào)槽的話則更加靈活性和代碼的耦合度再次降低,后期在說(shuō),先寫在這了。
原文鏈接:https://blog.csdn.net/wanglei_11/article/details/128793675
相關(guān)推薦
- 2022-11-14 git常用命令行操作
- 2022-04-27 jquery+css實(shí)現(xiàn)移動(dòng)端元素拖動(dòng)排序_jquery
- 2022-12-14 PostgreSql?JDBC事務(wù)操作方法詳解_PostgreSQL
- 2022-05-21 服務(wù)發(fā)現(xiàn)與負(fù)載均衡機(jī)制Service實(shí)例創(chuàng)建_服務(wù)器其它
- 2022-07-13 Collection和Collections有什么區(qū)別?
- 2023-04-08 C#以太網(wǎng)Sockets服務(wù)器設(shè)計(jì)實(shí)現(xiàn)_C#教程
- 2022-07-22 CSS3:盒陰影、邊界圖片、指定每一個(gè)圓角、背景、過(guò)度、動(dòng)畫、
- 2023-07-14 express+mongoose實(shí)現(xiàn)無(wú)限級(jí)分類
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支