- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个名为 Type 的虚拟类,以及派生类 Type1 和 Type2。
class Type {
public:
virtual void print() = 0;
};
class Type1 : public Type {
public:
void print() { cout << "I am of type 1" << endl; }
};
class Type2 : public Type {
public:
void print() { cout << "I am of type 2" << endl; }
};
根据用户输入的参数,我将实例化一个类或另一个类。
然后我想要另一个类来产生 Action ,这取决于作为参数给出的“Type”对象的类型。现在,这个类是:
class Action {
protected:
Type *t;
public:
Action(Type *t) : t(t) {} ;
void print() {
cout << "I am an action. My type says: ";
t->print();
}
};
如何让 Action::print 根据属性“t”的类型产生不同的任务?
我尝试过的:
我的愿望是有一个看起来像这样的 main:
int main(int argc, const char * argv[]) {
Type *t = new Type1();
Action *act = new Action(t);
act->print();
delete act, t;
return 0;
}
但是根据 t 的类型调用不同的打印方法。这可能(以及如何)吗?
编辑
看了评论,发现主要有两种可能的设计:
创建一个类Action,并调用特定的方法(print(Type1 *) 或print(Type2 *))。我认为这是评论中建议的访问者模式。
创建两个类,Action1 和 Action2(可能派生自 Action,也可能不是!),并实例化对应于 t、Type1 或 Type2 的类。
一种解决方案是否比另一种更清洁、更易于维护等?
编辑2
一些评论中提到的工厂模式呢?谁能告诉我它是否解决了我的问题?
最佳答案
首先要注意一点:我个人认为“使用 namespace ”是一种不好的做法,因为它会影响头文件。原因在整个互联网上都有描述。
此外,由于异常安全性,我总是尽量减少使用指针。 Sutter 有一本关于它的好书,叫做 Exceptional C++,它非常详细地描述了这些问题。然而,指针显然有其用途,其中一个特别是多态性。最后,如果类只有公共(public)成员,我喜欢将它们设为结构体……这只是个人喜好问题。
让我们从一些代码开始:
#include <string>
#include <iostream>
#include <memory>
struct Type
{
virtual void print() = 0;
};
struct Type1 : Type
{
void print() { std::cout << "I am of type 1" << std::endl; }
};
struct Type2 : Type
{
void print() { std::cout << "I am of type 2" << std::endl; }
};
class Action
{
protected:
std::unique_ptr<Type> t;
public:
Action(std::unique_ptr<Type> &&t) : t(std::move(t)) {};
void print()
{
std::cout << "I am an action. My type says: ";
t->print();
}
};
int main()
{
Action act(std::make_unique<Type1>());
act.print();
return 0;
}
您似乎要解决的第一个问题是用户输入会生成类型。根据输入,这可能指向一个抽象工厂或构建器模式,甚至是一个成熟的解析器,但如果这是一个简单的输入决定,最好 KISS:
int main()
{
std::string s;
std::getline(std::cin, s);
std::unique_ptr<Type> type;
if (s == "1")
{
type = std::make_unique<Type1>();
}
else
{
type = std::make_unique<Type2>();
}
Action act(std::move(type));
act.print();
return 0;
}
不过,通常您希望将模型与实现分开。 Action 有很多不同的形状和形式,所以您可能想根据您的模型做其他事情。访问者模式、双重 dispatch 或任何你想称呼的模式都会在这里派上用场。
请注意,我通常使用稍微修改过的访问器,它默认返回基本类型。这样,您可以轻松地将转换合并到您的代码中,恕我直言,这是一个常见的设计要求。在这一点上,我只是要使用指针。就我个人而言,我非常喜欢内存领域来解决内存管理问题,但我认为这超出了这个答案的范围。
最后,一个简单的基类可以帮助您摆脱无意义的管道。
struct TypeVisitor;
struct Type
{
virtual Type* Accept(TypeVisitor& visitor) = 0;
};
template <typename T>
struct TypeBase : Type
{
virtual Type* Accept(TypeVisitor& visitor) override
{
return visitor.Handle(static_cast<T*>(this));
}
};
struct Type1 : TypeBase<Type1>
{
};
struct Type2 : TypeBase<Type2>
{
};
struct TypeVisitor
{
virtual Type* Handle(Type1* input)
{
/* if necessary, recurse here like: input->child = input->child->Accept(this); */
return input;
}
virtual Type* Handle(Type2* input) { return input; }
};
struct DumpAction : TypeVisitor
{
virtual Type* Handle(Type1* input) override
{
std::cout << "Handling type 1." << std::endl;
return TypeVisitor::Handle(input);
}
virtual Type* Handle(Type2* input) override
{
std::cout << "Handling type 2." << std::endl;
return TypeVisitor::Handle(input);
}
};
int main()
{
DumpAction act;
Type2 type2;
type2.Accept(act);
return 0;
}
关于c++ - 如何根据参数的类型实例化不同的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43362744/
我有 table 像这样 -------------------------------------------- id size title priority
我的应用在不同的 Activity (4 个 Activity )中仅包含横幅广告。所以我的疑问是, 我可以对所有横幅广告使用一个广告单元 ID 吗? 或者 每个 Activity 使用不同的广告单元
我有任意(但统一)数字列表的任意列表。 (它们是 n 空间中 bin 的边界坐标,我想绘制其角,但这并不重要。)我想生成所有可能组合的列表。所以:[[1,2], [3,4],[5,6]] 产生 [[1
我刚刚在学校开始学习 Java,正在尝试自定义控件和图形。我目前正在研究图案锁,一开始一切都很好,但突然间它绘制不正确。我确实更改了一些代码,但是当我看到错误时,我立即将其更改回来(撤消,ftw),但
在获取 Distinct 的 Count 时,我在使用 Group By With Rollup 时遇到了一个小问题。 问题是 Rollup 摘要只是所有分组中 Distinct 值的总数,而不是所有
这不起作用: select count(distinct colA, colB) from mytable 我知道我可以通过双选来简单地解决这个问题。 select count(*) from (
这个问题在这里已经有了答案: JavaScript regex whitespace characters (5 个回答) 2年前关闭。 你能解释一下为什么我会得到 false比较 text ===
这个问题已经有答案了: 奥 git _a (56 个回答) 已关闭 9 年前。 我被要求用 Javascript 编写一个函数 sortByFoo 来正确响应此测试: // Does not cras
所以,我不得不说,SQL 是迄今为止我作为开发人员最薄弱的一面。也许我想要完成的事情很简单。我有这样的东西(这不是真正的模型,但为了使其易于理解而不浪费太多时间解释它,我想出了一个完全模仿我必须使用的
这个问题在这里已经有了答案: How does the "this" keyword work? (22 个回答) 3年前关闭。 简而言之:为什么在使用 Objects 时,直接调用的函数和通过引用传
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: what is the difference between (.) dot operator and (-
我真的不明白这里发生了什么但是: 当我这样做时: colorIndex += len - stopPos; for(int m = 0; m < len - stopPos; m++) { c
思考 MySQL 中的 Group By 函数的最佳方式是什么? 我正在编写一个 MySQL 查询,通过 ODBC 连接在 Excel 的数据透视表中提取数据,以便用户可以轻松访问数据。 例如,我有:
我想要的SQL是这样的: SELECT week_no, type, SELECT count(distinct user_id) FROM group WHERE pts > 0 FROM bas
商店表: +--+-------+--------+ |id|name |date | +--+-------+--------+ |1 |x |Ma
对于 chrome 和 ff,当涉及到可怕的 ie 时,这个脚本工作完美。有问题 function getY(oElement) { var curtop = 0; if (oElem
我现在无法提供代码,因为我目前正在脑海中研究这个想法并在互联网上四处乱逛。 我了解了进程间通信和使用共享内存在进程之间共享数据(特别是结构)。 但是,在对保存在不同 .c 文件中的程序使用 fork(
我想在用户集合中使用不同的功能。在 mongo shell 中,我可以像下面这样使用: db.users.distinct("name"); 其中名称是用于区分的集合字段。 同样我想要,在 C
List nastava_izvjestaj = new List(); var data_context = new DataEvidencijaDataContext();
我的 Rails 应用程序中有 Ransack 搜索和 Foundation,本地 css 渲染正常,而生产中的同一个应用程序有一个怪癖: 应用程序中的其他内容完全相同。 我在 Chrome 和 Sa
我是一名优秀的程序员,十分优秀!