我有一个使用内部Builder
对象实例化复杂对象的构造函数。数据结构中的五个成员是指针类型。但是,当对象被销毁时,使用这种模式我会遇到问题。以下是我的构造函数的样子,带有成员初始化列表:
Player::Player(const Builder& builder)
:m_name(builder._name)
,m_description(builder._description)
,m_primaryAttributes(builder._primaryAttributes)
,m_abilityAttributes(builder._abilityAttributes)
,m_armor(builder._armor)
,m_weapon(builder._weapon)
,m_inventory(new ComponentMap())
{}
正如预期的那样,客户端代码运行良好:
Player* player = Player::Builder()
.name("Dylan")
.description("Super bad-ass hero of the game")
.primaryAttributes(createPrimaryAttributes())
.abilityAttributes(createAbilityAttributes())
.weapon(createWeapon())
.armor(createArmor())
.build();
但是,如果我忽略消息链中的一个参数,然后销毁我的Player
对象,就会发生不好的事情:
Player* player = Player::Builder()
.name("Dylan")
.description("Super bad-ass hero of the game")
.primaryAttributes(createPrimaryAttributes())
.abilityAttributes(createAbilityAttributes())
.armor(createArmor())
.build();
// ...
delete player;
// ...
// cleanMemory() gets called in Player::~Player()
void Player::cleanMemory()
{
if(m_primaryAttributes != NULL )
delete m_primaryAttributes;
if(m_abilityAttributes != NULL )
delete m_abilityAttributes;
if(m_inventory != NULL )
delete m_inventory;
if(m_weapon != NULL) // oops, bad stuff happens here
delete m_weapon;
if(m_armor != NULL)
delete m_armor;
}
显然,发生这种情况是因为武器的指针没有被初始化为任何一个对象NULL
或对象的实例Weapon
。如果从链中省略了一种方法,则构造函数似乎也不允许使用默认值NULL
(至少从我所见) 。Builder
现在,客户端必须要么给出Weapon
一个指向对象的指针,要么给出NULL
一个对象的实例。
有没有办法在不Builder
完全修改这个构造函数的情况下解决这个问题?或者,是否应该使用另一种模式进行重构,例如Factory
, 并返回到带有位置参数列表的常规构造函数?