面向过程通过划分功用模块,通过函数彼此间的挪用来实现,当需求变革时,就需要更改函数。而你改动的函数有几的处所再挪用他联系关系几数据,那是很不容易弄得清晰的处所。

通过选择适宜得设想形式能够进步代码的可复用性,可维护性,可扩展性以及灵敏性,那些都是系统设想中十分重要的非功用性需求。

几大设想原则:

单一职责:关于一个类来说,应该仅有一个引起他变革的原因,功用要单一,降低耦合性。

开放-封锁原则:关于扩展开放,关于更改时封锁的

依赖倒转原则:高层模块不该该依赖低层模块,应该都依赖笼统;笼统不该该依赖细节,细节应该依赖笼统。

里氏替代原则:子类必需可以替代掉他们的父类;子类继承父类,所以子类拥有父类所有非私有办法。

迪米特法例:强调了类之间的松耦合。若是两个类没必要相互间接通信,那么那两个类就不该该发作间接的彼此感化。若是此中一个类需要挪用另一个类的某一个办法的话,能够通过圈外人转发那个挪用。

笼统工场形式

供给一个创建一系列或相关依赖对象的接口,而无需指定他们详细的类。很容易交换差别的产物,他让详细的创建实例过程与客户端别离,客户端是通过它们的笼统接口操做实例,产物的详细类名也被详细工场的实现别离。

#include <iostream>using namespace std;class User {PRivate: int id; string name;public:};class Department {private: int did; string dname;public:};//用户表笼统类class Iuser {public: virtual void insert(User* user) = 0; virtual User* getUser(int id) = 0;};//sql用户详细类class SqlserverUser :public Iuser {public: void insert(User* user) { cout << "在SQL Server 中给User表增加一笔记录\n"; } User* getUser(int id) { cout << "在SQL Server 中查到User一笔记录\n"; return nullptr; }};//access用户详细类class AccessUser :public Iuser { void insert(User* user) { cout << "在Acess Server 中给User表增加一笔记录\n"; } User* getUser(int id) { cout << "在Acess Server 中查到User一笔记录\n"; return nullptr; }};//部分笼统类class IdDepartment {public: virtual void insert(Department* dept) = 0; virtual User* getUser(int id) = 0;};//sql部分详细类class SqlserverDepartment :public IdDepartment {public: void insert(Department* dept) { cout << "在SQL Server 中给Department表增加一笔记录\n"; } User* getUser(int id) { cout << "在SQL Server 中查到Department一笔记录\n"; return nullptr; }};//access部分详细类class AccessDepartment :public IdDepartment { void insert(Department* deptr) { cout << "在Acess Server 中给Department表增加一笔记录\n"; } User* getUser(int id) { cout << "在Acess Server 中查到Department一笔记录\n"; return nullptr; }};//笼统工场类class IFactory {public: virtual Iuser* CreateUser() = 0; virtual IdDepartment* CreateDepartment() = 0;};//sql详细工场类class SqlserverFactory :public IFactory {public: Iuser* CreateUser() { return new SqlserverUser; } IdDepartment* CreateDepartment() { return new SqlserverDepartment; }};//access详细工场类class AcessserverFactory :public IFactory {public: Iuser* CreateUser() { return new AccessUser; } IdDepartment* CreateDepartment() { return new AccessDepartment; }};int main() { User* user = new User; Department* dept = new Department; //利用sql,就实例化sql工场对象 IFactory* factory = new SqlserverFactory; Iuser* iu = factory->CreateUser(); iu->insert(user); iu->getUser(1); IdDepartment* de = factory->CreateDepartment(); de->insert(dept); de->getUser(2); return 0;}战略形式

战略形式:定义一系列的算法,把他们一个个封拆起来,而且使他们可彼此替代,详细实现与客户端完全别离,客户端只需要操做一个类就能够实现差别功用

长处:1、削减了算法与利用算法之间的耦合度。 2、测试便利,每个算法都有本身的类,只要测试本身接口就行了,不影响其他算法类。

利用场景: 在阐发过程中需要在差别时间应用差别的营业规则,能够考虑用战略形式。

