Created
November 9, 2017 14:27
-
-
Save marc-x-andre/486cc044779bfa3d13c654b460d6fa3f to your computer and use it in GitHub Desktop.
Java Command (Design Pattern) [French/Français]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Java Command (Design Pattern) [French/Français] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Command { | |
void execute(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Faxe implements Command { | |
private String tel; | |
public Faxe(String tel) { | |
this.tel = tel; | |
} | |
public void execute() { | |
System.out.println("Document faxe au numero : "+tel+"."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class Imprimante { | |
private List<Command> history = new ArrayList<Command>(); | |
public void addCommand(Command command) { | |
history.add(command); | |
command.execute(); | |
} | |
/** | |
* Historique optionnel dans le design pattern 'command' | |
*/ | |
public void printHistory() { | |
System.out.println("__HISTORIQUE__"); | |
for (Command command : history) { | |
System.out.println(command.getClass().getName()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Imprime implements Command { | |
private String document; | |
public Imprime(String document) { | |
this.document = document; | |
} | |
public void execute() { | |
System.out.println("Impression du document "+document+"."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String... args) { | |
Imprimante imprimante = new Imprimante(); | |
Command imprime = new Imprime("test.pdf"); | |
Command scanner = new Scanner(); | |
Command faxe = new Faxe("514-123-4321"); | |
imprimante.addCommand(imprime); | |
imprimante.addCommand(scanner); | |
imprimante.addCommand(faxe); | |
System.out.print("\n\n"); | |
imprimante.printHistory(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Scanner implements Command { | |
public void execute() { | |
System.out.println("Scanning en cours... "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment