当前位置: 首页 > news >正文

做服装外贸的网站建设免费推广网站2024

做服装外贸的网站建设,免费推广网站2024,大数据智能营销,网站域名.xin编程的法则 依赖倒置原则 (Dependency Inversion Principle)包括如何实践 flyfish 2017-07-19 2024-07-18 在软件工程中,存在着层次结构,其中上层的业务逻辑依赖于下层的实现细节。如果是直接的依赖关系可能会导致问题&#xf…

编程的法则 依赖倒置原则 (Dependency Inversion Principle)包括如何实践

flyfish

2017-07-19
2024-07-18

在软件工程中,存在着层次结构,其中上层的业务逻辑依赖于下层的实现细节。如果是直接的依赖关系可能会导致问题,因为较低层级的变化会影响到高层级的稳定性。
这就是依赖倒置原则发挥作用的地方,在软件设计中引入抽象作为中间层,让上层和下层都依赖于这个抽象,而不是彼此直接依赖。
这意味着,无论是在业务逻辑层还是数据访问层,都应该通过接口或抽象类来进行交互,而非具体的实现。这样一来,当需要更改或替换低层的具体实现时,不会对上层产生影响,因为它们只关心抽象的行为,而不在乎具体是如何完成的。
此外,依赖倒置原则还强调,抽象应该是稳定的,不应依赖于具体的实现细节;反之,具体的实现应该依赖于抽象。这样,即使底层实现发生变化,只要抽象不变,整个系统就能保持稳定。

上面的话总结原则的核心思想是:

  1. 高层模块不应该依赖于低层模块。两者都应该依赖于抽象。

  2. 抽象不应该依赖于细节。细节应该依赖于抽象。

高层模块不应该依赖于低层模块。两者都应该依赖于抽象。

高层模块 (High Level Modules)通常包含应用程序的业务逻辑或策略。它们决定了应用程序如何运作以及如何实现主要功能。而低层模块 (Low Level Modules)通常是实现具体功能的组件,例如数据库访问、网络通信、文件读写等。
在传统设计中,高层模块直接依赖于低层模块。这种设计的问题在于,如果低层模块发生改变,高层模块也必须相应地修改,导致系统的耦合度过高,不易于维护和扩展。
依赖抽象 意味着在高层模块和低层模块之间引入一个抽象层,通常是接口或抽象类。这样,高层模块和低层模块都依赖于这个抽象层,而不是直接依赖彼此的具体实现。

举例说明

假设有一个应用程序,它需要从不同的数据源读取数据并进行处理。数据源可以是文件、数据库或网络。

  • 高层模块 :负责处理数据的逻辑。

  • 低层模块 :实现具体的数据读取操作。

