Back
Close

Design Pattern Command

QLenoir
1,399 views

Diagramme

Voici le diagramme des classes que nous allons implémenter. Nous allons simuler un système de fichiers. Le FileSystem ici va jouer le rôle du Client, le fichier MyFile joue ici le rôle d'invoker, les ConcreteCommandes CloseFileCommande WriteFileCommand et OpenFileCommand vont éxécuter les méthodes nécessaires du Receiver FileSystem.
Uml exemple of Design Pattern Command

Les Receiver

On crée nos objets Receiver :

  • L'interface FileSystem :

public interface FileSystemReceiver {

	void openFile();
	void writeFile();
	void closeFile();
}
  • Un objet UnixFileSystemReceiver qui implémentera l'interface FileSystem :

public class UnixFileSystemReceiver implements FileSystemReceiver {

	public void openFile() {
		System.out.println("Opening file in unix OS");
	}

	public void writeFile() {
		System.out.println("Writing file in unix OS");
	}

	public void closeFile() {
		System.out.println("Closing file in unix OS");
	}

}
  • Un objet WindowsFileSystemReceiver qui implémentera l'interface FileSystem :

public class WindowsFileSystemReceiver implements FileSystemReceiver {

	public void openFile() {
		System.out.println("Opening file in Windows OS");
		
	}

	public void writeFile() {
		System.out.println("Writing file in Windows OS");
	}

	public void closeFile() {
		System.out.println("Closing file in Windows OS");
	}

}

L'interface Command

On crée l'interface Commande qui sera implémentée pour chaque nouvelle commande

public interface Command {

	void execute();
}

Les ConcreteCommand de notre FileSystem

Commande permettant d'ouvrir un fichier :

public class OpenFileCommand implements Command {

	private FileSystemReceiver fileSystem;
	
	public OpenFileCommand(FileSystemReceiver fs){
		this.fileSystem=fs;
	}
	
	public void execute() {
		//open command is forwarding request to openFile method
		this.fileSystem.openFile();
	}

}

Commande permettant de fermer un fichier :

public class CloseFileCommand implements Command {

	private FileSystemReceiver fileSystem;
	
	public CloseFileCommand(FileSystemReceiver fs){
		this.fileSystem=fs;
	}
	
	public void execute() {
		this.fileSystem.closeFile();
	}

}

Commande permettant d'écrire dans un fichier :

public class WriteFileCommand implements Command {

	private FileSystemReceiver fileSystem;
	
	public WriteFileCommand(FileSystemReceiver fs){
		this.fileSystem=fs;
	}
	
	public void execute() {
		this.fileSystem.writeFile();
	}

}

Création de l'Invoker

Création du FileInvoker (MyFile sur le diagramme):

public class FileInvoker {

	public Command command;
	
	public FileInvoker(Command c){
		this.command=c;
	}
	
	public void execute(){
		this.command.execute();
	}
}

L'encapsulation ici est respectée, on ignore de quoi les Command sont faites.

Nous allons créer un FileSystemReceiverUtil pour fournir un FileSystemReceiver approprié

public class FileSystemReceiverUtil {
	
	public static FileSystemReceiver getUnderlyingFileSystem(){
		 String osName = System.getProperty("os.name");
		 System.out.println("Underlying OS is:"+osName);
		 if(osName.contains("Windows")){
			 return new WindowsFileSystemReceiver();
		 }else{
			 return new UnixFileSystemReceiver();
		 }
	}
	
}

Execution de notre programme

// Interface FileSystemReceiver {...}
// class UnixFileSystemReceiver implements FileSystemReceiver {...}
// class WindowsFile implements FileSystemReceiver {...}
// Interface Command {...}
// class OpenFileCommand implements Command {...}
// class CloseFileCommand implements Command {...}
// class WriteFileCommand implements Command {...}
// class FileInvoker {...}
// class FileSystemReceiverUtil {...}
public class Main {
public static void main(String[] args) {
//Creating the receiver object
FileSystemReceiver fs = FileSystemReceiverUtil.getUnderlyingFileSystem();
//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
file.execute();
WriteFileCommand writeFileCommand = new WriteFileCommand(fs);
file = new FileInvoker(writeFileCommand);
file.execute();
CloseFileCommand closeFileCommand = new CloseFileCommand(fs);
file = new FileInvoker(closeFileCommand);
file.execute();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants