我想要一个类模板template<std::size_t N> Shape
,其中模板参数N
代表Shape
. 应该有数量有限的预定义Shape
s,例如Shape<2> SQUARE
,Shape<3> CUBE
和Shape<3> SPHERE
。将来我可能会添加更多预定义Shape
的 s。
我希望Shape
对象仅可构造为任何预定义Shape
的 s。由于这些预定义Shape
s 的属性始终保持不变,因此最好只将它们存储一次,并让新Shape
对象引用它们。
此刻,我有以下实现:
// Flag for the possible shapes
enum class Tag
{
SPHERE,
CUBE,
SQUARE
};
template<std::size_t N>
class Shape
{
public:
// Predefined shapes.
static const Shape<3> SPHERE;
static const Shape<3> CUBE;
static const Shape<2> SQUARE;
// Information stored about the given shapes
const Tag tag; // tag specifying the shape
const double v; // Shape volume/area
const std::array<double, 2*N> surrounding_box; // Storing intervals for a surrounding box
//... Some other information that depends on template parameter N
private:
// Private constructor. This prevents other, unintended shapes from being created
Shape(Tag tag, double v, const std::array<double, 2*N> surrounding_box):
tag{tag}, v {v}, surrounding_box {surrounding_box} {};
};
// Initialization of predefined shape: SPHERE
template<std::size_t N>
const Shape<3> Shape<N>::SPHERE(Tag::SPHERE, 3.0,{{0.0,2.7,0.0,2.7,0.0,2.7}});
// Initialization of predefined shape: CUBE
template<std::size_t N>
const Shape<3> Shape<N>::CUBE(Tag::CUBE, 1.0,{{0.0,1.0,0.0,1.0,0.0,1.0}});
// Initialization of predefined shape: SQUARE
template<std::size_t N>
const Shape<2> Shape<N>::SQUARE(Tag::SQUARE, 1.0,{{0.0,1.0,0.0,1.0}});
这个实现有几个问题:
- 的每个实例都
Shape
包含所有预定义的 s(如本问题Shape
的评论中所指出的); Shape
在创建的每个实例之前,Shape
都会复制预定义 s 的内容;- 甚至一个
Shape<3>
对象也包含Shape<2> SQUARE
. - ...
我想知道实现上述目标的更好设计模式是什么。我正在考虑使用Tag
作为构造函数参数并使用某种工厂。但是,由于模板的复杂性以及我只希望预定义Shape
的 s 是可构造的,我无法正确获取实现细节。