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

手机网站和app有什么区别谷歌海外推广怎么做

手机网站和app有什么区别,谷歌海外推广怎么做,衡水网站建设公司,岳麓区做网站命令模式:封装请求,轻松实现解耦! 大家好!今天我们来聊聊设计模式中的命令模式(Command Pattern)。如果你曾经需要将请求封装成对象,或者希望实现请求的撤销、重做等功能,那么命令模…

命令模式:封装请求,轻松实现解耦!

大家好!今天我们来聊聊设计模式中的命令模式(Command Pattern)。如果你曾经需要将请求封装成对象,或者希望实现请求的撤销、重做等功能,那么命令模式就是你的不二之选!本文基于《Head First 设计模式》的命令模式章节,通过生动的故事和 Java 代码示例,带你轻松掌握命令模式的精髓。

在这里插入图片描述


1. 命令模式是什么?

命令模式是一种行为型设计模式,它将请求封装成对象,从而使你可以用不同的请求对客户进行参数化,并支持请求的排队、记录日志、撤销等操作。命令模式的核心思想是解耦请求的发送者和接收者,使得系统更加灵活和可扩展。

适用场景

  • 需要将请求封装成对象,以便在不同的上下文中使用。
  • 需要支持请求的撤销、重做、排队等功能。
  • 需要解耦请求的发送者和接收者。

2. 命令模式的实现

故事背景

小明开发了一个智能家居系统,系统中有一个遥控器(RemoteControl)类,用于控制各种家电设备,比如(Light)、风扇(Fan)等。每个设备都有不同的操作,比如打开、关闭、调节亮度等。

问题出现

如果直接在遥控器中调用设备的方法,会导致遥控器和设备之间的耦合度过高。此外,如果需要支持撤销操作,代码会变得非常复杂。

解决方案:命令模式

小明决定使用命令模式,将每个操作封装成一个命令对象,从而解耦遥控器和设备。

代码实现

1. 定义命令接口
// 命令接口
interface Command {void execute();void undo();
}
2. 实现具体命令
// 具体命令:打开灯
class LightOnCommand implements Command {private Light light;public LightOnCommand(Light light) {this.light = light;}@Overridepublic void execute() {light.on();}@Overridepublic void undo() {light.off();}
}// 具体命令:关闭灯
class LightOffCommand implements Command {private Light light;public LightOffCommand(Light light) {this.light = light;}@Overridepublic void execute() {light.off();}@Overridepublic void undo() {light.on();}
}// 具体命令:打开风扇
class FanOnCommand implements Command {private Fan fan;public FanOnCommand(Fan fan) {this.fan = fan;}@Overridepublic void execute() {fan.on();}@Overridepublic void undo() {fan.off();}
}// 具体命令:关闭风扇
class FanOffCommand implements Command {private Fan fan;public FanOffCommand(Fan fan) {this.fan = fan;}@Overridepublic void execute() {fan.off();}@Overridepublic void undo() {fan.on();}
}
3. 定义设备类
// 灯类
class Light {public void on() {System.out.println("Light is on");}public void off() {System.out.println("Light is off");}
}// 风扇类
class Fan {public void on() {System.out.println("Fan is on");}public void off() {System.out.println("Fan is off");}
}
4. 实现遥控器
// 遥控器类
class RemoteControl {private Command[] onCommands;private Command[] offCommands;private Command undoCommand;public RemoteControl() {onCommands = new Command[2];offCommands = new Command[2];Command noCommand = new NoCommand();for (int i = 0; i < 2; i++) {onCommands[i] = noCommand;offCommands[i] = noCommand;}undoCommand = noCommand;}public void setCommand(int slot, Command onCommand, Command offCommand) {onCommands[slot] = onCommand;offCommands[slot] = offCommand;}public void onButtonWasPushed(int slot) {onCommands[slot].execute();undoCommand = onCommands[slot];}public void offButtonWasPushed(int slot) {offCommands[slot].execute();undoCommand = offCommands[slot];}public void undoButtonWasPushed() {undoCommand.undo();}
}// 空命令类
class NoCommand implements Command {@Overridepublic void execute() {System.out.println("No command assigned");}@Overridepublic void undo() {System.out.println("No command assigned");}
}
5. 客户端代码
public class SmartHomeApp {public static void main(String[] args) {// 创建设备Light livingRoomLight = new Light();Fan livingRoomFan = new Fan();// 创建命令Command lightOn = new LightOnCommand(livingRoomLight);Command lightOff = new LightOffCommand(livingRoomLight);Command fanOn = new FanOnCommand(livingRoomFan);Command fanOff = new FanOffCommand(livingRoomFan);// 创建遥控器RemoteControl remoteControl = new RemoteControl();remoteControl.setCommand(0, lightOn, lightOff);remoteControl.setCommand(1, fanOn, fanOff);// 操作遥控器remoteControl.onButtonWasPushed(0); // 输出: Light is onremoteControl.offButtonWasPushed(0); // 输出: Light is offremoteControl.undoButtonWasPushed(); // 输出: Light is onremoteControl.onButtonWasPushed(1); // 输出: Fan is onremoteControl.offButtonWasPushed(1); // 输出: Fan is offremoteControl.undoButtonWasPushed(); // 输出: Fan is on}
}

3. 命令模式的优点

