福州网站开发cms,报网站开发培训班,wordpress过时了,合肥做网站找哪家好单例模式
单例模式保证一个类只能创建一个对象#xff0c;并提供全局访问点。通常用于全局共享例如日志、数据库连接池等。
Lazy Initialization 优点#xff1a;需要时才初始化#xff0c;节省空间 缺点#xff1a;线程不安全
class Singleton{
private:static Singlet…单例模式
单例模式保证一个类只能创建一个对象并提供全局访问点。通常用于全局共享例如日志、数据库连接池等。
Lazy Initialization 优点需要时才初始化节省空间 缺点线程不安全
class Singleton{
private:static Singleton* instance;//提供的静态成员Singleton() {}//重写构造函数为私有禁止使用通常方法创建Singleton
public://外界只能通过getInstance去获取静态成员从而保证只有一个实例ststic Singleton* getInstance(){if(instance nullptr)instance new Singleton();//可能会有多个线程同时执行这句导致问题return instance;}
}
Eager Initialization 优点线程安全 缺点不管用不用都会初始化占空间
class Singleton{
private:static Singleton* instance;Singleton(){}
public:static Singleton* getInstance(){return instance;}
}Singleton* Singleton::instance new Singleton();
Double-Checked Locking 优点延迟加载、线程安全 缺点没有
#include mutexclass Singleton{
private:static volatile Singleton* instance;static std::mutex mtx;Singleton(){}
public:static volatile Singleton* getInstance(){if(instance nullptr){std::lock_guardstd::mutex lock(mtx);if(instance nullptr){ //double-checked lockinginstance new Singleton();}}return instance; }
};volatile Singleton* Singleton::instance nullptr;
std::mutex Singleton::mtx;
代理模式
我要租房我不去租找了个中介去租。
class Person{
public:virtual void rentHouse() 0;//纯虚函数
}class I : public Person{
public:void rentHouse(){std::cout I want to rent a house.endl;}
};class Intermediary : public Person{
public: Intermediary(Person * person) : m_person(person) {}void rentHouse(){m_person-rentHouse();std::cout Iam Intermediary.endl;}
private:Person *m_person;
};void testDalegate()
{Person *i new I();Person *intermediary new Intermediary(i);intermediary-rentHouse();
}
简单工厂模式
简单工厂模式是一种实例化对象的方式只要我们输入的实例化信息就可以通过工厂方式实例化相应的实例化对象。
class TelPhone{
public:enum PhoneType{Mi, Oppo, Huawei};virtual void setName(std::string name) 0;virtual void setPrice(double price) 0;virtual double getPrice() 0;
protected:std::string name;double price;
};class MiPhone : public TelPhone{
public: Miphone(){setname(mi15);setprice(1234)}std::string getName(){return TelPhone::name;}std::double getPrice(){return TelPhone::Price;}void setName(std::string name){TelPhone::name name;}void setPrice(double price){TelPhone::price price;}};
class OppoPhone : public TelPhone{//...
};
class HuaWeiPhone : public TelPhone{//...
};class TelPhoneFactory{
public://生产手机static TelPhone* productTelPhone(TelPhone::PhoneType phoneType){TelPhone *telp nullptr;swith(phoneType){case TelPhone::Mi:telp new Miphone();break;case TelPhone::Oppo:telp new Oppophone();break;case TelPhone::Huawei:telp new Huaweiphone();break;default:break;}return telp;}
};void testFactory(){TelPhone *telp TelPhoneFactory::productTelPhone(TelPhone::Mi);if(telp ! nullptr){std::couttelp-getName() std::endl;std::couttelp-getPrice() std::endl;}
}
观察者模式
又叫发布-订阅模式定义对象间一对多的关系当一个对象状态发生变化时其所有依赖者都会收到通知
原型模式
用一个以及创建的实例作为原型通过复制该原型对象来创建一个和原型相同或者相似的对象。类似原件和复印件的关系。
策略模式
定义一系列算法把他们一个个封装起来并且使他们可以相互替换。通过策略模式可以动态的选择、配置和切换算法而无需修改客户端代码。
#includeiostreamenum StrategyType{E_StrategyNight,E_StraegyWeekend,
};
class Strategy{
public:virtual void algorithm() 0;virtual ~Strategy(){}
};class StrategyNight : public Strategy{
public:void algorithm(){std::cout 晚上加班计算方法std::endl;}virtual ~StrategyNight(){}
};class StraegyWeekend : public Strategy{
public:void algorithm(){std::cout周末加班std::endl;}virtual ~StraegyWeekend(){}
};class Context{
public:Context(StrategyType strategyType){switch (strategyType){case E_StrategyNight:pStrategy new StrategyNight();break;case E_StraegyWeekend:pStrategy new StraegyWeekend();break;default:break;}}~Context(){if(pStrategy)delete pStrategy;}void overtimePay(){if(pStrategy){pStrategy-algorithm();}}
private:Strategy* pStrategy;
};int main(){Context *pcont new Context(E_StraegyWeekend);pcont-overtimePay();if(pcont)delete pcont;return 0;
}
中介者模式
定义一个中介对象来封装一系列对象之间的交互使得原有对象之间的耦合松散且可以独立地改变他们之间的交互。 #includeiostream
#includevector
#includestring
using namespace std;class Employee{
private:string m_strName;string m_strContent;
public:Employee(string strName) : m_strName(strName){}void setName(string strName){m_strName strName;}string getName(){return m_strName;}void setContent(string content){m_strContent content;}string getContent(){if(m_strContent.empty())return 收到;return m_strContent;}virtual void talk() 0;
};class Boss : public Employee{
public:Boss(string str) :Employee(str){}void talk(){cout getName() says: getContent()endl;}
};class Manager : public Employee{
public:Manager(string str) :Employee(str){}void talk(){cout getName() says: getContent()endl;}
};
class Securtity: public Employee{
public:Securtity(string str) :Employee(str){}void talk(){cout getName() says: getContent()endl;}
};class Mediator{
protected:vectorEmployee* vec_emp;public:void addEmployee(Employee* emp){vec_emp.push_back(emp);}virtual void notify(Employee* emp) 0;
};class HolidaysMediator : public Mediator{
public:void notify(Employee* emp){ emp-talk();for(int i 0; i vec_emp.size(); i){if(emp ! vec_emp[i]){vec_emp[i]-talk();}}}
};int main(){HolidaysMediator holidaysMediator;Boss* boss new Boss(老板);Manager* manager new Manager(经理);Securtity* securtity new Securtity(保安);holidaysMediator.addEmployee(boss);holidaysMediator.addEmployee(manager);holidaysMediator.addEmployee(securtity);boss-setContent(明天放假);holidaysMediator.notify(boss);return 0;}
责任链模式
用来处理相关事务的一条执行链执行链上有多个节点每个节点都有机会处理请求事务如果某个节点处理完就可以根据实际业务需求传递给下一个节点继续处理或者返回处理完毕。 #includeiostream
using namespace std;class Logger{
public:enum LEVEL {DEBUG,INFO,WARN,ERROR };LEVEL m_level LEVEL::DEBUG;Logger(){ }virtual ~Logger(){}void logMessage(LEVEL level, string message){if(m_level level){write(message);}if(m_nextLogger ! NULL){m_nextLogger-logMessage(level, message);}}void setNextLogger(Logger* nextLogger){m_nextLogger nextLogger;}
protected:virtual void write(string logger){};Logger* m_nextLogger;
};class DebugLogger : public Logger{
public:DebugLogger(LEVEL level){m_level level;}void write(string logger){cout Debug Logger: m_level , message:loggerendl;}
};
class ErrorLogger : public Logger{
public:ErrorLogger(LEVEL level){m_level level;}void write(string logger){cout Error Logger: m_level , message:loggerendl;}
};class InfoLogger : public Logger{
public:InfoLogger(LEVEL level){m_level level;}void write(string logger){cout Info Logger: m_level , message:loggerendl;}
};class WarnLogger : public Logger{
public:WarnLogger(LEVEL level){m_level level;}void write(string logger){cout Warn Logger: m_level , message:loggerendl;}
};class Client{
public:Client(){}~Client(){}void test(){Logger *logger getChainOfLoggers();logger-logMessage(Logger::DEBUG, this is debug);cout --------------endl;logger-logMessage(Logger::DEBUG, this is info);cout --------------endl;logger-logMessage(Logger::DEBUG, this is warn);cout --------------endl;logger-logMessage(Logger::DEBUG, this is error);}
private:Logger* getChainOfLoggers(){Logger *debug new DebugLogger(Logger::DEBUG);Logger *info new InfoLogger(Logger::INFO);Logger *warn new WarnLogger(Logger::WARN);Logger *error new ErrorLogger(Logger::ERROR);error-setNextLogger(warn);warn-setNextLogger(info);info-setNextLogger(debug);return error;}
};int main(){Client client;client.test();return 0;
}
文章转载自: http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.pnntx.cn.gov.cn.pnntx.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.qrhh.cn.gov.cn.qrhh.cn http://www.morning.ppbrq.cn.gov.cn.ppbrq.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.zqfjn.cn.gov.cn.zqfjn.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.bfcrp.cn.gov.cn.bfcrp.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.fgxnb.cn.gov.cn.fgxnb.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.yggwn.cn.gov.cn.yggwn.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.tfwr.cn.gov.cn.tfwr.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.qieistand.com.gov.cn.qieistand.com http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn