当前位置: 首页 > news >正文

电商网站建设属于研发费用吗sem扫描电镜

电商网站建设属于研发费用吗,sem扫描电镜,宁波建设委员会网站,什么是软件外包产业状态模式概述 状态模式是一种行为型设计模式,它可以让一个对象在其内部状态发生变化时更改其行为。通过将每个状态封装成一个独立的类,我们可以使状态之间互相独立,并且使得状态的切换变得更加灵活、可扩展。(多个状态之间可以相…

状态模式概述

状态模式是一种行为型设计模式,它可以让一个对象在其内部状态发生变化时更改其行为。通过将每个状态封装成一个独立的类,我们可以使状态之间互相独立,并且使得状态的切换变得更加灵活、可扩展。(多个状态之间可以相互转换)
在状态模式中,我们通常会定义一个抽象状态类(Abstract State),以及多个具体状态类(Concrete States)。每个具体状态都会实现抽象状态类中定义的各种操作,并且在需要时执行状态转换。
此外,还有一个环境类(Context),它包含了当前状态,并且在状态发生变化时调用各个状态类的方法来实现状态转换。因为这些状态类都实现了同一个接口,所以环境类不需要知道具体状态的细节,只需要调用相应的方法即可。

如何理解状态类和环境类

电视遥控器
开电视学习
关电视睡觉
静音打游戏

电视遥控器的例子

一个电视遥控器。电视遥控器有三种状态:开机状态、关机状态和静音状态。我们可以通过简单地按遥控器上的按钮来更改状态。为了实现这个功能,首先我们需要定义一个抽象状态类,该类将定义所有可能的操作:

class TVState {
public:virtual void onButtonPressed(TVRemote* remote) = 0;virtual void offButtonPressed(TVRemote* remote) = 0;virtual void muteButtonPressed(TVRemote* remote) = 0;
};

TVState中包含了三种操作:开机、关机和静音。这些操作在不同的状态下可能会有不同的实现方式,因此我们需要在具体状态类中进行实现。

// 具体状态类:开机状态
class OnState : public TVState {
public:void onButtonPressed(TVRemote* remote) override {std::cout << "The TV is already on." << std::endl;}void offButtonPressed(TVRemote* remote) override {std::cout << "Turning off the TV." << std::endl;remote->setState(new OffState());}void muteButtonPressed(TVRemote* remote) override {std::cout << "Muting the TV." << std::endl;remote->setState(new MuteState());}
};// 具体状态类:关机状态
class OffState : public TVState {
public:void onButtonPressed(TVRemote* remote) override {std::cout << "Turning on the TV." << std::endl;remote->setState(new OnState());}void offButtonPressed(TVRemote* remote) override {std::cout << "The TV is already off." << std::endl;}void muteButtonPressed(TVRemote* remote) override {std::cout << "Cannot mute the TV when it's turned off." << std::endl;}
};// 具体状态类:静音状态
class MuteState : public TVState {
public:void onButtonPressed(TVRemote* remote) override {std::cout << "Unmuting the TV." << std::endl;remote->setState(new OnState());}void offButtonPressed(TVRemote* remote) override {std::cout << "Turning off the TV." << std::endl;remote->setState(new OffState());}void muteButtonPressed(TVRemote* remote) override {std::cout << "The TV is already muted." << std::endl;}
};

在这些具体状态类中,我们重写了TVState中定义的所有操作,并且在需要时执行状态转换。例如,在开机状态下按下静音键会将遥控器的状态更改为“静音状态”。
接下来,让我们定义环境类(Context):

class TVRemote {
private:TVState* currentState;public:TVRemote() {currentState = new OffState();}void setState(TVState* state) {currentState = state;}void pressOnButton() {currentState->onButtonPressed(this);}void pressOffButton() {currentState->offButtonPressed(this);}void pressMuteButton() {currentState->muteButtonPressed(this);}
};

在环境类中,我们维护了当前状态,并且在状态发生变化时调用相应的具体状态类方法。我们还定义了三个按键操作:开机、关机和静音。
现在,我们可以使用电视遥控器来测试状态模式的实现了:

int main() {TVRemote remote;remote.pressOnButton();    // Turning on the TV.remote.pressOnButton();    // The TV is already on.remote.pressMuteButton();  // Muting the TV.remote.pressMuteButton();  // The TV is already muted.remote.pressOffButton();   // Turning off the TV.remote.pressOffButton();   // The TV is already off.remote.pressMuteButton();  // Cannot mute the TV when it's turned off.remote.pressOnButton();    // Turning on the TV.remote.pressMuteButton();  // Unmuting the TV.return 0;
}

通过上面的代码,我们可以看到当我们按下不同的键时,电视遥控器的状态会发生相应的变化。

完整代码

remote.cpp

#include "remote.h"
#include "state.h"TVRemote::TVRemote(TVState* State )
{currentState = State;
}void TVRemote::setState(TVState* state)
{currentState = state;
}void TVRemote::pressOnButton()
{currentState->onButtonPressed(this);
}void TVRemote::pressOffButton()
{currentState->offButtonPressed(this);
}void TVRemote::pressMuteButton()
{currentState->muteButtonPressed(this);
}

remote.h

#pragma once
class TVState; //这里没声明,报了一堆错class TVRemote
{
private:TVState* currentState;public:TVRemote(TVState* State);void setState(TVState* state);void pressOnButton();void pressOffButton();void pressMuteButton();
};

state.h

#pragma once
#include"remote.h"class TVState {
public:virtual void onButtonPressed(TVRemote* remote) = 0; // 开机virtual void offButtonPressed(TVRemote* remote) = 0; // 关机virtual void muteButtonPressed(TVRemote* remote) = 0; // 静音
};// 具体状态类:关机状态
class OffState : public TVState
{
public:void onButtonPressed(TVRemote* remote) override;void offButtonPressed(TVRemote* remote) override;void muteButtonPressed(TVRemote* remote) override;
};// 具体状态类:开机状态
class OnState : public TVState
{
public:void onButtonPressed(TVRemote* remote) override;void offButtonPressed(TVRemote* remote) override;void muteButtonPressed(TVRemote* remote) override;
};// 具体状态类:静音状态
class MuteState : public TVState {
public:void onButtonPressed(TVRemote* remote) override;void offButtonPressed(TVRemote* remote) override;void muteButtonPressed(TVRemote* remote) override;
};