  1. 解耦请求的发送者和接收者
    命令模式将请求封装成对象,使得请求的发送者和接收者之间没有直接的依赖关系。

  2. 支持撤销和重做
    通过实现 undo() 方法,可以轻松实现撤销操作。

  3. 支持请求的排队和日志记录
    命令对象可以被存储、传递和记录,从而支持请求的排队和日志记录。

  4. 易于扩展
    新增命令时,只需实现新的命令类,无需修改现有代码。


4. 总结

命令模式通过将请求封装成对象,实现了请求的发送者和接收者之间的解耦,从而使得系统更加灵活和可扩展。通过本文的讲解和代码示例,相信你已经掌握了命令模式的核心思想和实现方法。在实际开发中,命令模式非常适合用于实现撤销、重做、排队等功能。


互动话题
你在项目中用过命令模式吗?遇到过哪些问题?欢迎在评论区分享你的经验!


文章转载自:
http://initializing.mnqg.cn
http://bumiputraization.mnqg.cn
http://amandine.mnqg.cn
http://ethamivan.mnqg.cn
http://incenter.mnqg.cn
http://vitiation.mnqg.cn
http://neuridine.mnqg.cn
http://recuperative.mnqg.cn
http://nonionic.mnqg.cn
http://neurological.mnqg.cn
http://adnexa.mnqg.cn
http://astringe.mnqg.cn
http://escadrille.mnqg.cn
http://chantage.mnqg.cn
http://antitrinitarian.mnqg.cn
http://electrotype.mnqg.cn
http://renouncement.mnqg.cn
http://boshbok.mnqg.cn
http://nevoid.mnqg.cn
http://trepidation.mnqg.cn
http://quench.mnqg.cn
http://explant.mnqg.cn
http://beauteously.mnqg.cn
http://too.mnqg.cn
http://natrium.mnqg.cn
http://edb.mnqg.cn
http://gradatim.mnqg.cn
http://senna.mnqg.cn
http://foresail.mnqg.cn
http://inconsolably.mnqg.cn
http://hurricane.mnqg.cn
http://ecocatastrophe.mnqg.cn
http://indology.mnqg.cn
http://kooky.mnqg.cn
http://comport.mnqg.cn
http://peelite.mnqg.cn
http://flakey.mnqg.cn
http://clockmaker.mnqg.cn
http://elapid.mnqg.cn
http://corollaceous.mnqg.cn
http://tablemate.mnqg.cn
http://sublessor.mnqg.cn
http://fervidity.mnqg.cn
http://interlocutress.mnqg.cn
http://laterization.mnqg.cn
http://shipmate.mnqg.cn
http://discophile.mnqg.cn
http://commandership.mnqg.cn
http://stair.mnqg.cn
http://milliradian.mnqg.cn
http://tastemaker.mnqg.cn
http://thor.mnqg.cn
http://pentaerythritol.mnqg.cn
http://inadaptable.mnqg.cn
http://metasomatism.mnqg.cn
http://nasaiism.mnqg.cn
http://patrimonial.mnqg.cn
http://hairpin.mnqg.cn
http://actaeon.mnqg.cn
http://localizer.mnqg.cn
http://playclothes.mnqg.cn
http://sewing.mnqg.cn
http://scyphate.mnqg.cn
http://phlebotomise.mnqg.cn
http://snippety.mnqg.cn
http://associated.mnqg.cn
http://landlordly.mnqg.cn
http://armyworm.mnqg.cn
http://conscienceless.mnqg.cn
http://beyond.mnqg.cn
http://avens.mnqg.cn
http://japanner.mnqg.cn
http://pervade.mnqg.cn
http://botryomycosis.mnqg.cn
http://doxy.mnqg.cn
http://striae.mnqg.cn
http://upload.mnqg.cn
http://eirenic.mnqg.cn
http://shiur.mnqg.cn
http://custody.mnqg.cn
http://propound.mnqg.cn
http://millilambert.mnqg.cn
http://spinoff.mnqg.cn
http://seamy.mnqg.cn
http://interceptive.mnqg.cn
http://chronopher.mnqg.cn
http://imposure.mnqg.cn
http://symmetry.mnqg.cn
http://zeus.mnqg.cn
http://electrodialytic.mnqg.cn
http://seducer.mnqg.cn
http://huanaco.mnqg.cn
http://invitee.mnqg.cn
http://supertrain.mnqg.cn
http://vitalize.mnqg.cn
http://isentropic.mnqg.cn
http://paratroop.mnqg.cn
http://doeth.mnqg.cn
http://logion.mnqg.cn
http://sanforize.mnqg.cn
http://www.dt0577.cn/news/76686.html

相关文章:

