C++25设计模式之模版方法模式

在这里插入图片描述
图片引用于百度

简介:定义一个操作中的骨架,而计算的一些步骤下放到子类中,以保证子类在不影响结构的前提下改变算法的某些特定步骤

优点:封装了某些不变的步骤,便于代码复用,扩展时只需改变变化的步骤,不影响原有代码,符合开闭原则

缺点:对每一个不同的操作都要实现一个子类,导致类的数量增加,设计也更抽象,大大提高了代码阅读难度

例子背景:这里使用桥接模式的代码实现

模板方法模式代码:

  • 抽象平台类:
#pragma once
#include "Goods.h"

class ECommercePlatform
{
public:
        ECommercePlatform() = default;
        virtual ~ECommercePlatform() = default;
public:
        virtual bool PutawayGoods(ElectricalShop* shop)
        {
                if (!CheckAnnualFee(shop)) return false;

                ShowShop(shop);

                return true;
        }
protected:
        virtual bool CheckAnnualFee(ElectricalShop* shop)
        {
                auto af = shop->GetAssets();
                auto residue = af - GetPlatformForTheAnnualFee();
                bool isPass = residue > 0;
                string str(shop->GetShopName());
                if (isPass)
                {
                        str += "交完";
                        str += GetECName();
                        str += "平台年费后还剩:";
                        str += to_string(residue);
                }
                else
                {
                        str += "资产不足以交";
                        str += GetECName();
                        str += "平台年费,上架失败";
                }
                cout << str << endl;
                return isPass;
        }
        virtual string MergeGoodsInfo(ElectricalShop* shop)
        {
                string str(shop->GetShopName());
                str += "的";
                str += shop->GetGoodsName();
                str += "产品在";
                str += GetECName();
                str += "平台上架成功,售价为:";
                str += to_string(shop->GetGoodsPrice());
                str += ",产品介绍:";
                str += shop->GetGoodsIntroduction();
                return str;
        }
        virtual void ShowShop(ElectricalShop* shop) = 0;
        virtual double GetPlatformForTheAnnualFee() = 0;
        virtual string GetECName() = 0;
};
  • 具体平台类:
#pragma once
#include "ECommercePlatform.h"

class MouBao : public ECommercePlatform
{
public:
        MouBao() = default;
        virtual ~MouBao() = default;
protected:
        virtual void ShowShop(ElectricalShop* shop)
        {
                cout << MergeGoodsInfo(shop) << endl;
        }
        virtual double GetPlatformForTheAnnualFee()
        {
                return 45000;
        }
        virtual string GetECName()
        {
                return "某宝";
        }
};

class MouDong : public ECommercePlatform
{
public:
        MouDong() = default;
        virtual ~MouDong() = default;
        virtual void ShowShop(ElectricalShop* shop)
        {
                cout << MergeGoodsInfo(shop) << endl;
        }
        virtual double GetPlatformForTheAnnualFee()
        {
                return 60000;
        }
        virtual string GetECName()
        {
                return "某东";
        }
};
  • 抽象商店类:
#pragma once
#include <string>
#include <iostream>
using namespace std;

class ElectricalShop
{
public:
        virtual ~ElectricalShop() = default;
public:
        virtual string GetShopName() = 0;
        virtual string GetGoodsName() = 0;
        virtual string GetGoodsIntroduction() = 0;
        virtual double GetGoodsPrice() = 0;
        virtual double GetAssets() = 0;
};
  • 具体商店类:
#pragma once
#include "ElectricalShop.h"

class i7Computer : public ElectricalShop
{
public:
        i7Computer() = default;
        virtual string GetShopName()
        {
                return "军工级电脑旗舰店";
        }
        virtual string GetGoodsName()
        {
                return "i7游戏电脑";
        }
        virtual string GetGoodsIntroduction()
        {
                return "i7级8核16线Cpu,军工级主板,16G大内存,8G显卡,你值得拥有";
        }
        virtual double GetGoodsPrice()
        {
                return 5899;
        }
        virtual double GetAssets()
        {
                return 80000;
        }
};

class i9Computer : public ElectricalShop
{
public:
        i9Computer() = default;
        virtual string GetShopName()
        {
                return "航天级电脑旗舰店";
        }
        virtual string GetGoodsName()
        {
                return "i9高端游戏电脑";
        }
        virtual string GetGoodsIntroduction()
        {
                return "i9级10核20线Cpu,航天级主板,32G大内存,12G显卡,你值得拥有";
        }
        virtual double GetGoodsPrice()
        {
                return 9999;
        }
        virtual double GetAssets()
        {
                return 100000;
        }
};

class ComputerPeripherals : public ElectricalShop
{
public:
        ComputerPeripherals() = default;
        virtual string GetShopName()
        {
                return "高端电脑外设旗舰店";
        }
        virtual string GetGoodsName()
        {
                return "电脑外设套装";
        }
        virtual string GetGoodsIntroduction()
        {
                return "好马配好鞍,外设套装你怎么能没有";
        }
        virtual double GetGoodsPrice()
        {
                return 2999;
        }
        virtual double GetAssets()
        {
                return 50000;
        }
};
  • 引用:
#include "Platform.h"

int main()
{
        shared_ptr<ElectricalShop> s1(new i7Computer);
        shared_ptr<ElectricalShop> s2(new i9Computer);
        shared_ptr<ElectricalShop> s3(new ComputerPeripherals);

        shared_ptr<ECommercePlatform> e1(new MouBao);
        e1->PutawayGoods(s1.get());
        e1->PutawayGoods(s2.get());
        e1->PutawayGoods(s3.get());

        shared_ptr<ECommercePlatform> e2(new MouDong);
        e2->PutawayGoods(s1.get());
        e2->PutawayGoods(s2.get());
        e2->PutawayGoods(s3.get());

        getchar();
        return 0;
}

总结:
模板方法模式(Template Method):父类中提取出了公共代码,提高代码复用率。封装了固定流程,使整个功能实现更加的灵活,又不会影响整体框架,所以设计处就要想好哪些复用率最高。

作者:丶梦爱
博客:https://blog.youkuaiyun.com/u014732297(转载请说明出处)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值