- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
对于这个有点冗长的问题,请提前接受我的道歉。这是我能想出的最小的独立示例...我很确定必须有一些明显/很好/整洁的解决方案来解决这个问题,但我目前看不到它。
好吧,问题来了:想象一下下面的情况(注意,代码的可编译版本可在 http://goo.gl/dhRNex 获得)。假设
struct Thing1 {
public:
void bar(class Implementation1 &i) {
i.baz();
}
// ...various other methods like bar()
};
struct Thing2 {
public:
void bar(class Implementation2 &i) {
i.qux();
}
// ...various other methods like bar()
};
给出。不幸的是,这些类是固定的,即不能更改/重构。
然而,Implementation1
和 Implementation2
是多变的。这两个类共享很多相似的代码,因此将共享代码放在一个公共(public)基类中似乎很自然。但是,代码取决于 Thing
的类型使用过,但 Thing1
没有公共(public)基类和 Thing2
,因此使用模板似乎也很自然。因此,我为基类想出了以下解决方案
template<class T, class S>
struct ImplementationBase {
public:
S *self;
void foo() {
T thing;
thing.bar(*self);
}
// ...lots more shared code like foo()
};
及具体实现
struct Implementation1 : public ImplementationBase<class Thing1, class Implementation1> {
public:
Implementation1() {
self = this;
}
void baz() {
std::cout << "Qux!" << std::endl;
}
};
struct Implementation2 : public ImplementationBase<class Thing2, class Implementation2> {
public:
Implementation2() {
self = this;
}
void qux() {
std::cout << "Qux!" << std::endl;
}
};
理想情况下,我们会使用 this
而不是 self
在 foo
,但问题是 this
类型为 ImplementationBase<class Thing1, class Implementation1>
,但是Implementation1
是必须的。显然,整个事情一团糟,Implementation
和 Thing
类耦合得太紧密,但如果不能重构 Thing
,我看不到一个简单的出路。类。所以,最后,我的问题是:
self
更好的替代方法?上面的技巧?如果您已经读到这里,非常感谢您花时间阅读整个故事,对于这个冗长的问题,我再次表示歉意。
最佳答案
您已经在使用 CRTP,因此您根本不需要 self:
template<class T, class S>
struct ImplementationBase {
public:
S* getThis() { return static_cast<S*>(this); }
void foo() {
T thing;
thing.bar(*getThis());
}
// ...lots more shared code like foo()
};
关于c++ - 当只能更改一侧时重构紧密耦合的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494072/
Law of Demeter表示你应该只与你直接了解的对象交谈。也就是说,不要执行方法链接来与其他对象通信。当您这样做时,您正在与中间对象不恰本地建立不正确的链接coupling您的代码到其他代码。
我已经实现了一个 CodeService,它将从代码表中检索国家/地区列表。 在我的 ShippingService 中,我想检查订单是否运送到某个国家/地区。 在这种情况下,我应该使用 CodeSe
虽然这个论坛上有很多包含耦合和内聚示例的好示例,但我正在努力将其完全应用到我的代码中。我可以识别代码中可能需要更改的部分。是否有任何 Java 专家能够查看我的代码并向我解释哪些方面是好的和坏的。我根
我有一个创建新对象 Student 并将其添加到数组列表 studentRegister 的方法: public void addStudent(String name, String age) {
在高耦合环境中,更改一个模块会影响另一个模块。好的,但我看不出这怎么可能(除了更改方法签名或返回类型)? 好吧,如果我改变一个类,那么它只能在以下情况下破坏其他类中的代码: 如果我突然改变了一个方法的
问题描述: 我正在一个项目中,以采用现有的JavaScript脚本并对其进行修改以支持异步加载。如果您不了解异步或延迟加载脚本,但想了解更多信息read this。 main.js脚本要做的第一件事之
我正在编写一个模拟,需要一些关于设计的提示。基本思想是生成给定随机过程的数据,然后将其用于各种计算。例如对于 1 次迭代: 进程 1 -> 为源 1 生成数据:x1 进程 2 -> 为源 1 生成数据
给定两个字符串,S1 和 S2。给定的计分方案包括空位罚分、不匹配分数和匹配分数。 找到与 S2 最匹配的 S1。 我的想法是列出所有可能的S1,然后与S2一一匹配。使用暴力列出所有可能的 S1。然后
我尝试使用 ctypes.CDLL 类 (Linux) 在 Python 中加载 C 共享库 .so。这是 link告诉我我做了什么。正如我看到的文档,它说 CDLL 类假定函数返回 int 类型。我
目录 什么是耦合性 什么是程序间的耦合 如何解耦 工厂模式解耦 案例 原因就是: 解决思路:
我尝试使用 Matlab 函数 Pdepe ( https://www.mathworks.com/help/matlab/ref/pdepe.html ) 求解平流扩散 react 问题的一维耦合偏
如何处理高级 C++ 应用程序中的耦合? 我们可以使用(例如)Witty 编写的 web 应用程序的完成代码,并用它制作控制台应用程序吗? ...或者将其更改为使用 Qt 制作的带有 GUI 的桌面应
假设我们有类: class Post def save # implementation end def self.find(id) #implementation e
简介 我正在尝试使用接口(interface)、抽象类和泛型在 Java 中创建一个相当复杂的结构。由于没有使用泛型的经验,在创建良好的 OOP 设计方面只有一般经验,这开始证明是一个相当大的挑战。
考虑 Windows 资源管理器(或 regedit 或类似工具)。左侧是 TreeView ,右侧是 ListView 。在我所知道的所有情况下,右 View 的内容都反射(reflect)了左 P
我有一个耦合代码(fortran 和 c++),我现在在 python 中调用。 main.exe 工作正常,但是当我在 python 中调用耦合版本时,出现段错误(核心转储)错误。我确定了出现问题的
我正在尝试编写 CMakeLists.txt 以耦合一个简单的 FORTRAN 程序,该程序使用 iso_c_binding 调用 C++ 函数。当我使用 gfortran 作为 FORTRAN 编译
我们将 NHibernate 用于 ORM,在程序的初始化阶段,我们需要从数据库中加载某个类 T 的许多实例。 在我们的应用程序中,提取所有这些实例的以下代码需要很长时间: public IList
我是一名优秀的程序员,十分优秀!