class TImpl分析

/** 进程间下载
*/
class DLUTILS_EXPORT IpcDownloadClient
{
public:
	/** 默认构造函数
	*/
	IpcDownloadClient();

	/** 默认析构函数
	*/
	~IpcDownloadClient();

public:
	/** 连接服务器
	*/
	bool Connect(uint16_t port);

	/** 是否已经连接
	*/
	bool IsConnected();

	/** 与服务器断开连接
	*/
	void Stop();

	/** 获取指定id的游戏的任务状态
	*/
	IpcTaskStatus GetGameStatus(IpcTaskStatusInfo info);

	/** 启动下载任务或者安装任务
	*/
	uint32_t StartTask(IpcStartTaskInfo info);

	/** 暂停下载或安装任务
	*/
	bool PauseTask(uint32_t gameId);

	/** 删除任务
	*/
	bool CancelTask(uint32_t gameId);

	/** 获取已安装游戏信息
	*/
	IpcGameInstallInfo GetIpcGameInfo(uint32_t gameId);

	/** 为已完成安装的游戏创建快捷方式
	*/
	IpcShotcutInfo CreateShotcut(uint32_t gameId);

private:
	/** 实现对象
	*/
	class TImpl;
	TImpl *m_impl;
};

-------------------------------------------------------------------------------------

//</span>
//IpcDownloadClient class implementation

class IpcDownloadClient::TImpl
{
public:
	TImpl()
	{
	}

	SharedPtr<IpcClient> m_pManagerRpcClient;
};

IpcDownloadClient::IpcDownloadClient()
{
	m_impl = new TImpl;
}

IpcDownloadClient::~IpcDownloadClient()
{
	delete m_impl;
}

bool IpcDownloadClient::Connect(uint16_t port)
{
	try
	{
		assert(m_impl);
		m_impl->m_pManagerRpcClient.reset(new IpcClient);
		m_impl->m_pManagerRpcClient->Connect(port);
		return true;
	}
	catch (std::exception& e)
	{
		(void)(e);
#ifdef _DEBUG
		OutputDebugStringA(e.what());
#endif // _DEBUG
		return false;
	}
}

bool IpcDownloadClient::PauseTask(uint32_t gameId)
{
	if (!m_impl ||!m_impl->m_pManagerRpcClient || !IsConnected())
	{
		return false;
	}

	try
	{
		return IpcDownloadStub::PauseTask(m_impl->m_pManagerRpcClient.get(), gameId);
	}
	catch(const std::exception &e)
	{
		(void)(e);
#ifdef _DEBUG
		OutputDebugStringA(e.what());
#endif // _DEBUG
	}
	return false;
}

uint32_t IpcDownloadClient::StartTask(IpcStartTaskInfo info)
{
	IpcStartTaskStatus status = IPC_TASK_STATUS_FAILED;
	if (!m_impl ||!m_impl->m_pManagerRpcClient || !IsConnected())
	{
		return status;
	}

	try
	{
		return IpcDownloadStub::StartTask(m_impl->m_pManagerRpcClient.get(), info);
	}
	catch(const std::exception &e)
	{
		(void)(e);
#ifdef _DEBUG
		OutputDebugStringA(e.what());
#endif // _DEBUG
	}
	return status;
}

bool IpcDownloadClient::CancelTask(uint32_t gameId)
{
	if (!m_impl ||!m_impl->m_pManagerRpcClient || !IsConnected())
	{
		return false;
	}

	try
	{
		return IpcDownloadStub::CancelTask(m_impl->m_pManagerRpcClient.get(), gameId);
	}
	catch(const std::exception &e)
	{
		(void)(e);
#ifdef _DEBUG
		OutputDebugStringA(e.what());
#endif // _DEBUG
	}
	return false;
}

IpcTaskStatus IpcDownloadClient::GetGameStatus(IpcTaskStatusInfo info)
{
	if (!m_impl ||!m_impl->m_pManagerRpcClient || !IsConnected())
	{
		return IpcTaskStatus();
	}

	try
	{
		return IpcDownloadStub::GetIpcTaskStatus(m_impl->m_pManagerRpcClient.get(), info);
	}
	catch(const std::exception &e)
	{
		(void)(e);
#ifdef _DEBUG
		OutputDebugStringA(e.what());
#endif // _DEBUG
	}
	return IpcTaskStatus();
}

IpcGameInstallInfo IpcDownloadClient::GetIpcGameInfo(uint32_t gameId)
{
	if (!m_impl ||!m_impl->m_pManagerRpcClient || !IsConnected())
	{
		return IpcGameInstallInfo();
	}

	try
	{
		return IpcDownloadStub::GetIpcGameInfo(m_impl->m_pManagerRpcClient.get(), gameId);
	}
	catch(const std::exception &e)
	{
		(void)(e);
#ifdef _DEBUG
		OutputDebugStringA(e.what());
#endif // _DEBUG
	}
	return IpcGameInstallInfo();
}

IpcShotcutInfo IpcDownloadClient::CreateShotcut(uint32_t gameId)
{
	if (!m_impl ||!m_impl->m_pManagerRpcClient || !IsConnected())
	{
		return IpcShotcutInfo();
	}

	try
	{
		return IpcDownloadStub::CreateShotcut(m_impl->m_pManagerRpcClient.get(), gameId);
	}
	catch(const std::exception &e)
	{
		(void)(e);
#ifdef _DEBUG
		OutputDebugStringA(e.what());
#endif // _DEBUG
	}
	return IpcShotcutInfo();
}

void IpcDownloadClient::Stop()
{
	if (!m_impl ||!m_impl->m_pManagerRpcClient || !IsConnected())
	{
		return ;
	}
	m_impl->m_pManagerRpcClient.reset();
}

bool IpcDownloadClient::IsConnected()
{
	if (!m_impl || !m_impl->m_pManagerRpcClient)
	{
		return false;
	}
	return m_impl->m_pManagerRpcClient->IsConnected();
}

class TImpl;在类的头文件中进行定义,这样的做法我认为是

(1)“编译包含过多不必要的头文件”,这些头文件是实现时候使用的,没有必要包含在头文件中,如果别人使用你的类,那么他就需要将哪些和他无关的头文件也包含进来,违反了“强内聚”原则;特别是作为“基础库”使用的时候

(2)将一个类分成两个来使用,这样便有了层次,如果是简单的类,则第二层是对第一层简单的包装,隐藏了头文件而已,接口什么的都是抄过来的,主类无需附带任何私有数据和方法,仅仅作为一个壳子,Timpl类作为实体。若是复杂的类,则Timpl类复杂核心功能,主类负责协调功能,主类有自己的私有数据和方法,Timpl类有自己的方法和数据,并不是抄主类的接口,他们分工明确。


何时使用:

(1)主类头文件中头文件过多,需要隐藏实现细节

(2)主类比较复杂,分成两个类实现太牛刀,一个类太乱,可以使用Timpl分层,两者各有分工,可以隐藏一部分头文件,可以让层次清晰。


总结:

我觉得只有在主类头文件中“不必要的头文件”过多的时候才需要使用,而其他方面完全无需使用,若需要分层则应该调理清晰,分成多个类文件,不要I放在一起,混在一个CPP文件中很乱,而且意义不大,不如再分几个底层类来使用,即使底层类只有一个方法或接口,后期维护完全清晰明朗。


这还审核?! 失望,真不给力!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值