单一职责:
一个类只能有一个职责,这个职责应当是明确的,完整的。就是说,一个类只能干一件事,并且具备干这件事所需要的所有方法。
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命令执行的时候对路径进行解析。
避免该问题的方法:
需求分析应当尽量详细,设计应当考虑充分细致。