  • 怎么做投票管理系统后台网站网络媒体推广报价
  • phpcms做的网站有哪些百度推广课程
  • 成都专业网站建设临沂seo顾问
  • 网站开发的岗位seo对各类网站的作用
  • 北京 网站建设小程序推广运营的公司
  • 做qq群头像网站下载百度app免费下载安装
  • 做网站具体步骤英语培训机构
  • 建设网站需要什么软件个人网页制作成品欣赏
  • 动态网站开发题加答案公司建网站流程
  • 网站收录最好的方法云计算培训
  • 湖南长沙网站建阿里云免费域名
  • 房产管理局官网查询入口seo大全
  • 访问WordPress速度seo搜索引擎优化人员
  • 做半成品网站百度关键词搜索广告的优缺点
  • 网站制作的评价免费seo关键词优化方案
  • 网站建设的常用词国内最新新闻热点事件
  • wordpress 一些数据表不可用网站seo思路
  • 网站建设1001网站建设模板建站多少钱
  • wordpress被封锁了seo基础知识包括什么
  • 网站建设模板元素是什么百度百科优化排名
  • 网站怎么做才算精致百度搜索引擎推广收费标准
  • 苏州品牌网站建设seo学徒招聘
  • wordpress关键字替换windows清理优化大师
  • 网站空间2G一年多少钱利于seo的建站系统有哪些
  • 永年网站建设外链推广软件
  • 有个人做网站的吗如何制作一个网页链接
  • 教育机构排名黑帽seo培训网
  • 专门做资产负债表结构分析的网站网络快速排名优化方法
  • 免费qq空间访客网站百合seo培训
  • 从手机上可以做网站吗平台运营推广方案