《headfirst设计模式》读书笔记14-剩下的模式

2024-02-12 08:59

本文主要是介绍《headfirst设计模式》读书笔记14-剩下的模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 1. 桥接
    • 1.1 TV.h
    • 1.2 TVFactory.h
    • 1.3 RemoteControl.h
    • 1.4 RemoteControl.cpp
    • 1.5 LG.h
    • 1.6 LG.cpp
    • 1.7 GenericRemote.h
    • 1.8 GenericRemote.cpp
  • 2. 生成器
    • 2.1 类图和部分源码
      • 2.1.1 VacationBuilder.h
      • 2.1.2 Accommodation.h
      • 2.1.3 main.cpp
  • 3. 责任链
  • 4. 蝇量
  • 5. 解释器
  • 6. 中介者
  • 7. 备忘录
  • 8. 原型
  • 9. 访问者

1. 桥接

桥接其实就是将两个层次进行解耦

在这里插入图片描述
在这里插入图片描述

1.1 TV.h

#include <iostream>
using std::cout;
using std::endl;
class TV{
public:virtual void on()=0;virtual void off()=0;virtual void tuneChannel(int channel)=0;virtual int getChannel()=0;virtual ~TV(){};
};

1.2 TVFactory.h

#include "LG.h"
#include "Sony.h"
#include <string>
using std::string;
class TVFactory{
public:TV *getTV(string type){if(type=="LG"){return new LG();}else if(type=="Sony"){return new Sony();}else{std::cerr<<"Type mismatch";return nullptr;}}
};

1.3 RemoteControl.h

#include "TV.h"
#include "TVFactory.h"
class RemoteControl{
public:RemoteControl(TVFactory *tvFactory);virtual void on();virtual void off();virtual void setChannel(int channel);virtual int getChannel();virtual void setTV(string type);virtual ~RemoteControl();
private:TV *tv;TVFactory *tvFactory;
};

1.4 RemoteControl.cpp

#include "RemoteControl.h"RemoteControl::RemoteControl(TVFactory *tvFactory)
{this->tvFactory=tvFactory;
}void RemoteControl::on()
{tv->on();
}void RemoteControl::off()
{tv->off();
}void RemoteControl::setChannel(int channel)
{tv->tuneChannel(channel);
}int RemoteControl::getChannel()
{return tv->getChannel();
}void RemoteControl::setTV(std::string type)
{tv=tvFactory->getTV(type);
}RemoteControl::~RemoteControl()
{}

1.5 LG.h

#include "TV.h"
class LG:public TV{virtual void on()override final;virtual void off()override final;virtual void tuneChannel(int channel)override final;virtual int getChannel()override final;virtual ~LG();
private:int channel=1;
};

1.6 LG.cpp

#include "LG.h"void LG::on()
{cout<<"Turning on the LG TV"<<endl;
}void LG::off()
{cout<<"Turning off the LG TV"<<endl;
}void LG::tuneChannel(int channel)
{this->channel=channel;cout<<"Set the LG TV Channel to " + std::to_string(this->channel)<<endl;
}int LG::getChannel()
{return channel;
}LG::~LG()
{}

1.7 GenericRemote.h

#include "RemoteControl.h"
class GenericRemote:public RemoteControl{
public:GenericRemote(TVFactory *tvFactory);void nextChannel();void prevChannel();virtual ~GenericRemote();
};

1.8 GenericRemote.cpp

#include "GenericRemote.h"GenericRemote::GenericRemote(TVFactory *tvFactory):RemoteControl(tvFactory)
{}void GenericRemote::nextChannel()
{int channel=getChannel();setChannel(channel+1);
}void GenericRemote::prevChannel()
{int channel=getChannel();setChannel(channel-1);
}GenericRemote::~GenericRemote()
{}

2. 生成器

生成器模式其实就相当于根据你的需求创建一个数据结构,并且此结构运用了很多组合聚合,最后客户只需要给最后呈现的类进行打交道就行了