#include <iostream>#include <string>using namespace std;//战略笼统类,定义所有撑持的算法的公共接口class CashSuPEr {public: virtual double acceptCash(double money) { return money; }};//详细战略类class NormalFee :CashSuper {public: double acceptCash(double money) { return money; }};class DiscountFee :CashSuper {public: float discountNum;public: DiscountFee(float discountNum) { this->discountNum = discountNum; cout << endl; } double acceptCash(double money) { return money * this->discountNum; }};class ReturnFee :CashSuper {public: int fill; int send;public: ReturnFee(int fill, int send) :fill(fill), send(send) {} double acceptCash(double money) { if (money > this->fill) { return money - send; } return money; }};//用一个详细战略来设置装备摆设维护一个对战略对象的引用class CashContext {private: CashSuper* cs = nullptr;public: CashContext(int type) { switch (type) { case 0: cs = (CashSuper*) new NormalFee; break; case 1: cs = (CashSuper*)new DiscountFee(0.8); break; case 2: cs = (CashSuper*) new DiscountFee(0.7); break; case 3: cs = (CashSuper*) new DiscountFee(0.5); break; case 4: cs = (CashSuper*) new ReturnFee(300, 100); break; } } //按照差别战略对象来实现差别成果 double getResult(double money) { return cs->acceptCash(money); }};int main(){ const char* discount[5] = { "一般价格","打八折","打七折","打五折" }; //实例化打八折类 CashContext cc(1); //获取打折后金额 double res = cc.getResult(100); cout << res << endl; return 0;}单例形式

单例形式:包管一个类仅有一个实例,并供给一个拜候他的全局拜候点。整个系统只要一个全局对象

#include <iostream>using namespace std;//懒汉式单例形式class SingletonLazy {private: static SingletonLazy* instance; // 构造函数私有化, 不让外界操纵new创建实例 SingletonLazy() { cout << "懒汉式单例形式\n"; }public: static SingletonLazy* getInstance() { //第一次引用时才被实例化 if (instance == nullptr) { instance = new SingletonLazy; } return instance; }};SingletonLazy* SingletonLazy::instance = nullptr;//饿汉式单例形式class SingletonHungry {private: static SingletonHungry* instance2; SingletonHungry() { cout << "饿汉式单例形式\n"; }public: static SingletonHungry* getInstance() { return instance2; }};//加载时实例化SingletonHungry* SingletonHungry::instance2 = new SingletonHungry;int main() { cout << "action: \n"; return 0;}粉饰器形式

动态地给一个类添加一些额外的功用。就增加功用来说,粉饰器形式比拟生成子类愈加灵敏

#include <iostream>#include <string>using namespace std;//笼统接口,能够给那些对象动态添加功用class AbstractPerson {public: string username; string heroname; int mHp; int mMp; int mAt; int mDf;public: virtual void show() = 0;};//详细对象class Person :public AbstractPerson {public: Person(string username,string heroname) { this->username = username; this->heroname = heroname; mHp = 0; mMp = 0; mAt = 0; mDf = 0; } virtual void show() { cout << "用户名:" << username << endl; cout << "英雄名:" << heroname << endl; cout << "血量:" << mHp << endl; cout << "魔法:" << mMp << endl; cout << "攻击:" << mAt << endl; cout << "防御:" << mDf << endl; }};//粉饰类,通过继承体例来实现添加额外功用class Bazhechongzhuang : public AbstractPerson {public: AbstractPerson* per;public: Bazhechongzhuang(AbstractPerson* per) :per(per){} void Add() { cout << "买了霸者重拆后:" << endl; this->mHp = this->per->mHp; this->mMp = this->per->mMp; this->mAt = this->per->mAt; this->mDf = this->per->mDf + 800; } virtual void show() { Add(); cout << "用户名:" << username << endl; cout << "英雄名:" << heroname << endl; cout << "血量:" << this->mHp << endl; cout << "魔法:" << this->mMp << endl; cout << "攻击:" << this->mAt << endl; cout << "防御:" << this->mDf << endl; }};//粉饰类class Fanjia : public AbstractPerson {public: AbstractPerson* per;public: Fanjia(AbstractPerson* per) :per(per) {} void Add() { cout << "买了反甲后:" << endl; this->mHp = this->per->mHp; this->mMp = this->per->mMp; this->mAt = this->per->mAt + 300; this->mDf = this->per->mDf + 500; } virtual void show() { Add(); cout << "血量:" << this->mHp << endl; cout << "魔法:" << this->mMp << endl; cout << "攻击:" << this->mAt << endl; cout << "防御:" << this->mDf << endl; }};int main() { AbstractPerson* per = new Person("Timel","典韦"); per->show(); cout << "-------------没买配备------------\n"; Bazhechongzhuang bzcz(per); Fanjia fj(per); fj.show(); return 0;}号令形式

