#include
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;
}
};
//private下的static变量虽然类外无法通过类直接访问,但是可以类外进行初始化
//加载时实例化,这里是类外对类内私有唯一对象 instance2进行初始化,其他在类外单独定义的对象不能被初始化,因为无法使用new SingletonHungry语句,也就是调用私有的构造函数。调用不了而对于类内唯一静态对象类内声明类外初始化符合原则,虽然类外初始化,但是使用范围还是类内,所以可以调用该类的私有构造函数对其进行初始化。
//不管后面是否调用函数,我们唯一的对象都被初始化了。
SingletonHungry* SingletonHungry::instance2 = new SingletonHungry;
int main() {
//外部可以直接通过类方法getInstance直接调用获得该唯一的对象
cout << SingletonHungry::getInstance << endl;
cout << "action: \n";
system("pause");
return 0;
}
#include
#include
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;
}
};
//用一个具体策略来配置维护一个对策略对象的引用
CashContext 为具体算法和客户端之间的桥梁
//客户端可以根据不同的需求通过CashContext获得不同的结果
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()
{
//实例化打八折类
CashContext cc(1);
//获取打折后金额
double res = cc.getResult(100);
cout << res << endl;
return 0;
}
总结,如果将满减和打8折以及原价放一起(同一个类),假如打折或者原价代码出错,也会导致满减代码无法运行,也就无法实现满减功能,所以降低各个小功能之间的耦合度势在必行。,各个算法单独封装成一个个类,就是一个个不同的策略。
具体C++设计模式参考教程:https://blog.csdn.net/qq_46423987/article/details/126702565