策略模式就是準備一組演算法,並將每一個演算法封裝起來,使得他們可以互換(這兒的關鍵就是演算法的邏輯抽象,介面封裝到一個類中,再通過委託的方式將具體的演算法實現委託給具體的類來實現)
對稱加密速度快加密大數據塊文件特點,加密密鑰和解密密鑰是一樣的
非對稱加密,加密速度慢、加密強度高高,安全性特點,加密密鑰和解密密鑰不一樣
#include<iostream>
using namespace std;
class Strategy
{
public:
virtual void crypy() = 0;
};
class AES :public Strategy
{
public:
virtual void crypy()
{
cout << "AES加密演算法 " << endl;
}
};
class DES :public Strategy
{
public:
virtual void crypy()
{
cout << " DES加密演算法" << endl;
}
};
class Context
{
public:
void setStrategy(Strategy *strategy)
{
this->strategy = strategy;
}
void myoperator()
{
strategy->crypy();
}
private:
Strategy *strategy;
};
void main()
{
//
DES *des = new DES;
des->crypy();
delete des;
Strategy*strategy = NULL;
strategy= new DES;
Context *context = new Context;
context->setStrategy(strategy);
context->myoperator();
delete strategy;
delete context;
system("pause");
return;
}中介者模式就是定義一個中介對象,未封裝系列對象之間的交互,終結者是各個對象不需要顯示的相互調用,從而使其耦合性鬆散,而且可以獨立的改變他們之間的交互
中介者問題拋出
#include<iostream>
using namespace std;
#include"string"
class Person
{
public:
Person(string name, int sex, int condi)
{
m_name=name;
m_sex=sex;
m_condi = condi;
}
string getName()
{
return m_name;
}
int getSex()
{
return m_sex;
}
int getCondi()
{
return m_condi;
}
protected:
string m_name;
int m_sex;
int m_condi;
};
class Women :public Person
{
public:
Women(string name, int sex, int condi) :Person(name, sex, condi)
{
}
virtual void getParter(Person*p)
{
if (this->m_sex == p->getSex())
{
cout << "我不是同性戀..(這裡就是問題研究,不帶任何感情色彩)" << endl;
}
if (this->getCondi() == p->getCondi())
{
cout << this->getName() << "和" << p->getName() << "絕配" << endl;
}
else
{
cout << this->getName() << "和" << p->getName() << "bu配" << endl;
}
}
};
class Man :public Person
{
public:
Man(string name, int sex, int condi) :Person(name, sex, condi)
{
}
virtual void getParter(Person*p)
{
if (this->m_sex == p->getSex())
{
cout << "我不是同性戀..(這裡就是問題研究,不帶任何感情色彩)" << endl;
}
if (this->getCondi() == p->getCondi())
{
cout << this->getName() << "和" << p->getName() << "絕配" << endl;
}
else
{
cout << this->getName() << "和" << p->getName() << "bu配" << endl;
}
}
};
void main()
{
Person *xiaofang = new Women("小芳", 2, 5);
Person *zhangsan = new Man("張三", 1, 4);
Person *lisi = new Man("李四", 2, 5);
xiaofang->getParter(zhangsan);
xiaofang->getParter(lisi);
system("pause");
return;
}中介者代碼實現
#include<iostream>
using namespace std;
#include"string"
class Person
{
public:
Person(string name, int sex, int condi, Mediator*m)
{
m_name = name;
m_sex = sex;
m_condi = condi;
mediator = m;
}
string getName()
{
return m_name;
}
int getSex()
{
return m_sex;
}
int getCondi()
{
return m_condi;
}
protected:
string m_name;
int m_sex;
int m_condi;
Mediator mediator;
};
class Mediator//中介這的抽象父類
{
public:
virtual void getParter() = 0;
void setMan(Person*pMan)
{
pMan = man;
}
void setWomen(Person*pMan)
{
pWomen = women;
}
public:
virtual void getParter()
{
if (pWomen->getSex() == pMan->getSex())
{
cout << "我不是同性戀..(這裡就是問題研究,不帶任何感情色彩)" << endl;
}
if (pWomen->getCondi() == pMan->getCondi())
{
cout << pWomen->getName() << "和" << pMan->getName() << "絕配" << endl;
}
else
{
cout << pWomen->getName() << "和" << pMan->getName() << "bu配" << endl;
}
}
private:
Person *pWomen;
Person *pMan;
};
class Women :public Person
{
public:
Women(string name, int sex, int condi, Mediator*m) :Person(name, sex, condi,m)
{
}
public:
virtual void getParter(Person*p)
{
mediator->setMan(p);
mediator->setWomen(this);
mediator->getParter();
}
};
class Man :public Person
{
public:
Man(string name, int sex, int condi, Mediator*m) :Person(name, sex, condi,m)
{
}
public:
virtual void getParter(Person*p)
{
mediator->setMan(this);
mediator->setWomen(p);
mediator->getParter();
}
};
void main()
{
Mediator *m = new Mediator;
Person *xiaofang = new Women("小芳", 2, 5,m);
Person *zhangsan = new Man("張三", 1, 4,m);
Person *lisi = new Man("李四", 2, 5,m);
xiaofang->getParter(zhangsan);
xiaofang->getParter(lisi);
system("pause");
return;
}原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/226006.html
微信掃一掃
支付寶掃一掃