C++设计模式-单一职责原则

单一职责原则定义了类的*++封装规范++***。(封装、继承、多态)

++定义:有且仅有一个引起类变化的原因。++

e.g.1.一个需要重新封装的类

cpp 复制代码
class A {
public:
	void funName() {}
	void funSex() {}
	void funAge() {}
	void funCourse() {}//要重新封装到其他类
	void funTeacher() {}//要重新封装到其他类
	void funProject() {}//要重新封装到其他类
private:
	std::string m_strName, m_strSex;
	int m_nAge;
	std::string m_Course1, m_TeacherName,m_ProjectName;//要重新封装到其他类
};

e.g.2不可散乱为全局变量

cpp 复制代码
std::string g_strName, g_strSex;
int g_nAge;
std::string g_Course1, g_TeacherName, g_ProjectName;
void funName() {}
void funSex() {}
void funAge() {}
void funCourse() {}
void funTeacher() {}
void funProject() {}
相关推荐
geovindu8 小时前
rust: Simple Factory Pattern(续)
后端·设计模式·rust·简单工厂模式·创建型模式
喜欢coding的谢同学9 小时前
Go语言常用设计模式基础和实践案例介绍
开发语言·设计模式·golang
sarasuki1 天前
Agent 的记忆有几种?你的名字是?
人工智能·设计模式·agent
geovindu1 天前
rust: Flyweight Pattern
开发语言·后端·设计模式·rust·享元模式·结构型模式
沫璃染墨2 天前
《Linux工程实践篇(一):认识设计模式——从日志系统看策略模式》
linux·c++·安全·设计模式·策略模式
十六宿舍2 天前
【软件设计模式-第一课:基础知识了解】
设计模式·架构·面向对象·软件架构
geovindu2 天前
rust: Facade Pattern
开发语言·后端·设计模式·rust·外观模式
怕浪猫2 天前
面试官让我手写一个 Tool Use,我用了 3 种方案
设计模式·面试·程序员
geovindu3 天前
rust: Abstract Factory pattern
开发语言·后端·设计模式·rust·抽象工厂模式
Carl_奕然3 天前
【智能体】Loop 的四种设计模式之:Event-Driven Loop(2026 最新版)
人工智能·驱动开发·python·设计模式