龙哥网

龙哥网

c++代码示例大全(简单好玩的编程代码)(c语言代码简单例子)
2022-03-24

策略模式就是准备一组算法,并将每一个算法封装起来,使得他们可以互换(这儿的关键就是算法的逻辑抽象,接口封装到一个类中,再通过委托的方式将具体的算法实现委托给具体的类来实现)

对称加密速度快加密大数据块文件特点,加密密钥和解密密钥是一样的

非对称加密,加密速度慢、加密强度高高,安全性特点,加密密钥和解密密钥不一样

#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;
}
免责声明
本站部分资源来源于互联网 如有侵权 请联系站长删除
龙哥网是优质的互联网科技创业资源_行业项目分享_网络知识引流变现方法的平台为广大网友提供学习互联网相关知识_内容变现的方法。