《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

相关文章

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 不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素

《x86汇编语言:从实模式到保护模式》视频来了

《x86汇编语言:从实模式到保护模式》视频来了 很多朋友留言,说我的专栏《x86汇编语言:从实模式到保护模式》写得很详细,还有的朋友希望我能写得更细,最好是覆盖全书的所有章节。 毕竟我不是作者,只有作者的解读才是最权威的。 当初我学习这本书的时候,只能靠自己摸索,网上搜不到什么好资源。 如果你正在学这本书或者汇编语言,那你有福气了。 本书作者李忠老师,以此书为蓝本,录制了全套视频。 试

利用命令模式构建高效的手游后端架构

在现代手游开发中,后端架构的设计对于支持高并发、快速迭代和复杂游戏逻辑至关重要。命令模式作为一种行为设计模式,可以有效地解耦请求的发起者与接收者,提升系统的可维护性和扩展性。本文将深入探讨如何利用命令模式构建一个强大且灵活的手游后端架构。 1. 命令模式的概念与优势 命令模式通过将请求封装为对象,使得请求的发起者和接收者之间的耦合度降低。这种模式的主要优势包括: 解耦请求发起者与处理者

springboot实战学习(1)(开发模式与环境)

目录 一、实战学习的引言 (1)前后端的大致学习模块 (2)后端 (3)前端 二、开发模式 一、实战学习的引言 (1)前后端的大致学习模块 (2)后端 Validation:做参数校验Mybatis:做数据库的操作Redis:做缓存Junit:单元测试项目部署:springboot项目部署相关的知识 (3)前端 Vite:Vue项目的脚手架Router:路由Pina:状态管理Eleme

状态模式state

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/state 在一个对象的内部状态变化时改变其行为, 使其看上去就像改变了自身所属的类一样。 在状态模式中,player.getState()获取的是player的当前状态,通常是一个实现了状态接口的对象。 onPlay()是状态模式中定义的一个方法,不同状态下(例如“正在播放”、“暂停