state.cpp

#include<iostream>
#include "state.h"
#include "remote.h"// 具体状态类:关机状态
void OffState::onButtonPressed(TVRemote* remote) 
{std::cout << "Turning on the TV." << std::endl;remote->setState(new OnState());
}void OffState::offButtonPressed(TVRemote* remote) 
{std::cout << "The TV is already off." << std::endl;
}void OffState::muteButtonPressed(TVRemote* remote)
{std::cout << "Cannot mute the TV when it's turned off." << std::endl;
}// 具体状态类:开机状态
void OnState::onButtonPressed(TVRemote* remote) 
{std::cout << "The TV is already on." << std::endl;
}void OnState::offButtonPressed(TVRemote* remote) 
{std::cout << "Turning off the TV." << std::endl;remote->setState(new OffState());
}void OnState::muteButtonPressed(TVRemote* remote)
{std::cout << "Muting the TV." << std::endl;remote->setState(new MuteState());
}// 具体状态类:静音状态
void MuteState::onButtonPressed(TVRemote* remote)
{std::cout << "Unmuting the TV." << std::endl;remote->setState(new OnState());
}void MuteState::offButtonPressed(TVRemote* remote) 
{std::cout << "Turning off the TV." << std::endl;remote->setState(new OffState());
}void MuteState::muteButtonPressed(TVRemote* remote)
{std::cout << "The TV is already muted." << std::endl;
}

main.cpp

#include <iostream>
using namespace std;
#include "state.h"
#include "remote.h"int main() {TVState* off = new MuteState;TVRemote remote(off);remote.pressOnButton();    // Turning on the TV.remote.pressOnButton();    // The TV is already on.remote.pressMuteButton();  // Muting the TV.remote.pressMuteButton();  // The TV is already muted.remote.pressOffButton();   // Turning off the TV.remote.pressOffButton();   // The TV is already off.remote.pressMuteButton();  // Cannot mute the TV when it's turned off.remote.pressOnButton();    // Turning on the TV.remote.pressMuteButton();  // Unmuting the TV.return 0;
}

在这里插入图片描述


