总的来说,你很好,因为它自己House
创建了成员变量m_room
——它不需要消费者在实例化后调用某些东西。这遵循项目在实例化后立即可用的模式(它不需要像设置房间或其他任何特殊操作)。
我确实有一些小问题:
class Room
{
public:
// virtual method to allow overriding
virtual void ColorRoom(){};
};
class House
{
public:
// Returning a non-const pointer in C++ is typically a bad smell.
const Room& Room() const { return m_room; }
// Allow for assignment and manipulating room, but breaks const-ness
Room& Room() { return m_room; }
// Facade method for houses with more than one room
// You can forward parameters or come up with room-painting schemes, but you should
// not require that a House has a Room called Room().
virtual void ColorAllRooms()
{
m_room.ColorRoom();
}
private:
Room m_room;
};