将一个恳求封拆为一个对象,从而使你可用差别的恳求对客户停止参数化

#include <iostream>using namespace std;//详细的施行恳求class Request {public: void Get() { cout << "Get!\n"; } void Post() { cout << "Post!\n"; }};//用来声明施行操做的笼统接口class Command {protected: Request request;public: Command() {} Command(Request& res) :request(res) {} virtual void excuteCommand() {};//施行};//详细施行动做class GetCommand :public Command {public: GetCommand(Request res) :Command(res) {} void excuteCommand() { request.Get(); }};class PostCommand :public Command {public: PostCommand(Request res) :Command(res) {} void excuteCommand() { request.Post(); }};//详细施行哪个施行动做class Excutor {private: Command* comm;public: void setOrder(Command* comm) { this->comm = comm; } void modify() { comm->excuteCommand(); }};int main() { Request boy; Command *get = new GetCommand(boy); Command *post = new PostCommand(boy); Excutor girl; girl.setOrder(get); girl.modify(); girl.setOrder(post); girl.modify(); return 0;}外不雅形式

为子系统中的一组接口供给一个一致的界面,外不雅形式定义了一个高层接口,那个接口使得那一系统愈加容易利用。

#include <iostream>using namespace std;class Stock1 {//实现子系统的功用public: void buy() { cout << "买入股票1\n"; } void sell() { cout << "卖出股票1\n"; }};class Stock2 {public: void buy() { cout << "买入股票2\n"; } void sell() { cout << "卖出股票2\n"; }};class Stock3 {public: void buy() { cout << "买入股票3\n"; } void sell() { cout << "卖出股票3\n"; }};//晓得哪些子系统类负责处置恳求,将客户的恳求代办署理恰当的子系统对象class Fund {private: Stock1* sk1; Stock2* sk2; Stock3* sk3;public: Fund() { sk1 = new Stock1; sk2 = new Stock2; sk3 = new Stock3; } void buyFund() { sk1->buy(); sk2->buy(); sk3->buy(); } void sellFund() { sk1->sell(); sk2->sell(); sk3->sell(); }};int main() { Fund f; f.buyFund(); f.sellFund(); return 0;}代办署理形式

为其他对象供给一种代办署理以控造对那个对象的拜候。

#include <iostream>using namespace std;class SchoolGirl {public: string name;public: SchoolGirl() {} SchoolGirl(const char* name) :name(name) {}};//实在对象和代办署理的共用接口class AbstractPeople {public: virtual void giveDolls() = 0; virtual void giveFlowers() = 0; virtual void giveChocolate() = 0;};//实在对象class Pursuit:AbstractPeople {private: SchoolGirl *mm;public: Pursuit() {} Pursuit(SchoolGirl* sg) :mm(sg) {} void giveDolls() { cout << mm->name << "送你布娃娃" << endl; } void giveFlowers() { cout << mm->name << "送你鲜花" << endl; } void giveChocolate() { cout << mm->name << "送你巧克力" << endl; }};//代办署理人class Proxy :AbstractPeople {private: Pursuit* ps;public: Proxy(SchoolGirl* sg) { ps = new Pursuit(sg); } void giveDolls() { ps->giveDolls(); } void giveFlowers() { ps->giveFlowers(); } void giveChocolate() { ps->giveChocolate(); }};int main() { SchoolGirl* sg = new SchoolGirl("娇娇"); Proxy ps(sg); ps.giveDolls(); ps.giveFlowers(); ps.giveChocolate(); return 0;}