Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command design pattern #17

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.
# What is Command Design Pattern
Command encapsulate all information needed to perform an action. It allows the requester of a particular action to be decoupled from the object that performs the action.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/command-pattern/diagrams/Command%20Design%20Pattern%20class%20diagram.jpeg "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/command-pattern/diagrams/Command-Design-Pattern-general%20-%20Page%201.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/command-pattern/diagrams/command%20sequence%20diagram.png "Diagram")

### When to use Command Design Pattern
The invoker should be decoupled from the object handling the invocation.

### Learn Design Patterns with Java by Aseem Jain
This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain".

### Course link:
https://www.packtpub.com/application-development/learn-design-patterns-java-9-video

### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem

### Authors blog on design patterns:
https://premaseem.wordpress.com/category/computers/design-patterns/

### Software Design pattern community face book page:
https://www.facebook.com/DesignPatternGuru/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/command sequence diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 44 additions & 2 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
package com.premaseem;

import java.util.Scanner;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
Scanner scanInput = new Scanner(System.in);
System.out.println("Light switch Project using COMMAND DESIGN PATTERN \n ");

/** After implementing Command design pattern **/

// All available Commands will be created and init by command factory
CommandFactory cf = CommandFactory.init();

// Will list all commands which can be executed
cf.listCommands();

// Commands can be added and listed dynamically with ZERO code changes in client side
System.out.println("Master - what command you want to execute ");
String command = scanInput.next();

// Actual execution of command will happen
cf.executeCommand(command);

/** Lessons Learnt - AFTER CODE
# New commands can be added dynamically with ease
# Client is loosely coupled and have no knowledge of encapsulated commands
# Zero code changed needed at client when new commands are added
*/


/** BEFORE CODE
// Assume you will invoke a method to execute turn on method
if (command.equalsIgnoreCase("LightOn")){
System.out.println("Light turned on");
}
else if (command.equalsIgnoreCase("LightOff")){
System.out.println("Light turned off");
}
else {
System.out.println(command+ " Command not found");
}

// Lessons Learnt - BEFORE CODE
// Lots of if else ladder
// Commands cannot be added Dynamically
// Code is tightly coupled, no separation of responsibility
*/
}
}
14 changes: 14 additions & 0 deletions pattern/src/com/premaseem/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
/**
* The Command functional interface.
*/
@FunctionalInterface
public interface Command {
public void execute();
}
47 changes: 47 additions & 0 deletions pattern/src/com/premaseem/CommandFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public final class CommandFactory {
private final Map<String, Command> commands;

private CommandFactory() {
commands = new HashMap<>();
}

public void addCommand(final String name, final Command command) {
commands.put(name, command);
}

public void executeCommand(String name) {
if (commands.containsKey(name)) {
commands.get(name).execute();
}else{
System.out.println(name+ " Command not found");
}
}

public void listCommands() {
System.out.println("Available commands: " + commands.keySet().stream().collect(Collectors.joining(", ")));
}

/* Factory pattern */
public static CommandFactory init() {
final CommandFactory cf = new CommandFactory();

// commands are added here using lambdas.
// It is also possible to dynamically add commands without editing the code.
cf.addCommand("LightOn", () -> System.out.println("Light turned on"));
cf.addCommand("LightOff", () -> System.out.println("Light turned off"));

return cf;
}
}
13 changes: 0 additions & 13 deletions patternBonus/src/com/premaseem/Client.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.premaseem.commandPattern;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

import java.util.Scanner;

public class ClientForRemoteControl {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int repeatRunFlag = 1;
RemoteControl remote = new RemoteControl();
RemoteControlLoader loader = new RemoteControlLoader(remote);
loader.load();
while (repeatRunFlag == 1) {

System.out.println("This is the Client Main Command Pattern with Home Automation system ");

System.out.println("Loading the remote with approviate commands and mapping them in slots ... ");

System.out.println("Please press the any command between 0 to 10 for operation ");
System.out.println("Remember 11 is for Master off, \n 0 is for UNDO and \n 10 is undomacro mode");


int remoteButtonNumber = scan.nextInt();
remote.remoteButtonPress(remoteButtonNumber);

System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT ");
try {
repeatRunFlag = scan.nextInt();
} catch (Exception e) {
repeatRunFlag = 0;
}
}
}
}
Loading