深入浅出设计模式原则之单一原则(SRP)
生活随笔
收集整理的這篇文章主要介紹了
深入浅出设计模式原则之单一原则(SRP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面以兩個例子來解釋一下單一原則(工程源代碼)
【案例1】用戶權限系統
設計案例用戶權限系統(用戶、機構、角色管理這些模塊),基本上使用的都是RBAC模型(基于角色的訪問控制,通過分配和取消角色來完成用戶權限的授予和取消,使動作主體(用戶)與資源的行為(權限)分離),確實是一個很好的解決方案。
? ? 傳統方法:
#include <QCoreApplication> #include <iostream> /*!* \brief The UserInfo class*/ class UserInfo{public:void SetUserID(std::string userID);std::string GetUserID();void SetUserName(std::string userName);std::string GetUserName();void SetUserPasswd(std::string userPasswd);std::string GetUserPasswd();void DeleteUser(std::string userName);void ChangeUserPasswd(std::string &oldUserPasswd,std::string &newUserPasswd);void AddOrg(int orgID);void AddRole(int roleID);private:std::string _user_id;std::string _user_name;std::string _user_passwd;};/*!* \brief UserInfo::SetUserID* \param userID*/ void UserInfo::SetUserID(std::string userID) {this->_user_id = userID; } /*!* \brief UserInfo::GetUserID* \return*/ std::string UserInfo::GetUserID() {std::cout<<this->_user_id.c_str()<<std::endl;return this->_user_id; } /*!* \brief UserInfo::SetUserName* \param userName*/ void UserInfo::SetUserName(std::string userName) {this->_user_name = userName; } /*!* \brief UserInfo::GetUserName* \return*/ std::string UserInfo::GetUserName() {std::cout<<this->_user_name.c_str()<<std::endl;return this->_user_name; } /*!* \brief UserInfo::SetUserPasswd* \param userPasswd*/ void UserInfo::SetUserPasswd(std::string userPasswd) {this->_user_passwd = userPasswd; } /*!* \brief UserInfo::GetUserPasswd* \return*/ std::string UserInfo::GetUserPasswd() {std::cout<<this->_user_passwd.c_str()<<std::endl;return this->_user_passwd; } /*!* \brief UserInfo::DeleteUser* \param userName*/ void UserInfo::DeleteUser(std::string userName) {} /*!* \brief UserInfo::ChangeUserPasswd* \param oldUserPasswd* \param newUserPasswd*/ void UserInfo::ChangeUserPasswd(std::string &oldUserPasswd, std::string &newUserPasswd) {} /*!* \brief UserInfo::AddOrg* \param orgID*/ void UserInfo::AddOrg(int orgID) {} /*!* \brief UserInfo::AddRole* \param roleID*/ void UserInfo::AddRole(int roleID) {}int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);UserInfo *myUserInfo = new UserInfo;myUserInfo->SetUserName("lili");myUserInfo->SetUserID("C-1");myUserInfo->SetUserPasswd("123456");myUserInfo->GetUserName();myUserInfo->GetUserID();myUserInfo->GetUserPasswd();delete myUserInfo;return a.exec(); }以上設計不符合單一原則,解決方法如下:
依據定義咱們可以再重新設計下,把用戶的信息抽取成一個業務對象(Business Object ,BO),把行為抽取成一個業務邏輯(Business Logic,Biz)
#include <QCoreApplication> #include <iostream> /*!* \brief The UserBo class*/ class UserBo{public:void SetUserID(std::string userID);std::string GetUserID();void SetUserName(std::string userName);std::string GetUserName();void SetUserPasswd(std::string userPasswd);std::string GetUserPasswd();public:std::string _user_id;std::string _user_name;std::string _user_passwd;}; /*!* \brief UserBo::SetUserID* \param userID*/ void UserBo::SetUserID(std::string userID) {this->_user_id = userID; } /*!* \brief UserBo::GetUserID* \return*/ std::string UserBo::GetUserID() {std::cout<<this->_user_id.c_str()<<std::endl;return this->_user_id; } /*!* \brief UserBo::SetUserName* \param userName*/ void UserBo::SetUserName(std::string userName) {this->_user_name = userName; } /*!* \brief UserBo::GetUserName* \return*/ std::string UserBo::GetUserName() {std::cout<<this->_user_name.c_str()<<std::endl;return this->_user_name; } /*!* \brief UserBo::SetUserPasswd* \param userPasswd*/ void UserBo::SetUserPasswd(std::string userPasswd) {this->_user_passwd = userPasswd; } /*!* \brief UserBo::GetUserPasswd* \return*/ std::string UserBo::GetUserPasswd() {std::cout<<this->_user_passwd.c_str()<<std::endl;return this->_user_passwd; } /*!* \brief The UserBiz class*/ class UserBiz{public:void DeleteUser(UserBo myUserBo);void ChangeUserPasswd(UserBo *myUserBo,std::string &oldUserPasswd,std::string &newUserPasswd);void AddOrg(UserBo myUserBo,int orgID);void AddRole(UserBo myUserBo,int roleID);};/*!* \brief UserBiz::DeleteUser* \param myUserBo*/ void UserBiz::DeleteUser(UserBo myUserBo) {} /*!* \brief UserBiz::ChangeUserPasswd* \param oldUserPasswd* \param newUserPasswd*/ void UserBiz::ChangeUserPasswd(UserBo *myUserBo,std::string &oldUserPasswd, std::string &newUserPasswd) {if (!myUserBo->_user_passwd.compare(oldUserPasswd)){myUserBo->_user_passwd = newUserPasswd;std::cout<<myUserBo->_user_passwd<<std::endl;}} /*!* \brief UserBiz::AddOrg* \param myUserBo* \param orgID*/ void UserBiz::AddOrg(UserBo myUserBo, int orgID) {} /*!* \brief UserBiz::AddRole* \param myUserBo* \param roleID*/ void UserBiz::AddRole(UserBo myUserBo, int roleID) {} /*!* \brief The NewUserInfo class*/ class UserInfo : public UserBo,public UserBiz{};int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);UserInfo *myUserInfo = new UserInfo;UserBo *myUserBo= (UserBo *)myUserInfo;myUserBo->SetUserName("lili");myUserBo->SetUserID("C-1");myUserBo->SetUserPasswd("123456");myUserBo->GetUserID();myUserBo->GetUserName();myUserBo->GetUserPasswd();UserBiz *myUserBiz = (UserBiz *)myUserInfo;myUserBiz->ChangeUserPasswd(myUserBo,myUserBo->_user_passwd,std::string("789012"));return a.exec(); }?
單一職責原則規定一個類應該有且僅有一個引起它變化的原因,否則類應該被拆分。
在實際應用中,類的設計盡量做到只有一個原因引起變化,接口一定要做到單一職責原則,方法一定要做到單一職責原則,即一個方法只實現一個功能。
參考文獻:
總結
以上是生活随笔為你收集整理的深入浅出设计模式原则之单一原则(SRP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王北车的陷阱好在哪?
- 下一篇: 新能源汽车显示充电系统异常是怎么回事?