【发布时间】:2016-06-22 11:56:27
【问题描述】:
我的类具有相同名称的方法,它们做同样的事情,但它们的实现方式不同。
例如:
class converterA {
map(Item1 item1) {
// Implementation details.
}
convert(Item1 item1) {
// Implementation details.
}
translate(Item1 item1) {
// Implementation details.
}
}
class converterB {
map(Item2 item2) {
// Implementation details.
}
convert(Item2 item2) {
// Implementation details.
}
translate(Item2 item2) {
// Implementation details.
}
}
我考虑过使用接口,但问题是方法采用不同的参数。然而,模板也不完全适合,因为 Item1 和 Item2 以不同的方式运行。换句话说,它们没有通用方法,因此模板也不完全适合。
这里有重构代码的解决方案吗?
【问题讨论】:
-
在这种情况下你想通过重构获得什么?
-
我只是想知道是否有办法压缩代码或有一个可以扩展的接口样式类,因为这两个类具有相似的功能。
标签: c++11 design-patterns