设计模式指令模式_指令设计模式

更新时间:2023-06-30 19:15:51 阅读: 评论:0

设计模式指令模式_指令设计模式
设计模式 指令模式
Command Pattern is one of the Behavioral Design Pattern. Command design pattern is ud to implement loo coupling in a request-respon model.
命令模式是⾏为设计模式之⼀。 命令设计模式⽤于在请求-响应模型中实现松耦合 。
命令模式 (Command Pattern)
In command pattern, the request is nd to the invoker and invoker pass it to the encapsulated command object.
在命令模式中,请求被发送到invoker ,调⽤者将其传递给封装的command对象。
Command object pass the request to the appropriate method of Receiver to perform the specific action.
命令对象将请求传递给Receiver的适当⽅法以执⾏特定操作。
The client program create the receiver object and then attach it to the Command. Then it creates the invoker object and attach the command object to perform an action.
客户端程序创建接收器对象,然后将其附加到命令。 然后,它创建调⽤者对象并附加命令对象以执⾏操作。
Now when client program executes the action, it’s procesd bad on the command and receiver object.
现在,当客户端程序执⾏动作时,将根据命令和接收器对象对其进⾏处理。
命令设计模式⽰例 (Command Design Pattern Example)
We will look at a real life scenario where we can implement Command pattern. Let’s say we want to provide a File System utility with methods to open, write and clo file. This file system utility should support multiple operating systems such as Windows and Unix.
我们将看⼀个可以实现Command模式的现实⽣活场景。 假设我们要为File System实⽤程序提供打开,写⼊和关闭⽂件的⽅法。 该⽂件系统实⽤程序应⽀持多种操作系统,例如Windows和Unix。
To implement our File System utility, first of all we need to create the receiver class that will actually do all the work.
要实现我们的⽂件系统实⽤程序,⾸先,我们需要创建将实际完成所有⼯作的接收器类。
Since we code in terms of , we can have FileSystemReceiver interface and it’s implementation class for different operating system flavors such as Windows, Unix, Solaris etc.
由于我们中的编码,因此我们可以拥有FileSystemReceiver接⼝,并且它是⽤于不同操作系统风格的实现类,例如
2013广东中考
Windows,Unix,Solaris等。
命令模式接收器类 (Command Pattern Receiver Class)
package com.and;
public interface FileSystemReceiver {
void openFile();
can u speak english
void writeFile();
void cloFile();
supreme怎么读}
FileSystemReceiver interface defines the contract for the implementation class. For simplicity, I am creating two flavors of receiver class to work with Unix and Windows systems.
FileSystemReceiver接⼝定义实现类的协定。 为简单起见,我创建了两种类型的接收器类以与Unix和Windows系统⼀起使⽤。
public class UnixFileSystemReceiver implements FileSystemReceiver {
@Override
public void openFile() {
System.out.println("Opening file in unix OS");
}
@Override
纪录片下载public void writeFile() {
System.out.println("Writing file in unix OS");
}
@Override
public void cloFile() {
System.out.println("Closing file in unix OS");
}
}
package com.and;
public class WindowsFileSystemReceiver implements FileSystemReceiver {
@Override
public void openFile() {
System.out.println("Opening file in Windows OS");
}
@Overrideabit
public void writeFile() {
System.out.println("Writing file in Windows OS");
}
@Override
public void cloFile() {
System.out.println("Closing file in Windows OS");
}
}
Did you noticed the Override annotation and if you wonder why it’s ud, plea read and .
您是否注意到Override注释,并且如果您想知道为什么使⽤它,请阅读并 。
Now that our receiver class are ready, we can move to implement our Command class.
现在我们的接收器类已经准备好了,我们可以开始实现Command类了。
命令模式接⼝和实现 (Command Pattern Interface and Implementations)
We can u to create our ba Command, it’s a design decision and depends on your requirement.我们可以使⽤来创建基本Command,这是设计决定,取决于您的要求。htsc
We are going with interface becau we don’t have any default implementations.
我们要使⽤接⼝,因为我们没有任何默认实现。
public interface Command {
void execute();
}
Now we need to create implementations for all the different types of action performed by the receiver. Since we have three actions we will create three Command implementations. Each Command implementation will forward the request to the appropriate method of receiver.
现在,我们需要为接收器执⾏的所有不同类型的操作创建实现。 由于我们有三个动作,因此我们将创建三个Command实现。 每个Command实现都会将请求转发到适当的接收⽅⽅法。
package com.and;
public class OpenFileCommand implements Command {
private FileSystemReceiver fileSystem;
public OpenFileCommand(FileSystemReceiver fs){wait for you
this.fileSystem=fs;
}
@Override
public void execute() {
//open command is forwarding request to openFile method
this.fileSystem.openFile();
punk
}
}
package com.and;
public class CloFileCommand implements Command {
private FileSystemReceiver fileSystem;
public CloFileCommand(FileSystemReceiver fs){
this.fileSystem=fs;
}
@Override
public void execute() {
this.fileSystem.cloFile();
}
}
public class WriteFileCommand implements Command {
private FileSystemReceiver fileSystem;
public WriteFileCommand(FileSystemReceiver fs){
this.fileSystem=fs;
}
@Override
public void execute() {
this.fileSystem.writeFile();
}
}
Now we have receiver and command implementations ready, so we can move to implement the invoker class.
现在我们已经准备好接收器和命令实现,因此可以继续执⾏调⽤程序类。
命令模式调⽤者类 (Command Pattern Invoker Class)
Invoker is a simple class that encapsulates the Command and pass the request to the command object to process it.
Invoker是⼀个简单的类,它封装Command,并将请求传递给Command对象以对其进⾏处理。
package com.and;
public class FileInvoker {
public Command command;
public FileInvoker(Command c){
}
foreigner怎么读public void execute(){
}
}
Our file system utility implementation is ready and we can move to write a simple command pattern client program. But before that I will provide a utility method to create the appropriate FileSystemReceiver object.
我们的⽂件系统实⽤程序实现已经准备就绪,我们可以继续编写简单的命令模式客户端程序。 但是在此之前,我将提供⼀种实⽤程序⽅法来创建适当的FileSystemReceiver对象。
Since we can u , we will u this or el we can u to return appropriate type bad on the input.
由于我们可以使⽤ ,因此将使⽤此类,否则我们可以使⽤来基于输⼊返回适当的类型。
public class FileSystemReceiverUtil {
public static FileSystemReceiver getUnderlyingFileSystem(){
String osName = Property("os.name");
System.out.println("Underlying OS is:"+osName);
ains("Windows")){
return new WindowsFileSystemReceiver();
}el{
return new UnixFileSystemReceiver();
}
}
}
Let’s move now to create our command pattern example client program that will consume our file system utility.
现在开始创建将使⽤我们的⽂件系统实⽤程序的命令模式⽰例客户端程序。
package com.and;
public class FileSystemClient {
public static void main(String[] args) {
//Creating the receiver object
FileSystemReceiver fs = UnderlyingFileSystem();
//creating command and associating with receiver
一般现在时的构成OpenFileCommand openFileCommand = new OpenFileCommand(fs);
//Creating invoker and associating with Command
FileInvoker file = new FileInvoker(openFileCommand);
//perform action on invoker object
WriteFileCommand writeFileCommand = new WriteFileCommand(fs);
file = new FileInvoker(writeFileCommand);
CloFileCommand cloFileCommand = new CloFileCommand(fs);
file = new FileInvoker(cloFileCommand);
}
}
Notice that client is responsible to create the appropriate type of command object. For example if you want to write a file you are not suppod to create CloFileCommand object.
注意,客户端负责创建适当类型的命令对象。 例如,如果要编写⽂件,则不应创建CloFileCommand对象。
Client program is also responsible to attach receiver to the command and then command to the invoker class.
客户端程序还负责将接收⽅附加到命令,然后将命令附加到调⽤⽅类。
Output of the above command pattern example program is:
上⾯的命令模式⽰例程序的输出为:

本文发布于:2023-06-30 19:15:51,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1070741.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:命令   实现   创建   对象   模式   程序   接收器
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图