在这里插入图片描述
在这里插入图片描述

2.1 类图和部分源码

在这里插入图片描述

2.1.1 VacationBuilder.h

#include "Accommodation.h"
#include "Vacation.h"
#include <vector>
using std::vector;
class VacationBuilder{
public:virtual VacationBuilder *addAccommodation()=0;virtual VacationBuilder *addAccommodation(string name)=0;virtual VacationBuilder *addAccommodation(string name,int year,int month,int day,int nights,int location)=0;virtual VacationBuilder *addEvent(string event)=0;Vacation *getVacation(){Vacation *vacation=new Vacation();vacation->setName(name);vacation->setAccommodations(accommodations);vacation->setEvents(events);return vacation;}virtual ~VacationBuilder(){};string name;vector<Accommodation *> accommodations;vector<string> events;
};

2.1.2 Accommodation.h

#include "Reservation.h"
#include <string>
using std::string;
class Accommodation{
public:void setReservation(Reservation *r);Reservation *getReservation();virtual string getLocation()=0;string toString();virtual ~Accommodation();string name;Reservation *reservation=nullptr;
};

2.1.3 main.cpp

#include <iostream>
#include "VacationBuilder.h"
#include "CityVacationBuilder.h"
#include "OutdoorsVacationBuilder.h"
using namespace std;int main()
{VacationBuilder *outdoorsyVacationBuilder = new OutdoorsVacationBuilder();Vacation *outdoorsyVacation = outdoorsyVacationBuilder->addAccommodation("Two person tent", 2020, 7, 1, 5, 34)->addEvent("Beach")->addAccommodation("Two person tent")->addEvent("Mountains")->getVacation();cout<<outdoorsyVacation->toString()<<endl;VacationBuilder *cityVacationBuilder = new CityVacationBuilder();Vacation *cityVacation = cityVacationBuilder->addAccommodation("Grand Facadian", 2020, 8, 1, 5, 0)->addAccommodation("Hotel Commander", 2020, 8, 6, 2, 0)->addEvent("Cirque du Soleil")->getVacation();cout<<cityVacation->toString()<<endl;return 0;
}

3. 责任链

责任链模式其实就是超类的自关联,每个子类都是一道处理工序,子类继承后通过相应的工序连接相应的子类即可打造出一条链条

在这里插入图片描述
在这里插入图片描述

4. 蝇量

蝇量模式其实就是将一个实例放到不同位置,造成有许多个实例的假象

在这里插入图片描述
在这里插入图片描述

5. 解释器

解释器模式就相当于一种递归的用法,将表达式进行递归的分解,直到分解到可以执行的程度(相当于计算器的原理)

在这里插入图片描述
在这里插入图片描述

6. 中介者

中介者其实相当于将组件间复杂的沟通使用中介者进行集中沟通,其实有点像外观模式,但是这个应该更加复杂一点

在这里插入图片描述
在这里插入图片描述

7. 备忘录

在这里插入图片描述
在这里插入图片描述

8. 原型

在这里插入图片描述
在这里插入图片描述

9. 访问者

在这里插入图片描述
在这里插入图片描述

这篇关于《headfirst设计模式》读书笔记14-剩下的模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/702216

相关文章

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

业务中14个需要进行A/B测试的时刻[信息图]

在本指南中,我们将全面了解有关 A/B测试 的所有内容。 我们将介绍不同类型的A/B测试,如何有效地规划和启动测试,如何评估测试是否成功,您应该关注哪些指标,多年来我们发现的常见错误等等。 什么是A/B测试? A/B测试(有时称为“分割测试”)是一种实验类型,其中您创建两种或多种内容变体——如登录页面、电子邮件或广告——并将它们显示给不同的受众群体,以查看哪一种效果最好。 本质上,A/B测

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

迭代器模式iterator

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/iterator 不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素