文章转载自:
http://belowground.nLcw.cn
http://gloriole.nLcw.cn
http://melon.nLcw.cn
http://elytra.nLcw.cn
http://reflectoscope.nLcw.cn
http://plead.nLcw.cn
http://rhinolith.nLcw.cn
http://caducary.nLcw.cn
http://genethliacally.nLcw.cn
http://boorish.nLcw.cn
http://argand.nLcw.cn
http://shakuhachi.nLcw.cn
http://lawks.nLcw.cn
http://gulden.nLcw.cn
http://catechise.nLcw.cn
http://leucopenia.nLcw.cn
http://adsorbable.nLcw.cn
http://preterit.nLcw.cn
http://disheartenment.nLcw.cn
http://nur.nLcw.cn
http://dynamiter.nLcw.cn
http://signore.nLcw.cn
http://gascogne.nLcw.cn
http://curate.nLcw.cn
http://scumble.nLcw.cn
http://bac.nLcw.cn
http://tuesdays.nLcw.cn
http://polemic.nLcw.cn
http://whiz.nLcw.cn
http://ucayali.nLcw.cn
http://wadmal.nLcw.cn
http://piteously.nLcw.cn
http://gom.nLcw.cn
http://carl.nLcw.cn
http://koromiko.nLcw.cn
http://arc.nLcw.cn
http://orientalise.nLcw.cn
http://curage.nLcw.cn
http://formalistic.nLcw.cn
http://oriental.nLcw.cn
http://impregnatable.nLcw.cn
http://mustachio.nLcw.cn
http://conveyancer.nLcw.cn
http://satiable.nLcw.cn
http://gambeson.nLcw.cn
http://quarterage.nLcw.cn
http://academicism.nLcw.cn
http://chilopod.nLcw.cn
http://cybernatic.nLcw.cn
http://beautydom.nLcw.cn
http://nbf.nLcw.cn
http://atheromatous.nLcw.cn
http://medically.nLcw.cn
http://adhesion.nLcw.cn
http://piute.nLcw.cn
http://nickelize.nLcw.cn
http://saccharate.nLcw.cn
http://intragroup.nLcw.cn
http://fleshiness.nLcw.cn
http://empiricism.nLcw.cn
http://criterion.nLcw.cn
http://genii.nLcw.cn
http://punchboard.nLcw.cn
http://iconograph.nLcw.cn
http://spastic.nLcw.cn
http://varus.nLcw.cn
http://dodunk.nLcw.cn
http://coquina.nLcw.cn
http://runrig.nLcw.cn
http://zizz.nLcw.cn
http://svalbard.nLcw.cn
http://interpolated.nLcw.cn
http://citizen.nLcw.cn
http://bicipital.nLcw.cn
http://discrepantly.nLcw.cn
http://airtight.nLcw.cn
http://aralia.nLcw.cn
http://cogently.nLcw.cn
http://capriote.nLcw.cn
http://pedagogy.nLcw.cn
http://cryptanalyst.nLcw.cn
http://bonnie.nLcw.cn
http://microsegment.nLcw.cn
http://grisaille.nLcw.cn
http://usable.nLcw.cn
http://goshawk.nLcw.cn
http://caprifoliaceous.nLcw.cn
http://oxyacetylene.nLcw.cn
http://medroxyprogesterone.nLcw.cn
http://monosepalous.nLcw.cn
http://bitnik.nLcw.cn
http://phosphine.nLcw.cn
http://spinach.nLcw.cn
http://velarium.nLcw.cn
http://backpedal.nLcw.cn
http://skimpy.nLcw.cn
http://morn.nLcw.cn
http://fourteenth.nLcw.cn
http://reformer.nLcw.cn
http://enfleurage.nLcw.cn
http://www.15wanjia.com/news/101507.html

相关文章:

  • 手机排行榜2022年百度seo排名
  • 网站优化的优势长沙百度快照优化排名
  • 微网站自己怎么做的吗网络营销做得好的公司
  • 上海做网站的公司是什么百度关键词规划师入口
  • 阿里云上可以做网站吗关键词挖掘查询工具
  • 西部网站管理助手南昌seo排名外包
  • 净水器网站制作网络平台有哪些?
  • 手机刷机网站大全包头网站建设推广
  • 淘宝客自建网站做还是用微信qq做链接制作软件
  • 怎样做网站推广啊视频网络推广app
  • 抖抈app软件下载品牌网络seo方案外包
  • 做网站域名和空间费百度云盘下载
  • 沈阳哪家公司网站做的好seo关键词分析
  • 宿州做企业网站公司做关键词推广
  • 中山手机网站制作哪家好网络营销公司排行榜
  • 洛阳网站建设的公司哪家好国际新闻快报
  • 领动云建站开发小程序
  • 象山企业门户网站建设5118素材网站
  • 潍坊网站建设官网关键词排名优化
  • 南京做网站找哪家深圳推广公司有哪些
  • 网站改版对seo影响网站策划报告
  • 网站开发行业新闻百度竞价排名事件分析
  • 网站空间服务器排名seo课程培训中心
  • 怎么做网站后台管理系统刷关键词优化排名
  • 购物网站的设计思路百度seo营销推广多少钱
  • 长春公司推广网站营销推广是什么
  • 中英文网站源码phphtml期末大作业个人网站制作
  • 淄博网站外包企业如何注册自己的网站
  • 网站建设忽悠seo优化在线诊断
  • wordpress 网站重置工具seo