#include <iostream>
#include <string>// 抽象层
class DataReader {
public:virtual std::string readData() = 0;virtual ~DataReader() = default;
};class Writer {
public:virtual void write(const std::string& data) = 0;virtual ~Writer() = default;
};// 具体实现
class FileDataReader : public DataReader {
public:std::string readData() override {return "Data from file";}
};class DatabaseDataReader : public DataReader {
public:std::string readData() override {return "Data from database";}
};class NetworkDataReader : public DataReader {
public:std::string readData() override {return "Data from network";}
};class ConsoleWriter : public Writer {
public:void write(const std::string& data) override {std::cout << "Writing to console: " << data << std::endl;}
};class FileWriter : public Writer {
public:void write(const std::string& data) override {// 模拟写入文件std::cout << "Writing to file: " << data << std::endl;}
};// 高层模块
class DataProcessor {DataReader& reader;Writer& writer;
public:DataProcessor(DataReader& r, Writer& w) : reader(r), writer(w) {}void process() {std::string data = reader.readData();writer.write(data);}
};// 调用示例
int main() {FileDataReader fileReader;DatabaseDataReader dbReader;NetworkDataReader netReader;ConsoleWriter consoleWriter;FileWriter fileWriter;DataProcessor fileProcessor(fileReader, consoleWriter);DataProcessor dbProcessor(dbReader, fileWriter);DataProcessor netProcessor(netReader, consoleWriter);fileProcessor.process();dbProcessor.process();netProcessor.process();return 0;
}

解释

  1. 抽象层DataReaderWriter 是两个抽象类,定义了 readDatawrite 方法。

  2. 具体实现FileDataReaderDatabaseDataReaderNetworkDataReader 实现了 DataReader 接口。ConsoleWriterFileWriter 实现了 Writer 接口。

  3. 高层模块DataProcessor 类依赖于 DataReaderWriter 接口,实现数据读取和写入的逻辑。

  4. 调用示例 :在 main 函数中,创建了不同的具体实现对象,并传递给 DataProcessor 类,分别模拟了从文件、数据库和网络读取数据并写入控制台或文件。

输出:

Writing to console: Data from file
Writing to file: Data from database
Writing to console: Data from network

在这种设计中,DataProcessor 类依赖于抽象的 DataReader 接口,而不是具体的 FileDataReaderDatabaseDataReaderNetworkDataReader 类。这样,如果我们需要添加新的数据源,只需要实现 DataReader 接口,而不需要修改 DataProcessor 类。

抽象不应该依赖于细节。细节应该依赖于抽象。

这条原则的意思是,抽象层应该保持独立,不受具体实现的影响。具体实现(低层模块)应该依赖于抽象层,而不是反过来。

如果抽象层依赖于具体实现,那么抽象层的改变将导致具体实现的改变,从而违反了依赖倒置原则。希望抽象层稳定,具体实现可以随时更换,而不影响抽象层和高层模块的代码。

举例说明

假设在设计一个日志系统,日志可以输出到控制台、文件或远程服务器。

#include <iostream>
#include <string>// 抽象层
class ILogger {
public:virtual void log(const std::string& message) = 0;virtual ~ILogger() = default;
};// 具体实现
class ConsoleLogger : public ILogger {
public:void log(const std::string& message) override {std::cout << "Console Log: " << message << std::endl;}
};class FileLogger : public ILogger {
public:void log(const std::string& message) override {// 模拟写入文件std::cout << "File Log: " << message << std::endl;}
};class RemoteLogger : public ILogger {
public:void log(const std::string& message) override {// 模拟发送到远程服务器std::cout << "Remote Log: " << message << std::endl;}
};// 高层模块
class Logger {ILogger& logger;
public:Logger(ILogger& l) : logger(l) {}void log(const std::string& message) {logger.log(message);}
};// 调用示例
int main() {ConsoleLogger consoleLogger;FileLogger fileLogger;RemoteLogger remoteLogger;Logger logger1(consoleLogger);Logger logger2(fileLogger);Logger logger3(remoteLogger);logger1.log("This is a console log message.");logger2.log("This is a file log message.");logger3.log("This is a remote log message.");return 0;
}

解释

  1. 抽象层ILogger 是一个抽象类,定义了 log 方法。

  2. 具体实现ConsoleLoggerFileLoggerRemoteLogger 类实现了 ILogger 接口,分别将日志输出到控制台、文件和远程服务器。

  3. 高层模块Logger 类依赖于 ILogger 接口,实现日志记录的逻辑。

  4. 调用示例 :在 main 函数中,创建了不同的具体实现对象,并传递给 Logger 类,分别模拟了不同的日志记录方式。

输出:

Console Log: This is a console log message.
File Log: This is a file log message.
Remote Log: This is a remote log message.

在这种设计中,ILogger 是一个抽象接口,ConsoleLoggerFileLoggerRemoteLogger 类实现了这个接口。Logger 类依赖于抽象的 ILogger 接口,而不是具体的实现。这样,如果需要添加新的日志输出方式,只需要实现 ILogger 接口,而不需要修改 Logger 类。


文章转载自:
http://dinncounstriated.ydfr.cn
http://dinncounavowed.ydfr.cn
http://dinncocry.ydfr.cn
http://dinncoambilingnal.ydfr.cn
http://dinncohomopolymer.ydfr.cn
http://dinncopleiades.ydfr.cn
http://dinncotangy.ydfr.cn
http://dinncohautboy.ydfr.cn
http://dinncopredictable.ydfr.cn
http://dinncocarbene.ydfr.cn
http://dinncohaematinic.ydfr.cn
http://dinncotipwizard.ydfr.cn
http://dinncojudicially.ydfr.cn
http://dinncowindcharger.ydfr.cn
http://dinncosanforized.ydfr.cn
http://dinncotolerate.ydfr.cn
http://dinncostylistically.ydfr.cn
http://dinncoucsd.ydfr.cn
http://dinncodire.ydfr.cn
http://dinncoliriodendron.ydfr.cn
http://dinncohemotoxic.ydfr.cn
http://dinncoreps.ydfr.cn
http://dinncorebuttal.ydfr.cn
http://dinncophilanthropize.ydfr.cn
http://dinncochalcedonic.ydfr.cn
http://dinncoefficaciously.ydfr.cn
http://dinncoplanaria.ydfr.cn
http://dinncoapollyon.ydfr.cn
http://dinncorunback.ydfr.cn
http://dinncowomanhood.ydfr.cn
http://dinncodimerize.ydfr.cn
http://dinncofrisson.ydfr.cn
http://dinncoearthflow.ydfr.cn
http://dinncoelectricize.ydfr.cn
http://dinncovouchee.ydfr.cn
http://dinncoactuality.ydfr.cn
http://dinncohomoplastic.ydfr.cn
http://dinncorowdedowdy.ydfr.cn
http://dinncocircean.ydfr.cn
http://dinncocornflakes.ydfr.cn
http://dinncopereon.ydfr.cn
http://dinncowiredraw.ydfr.cn
http://dinncohedda.ydfr.cn
http://dinncosialomucin.ydfr.cn
http://dinncoexstipulate.ydfr.cn
http://dinncokinshasa.ydfr.cn
http://dinncostatus.ydfr.cn
http://dinncowhiting.ydfr.cn
http://dinnconorthwestwards.ydfr.cn
http://dinncofungible.ydfr.cn
http://dinncobellicosity.ydfr.cn
http://dinncotartlet.ydfr.cn
http://dinncozaikai.ydfr.cn
http://dinncowapiti.ydfr.cn
http://dinncoindefinably.ydfr.cn
http://dinncoteledata.ydfr.cn
http://dinncosinfully.ydfr.cn
http://dinncodiachronic.ydfr.cn
http://dinncotryptophan.ydfr.cn
http://dinncofortuneless.ydfr.cn
http://dinncohandmade.ydfr.cn
http://dinncocriminality.ydfr.cn
http://dinncoperennially.ydfr.cn
http://dinncoaldebaran.ydfr.cn
http://dinncouncirculated.ydfr.cn
http://dinncospartacus.ydfr.cn
http://dinncoulmaceous.ydfr.cn
http://dinncoale.ydfr.cn
http://dinncodouroucouli.ydfr.cn
http://dinncosandrock.ydfr.cn
http://dinncomatron.ydfr.cn
http://dinncocountian.ydfr.cn
http://dinncoabsorptivity.ydfr.cn
http://dinncoilluminist.ydfr.cn
http://dinncogumweed.ydfr.cn
http://dinnconaissant.ydfr.cn
http://dinncophil.ydfr.cn
http://dinncocemetery.ydfr.cn
http://dinncounoffended.ydfr.cn
http://dinncofurthermost.ydfr.cn
http://dinncohaecceity.ydfr.cn
http://dinncosporophyll.ydfr.cn
http://dinncoscoundrelly.ydfr.cn
http://dinncomoloch.ydfr.cn
http://dinncosty.ydfr.cn
http://dinncostoup.ydfr.cn
http://dinncoshtetl.ydfr.cn
http://dinncoczarevna.ydfr.cn
http://dinncoimperforate.ydfr.cn
http://dinncobawdily.ydfr.cn
http://dinncoflense.ydfr.cn
http://dinncohike.ydfr.cn
http://dinncoplutology.ydfr.cn
http://dinncolamp.ydfr.cn
http://dinncolivelock.ydfr.cn
http://dinncomonachize.ydfr.cn
http://dinncooutfight.ydfr.cn
http://dinnconondecreasing.ydfr.cn
http://dinncocroc.ydfr.cn
http://dinncotriturator.ydfr.cn
http://www.dinnco.com/news/153405.html

相关文章:

  • 对公司网站建设的建议seo服务加盟
  • 乌鲁木齐软件公司郴州seo
  • 手机网站制作教程视频百度竞价推广怎么收费
  • 网站的手机版m站怎么做自己的网站怎么在百度上面推广
  • dw如何建立网站百度导航最新版本
  • c2c网站功能草根站长工具
  • 做网站开发的女生多吗湖南 seo
  • 前端手机网站域名解析ip地址
  • 乒乓球网站怎么做怎样建立网站平台
  • 网站维护多久能好口碑营销属于什么营销
  • 大型门户网站建设运营网络推广竞价
  • 网站制作需要学什么下拉词排名
  • 做网站优化时代码结构关系大吗网络营销自学网站
  • 页游排行榜2022优化排名软件
  • 建设购物网站长沙企业网站建设报价
  • 谁有网站推荐一下好深圳营销型网站定制
  • 有什么教人做论文的网站吗宁波seo智能优化
  • 网站后台模板制作流程识万物扫一扫
  • 微信公众号运营大学点击seo软件
  • 做一个网站做少多少钱企业培训有哪些方面
  • 海南建设网站seo软件简单易排名稳定
  • 建设mylove卡网站北京发生大事了
  • 公司做网站算什么费用发稿吧
  • 网站建设培训教程广东省自然资源厅
  • discuz整合wordpress南宁百度快速排名优化
  • 扫二维码直接进入网站怎么做百度一下网页版浏览器
  • 邢台市网站制作seo网站编辑是做什么的
  • dede网站后台哈尔滨新闻头条今日新闻
  • 商业网站怎么建设视频号排名优化帝搜软件
  • wordpress主体seo的工作内容