设计模式--单一职责原则的个人理解

单一职责:
一个类只能有一个职责,这个职责应当是明确的,完整的。就是说,一个类只能干一件事,并且具备干这件事所需要的所有方法。
1.一个好的例子

class CmdFactory {
private:
	std::string mCmdString;
	CmdSplit* mCmdSplit;
	CmdTypeAnalyze* mCmdTypeAnalyze;
	CmdParaAnalyze* mCmdParaAnalyze;
	CmdPathAnalyze* mCmdPathAnalyze;
public:
	CmdFactory();
	CmdFactory(std::string cmdString);
	~CmdFactory();

	CmdSplit*GetCmdSplit();
	CmdTypeAnalyze* GetCmdTypeAnalyze();
	CmdParaAnalyze* GetCmdParaAnalyze();
	CmdPathAnalyze* GetCmdPathAnalyze();
	CmdBase* CmdFactory::CreateCmd(Cmd cmd,CmdPara cmdPara,std::list<CmdPath> cmdPathList,FileOrFolder * rootPath,FileOrFolder ** workingFolder);
};

cmd工厂的职责是创建cmd命令:

CmdBase* CmdFactory::CreateCmd(Cmd cmd,CmdPara cmdPara,std::list<CmdPath> cmdPathList,FileOrFolder * rootPath,FileOrFolder ** workingFolder);

该cmd工厂具备创建cmd命令所需要的所有机器:

	CmdSplit* mCmdSplit;
	CmdTypeAnalyze* mCmdTypeAnalyze;
	CmdParaAnalyze* mCmdParaAnalyze;
	CmdPathAnalyze* mCmdPathAnalyze;

2.一个不好的例子

class CmdPathAnalyze{
private:
	std::list<CmdElement> mCmdElementList;
	std::list<CmdPath> mCmdPathList;
	std::list<FileOrFolder*> mCmdPathLinkList;//用于存放路径指向的文件或文件夹对象的指针
public:
	CmdPathAnalyze(std::list<CmdElement> cmdElementList);
	~CmdPathAnalyze();
	std::list<CmdElement> GetCmdElementList();
	std::list<CmdPath> GetCmdPathList();
	std::list<FileOrFolder*>  GetCmdPathLinkList();
	void CmdPathHeadAnalyzeAndPathSplit(CmdPara cmdPara);
protected:
	int IsCmdPathExsit(int cmdType,FileOrFolder * rootPath,FileOrFolder * workingFolder);
	bool IsStringMatch(std::string myString,std::string myPattern);
	void IsPathMatch(std::list<std::string> cmdPathElementList,bool& isCmdPathExist,FileOrFolder * pathPtr);
	std::list<std::string>  CmdPathAnalyze::CmdPathSplit(int deleteCharNum,std::string pathString);
public:
	bool NonePathIsExsit(std::list<CmdPath> cmdPathList);
	bool PathStartWithAtIsExsit(std::list<CmdPath> cmdPathList);
	bool IsAllPathExsit(std::list<CmdPath> cmdPathList,FileOrFolder * rootPath,FileOrFolder ** workingFolder);
	bool IsPathExsit(CmdPath cmdPath,FileOrFolder * rootPath,FileOrFolder ** workingFolder);
};

cmdPathAnalyze用于分析路径,并且可以完成分析路径的所有工作。

class CmdBase
{
protected:
	Cmd mCmd;
	CmdPara mCmdPara;
	std::list<CmdPath> mCmdPathList;
	FileOrFolder * mRootPath;
	FileOrFolder ** mWorkingFolder;
public:
	CmdBase();
	virtual ~CmdBase();
	virtual int Execute() =0;
protected:
	FileOrFolder* TraverseTargetPath(const CmdPath targetPath); //遍历路径,找到路径指向的文件
	std::list<FileOrFolder*> TraverseTargetPathList(); //遍历路径列表,返回路径指向文件列表
	FileOrFolder*  IsPathMatch(std::list<std::string> cmdPathElementList,bool& isCmdPathExist,FileOrFolder * pathPtr);
	FileOrFolder * IsNameExsit(std::string name,FileOrFolder * targetFolder,bool &isExsit );

	bool IsStringMatch(std::string myString,std::string myPattern);
	std::string ResumeRealPath(CmdPath cmdPath);
	bool IsNameValid(std::string name);
	void DeleteFolders(std::list<FileOrFolder*> folderList,CmdPara cmdPara);
	void CreateFileOrLink(FileOrFolder* ite,FileOrFolder* secondFloder);
};

cmdBase用于作为cmd命令的基类使用。但是该类中包含路径分析的方法,这些方法应当属于cmdPathAnalyze,所以,cmdBase的职责划分就不太清晰,职责不单一。应当在cmdBase中包含一个cmdPathAnalyze对象,通过该对象对路径进行分析。

问题原因:
设计的时候未对路径分析考虑清楚,在cmd工厂中未对路径完全分析,因此需要在具体cmd命令执行的时候对路径进行解析。

避免该问题的方法:
需求分析应当尽量详细,设计应当考虑充分细致。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiZhi_BUAA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值