Skip to content

Commit

Permalink
Add fan
Browse files Browse the repository at this point in the history
To understand the command pattern Fan model added.
  • Loading branch information
sandeep committed Feb 3, 2016
1 parent 8c6e6b8 commit 1ff97a9
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 9 deletions.
19 changes: 19 additions & 0 deletions CommandPattern/src/org/sandeep/command/Fan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sandeep.command;

public class Fan {

public void rotateFan(){

System.out.println("Fan is rotating now");
}

public void stopFan(){

System.out.println("Fan is stoped");
}





}
16 changes: 8 additions & 8 deletions CommandPattern/src/org/sandeep/command/Light.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.sandeep.command;

public class Light {
public void turnOn(){

System.out.println("Light are on..");
}
public void turnOn(){

public void turnOff(){

System.out.println("Light are Off");
}
System.out.println("Light are on..");
}

public void turnOff(){

System.out.println("Light are Off");
}


}
23 changes: 23 additions & 0 deletions CommandPattern/src/org/sandeep/command/RotateFan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sandeep.command;

public class RotateFan implements Command {

private Fan fan;




public RotateFan(Fan fan) {
// TODO Auto-generated constructor stub
this.fan= fan;
}



@Override
public void execute() {
// TODO Auto-generated method stub
this.fan.rotateFan();
}

}
16 changes: 16 additions & 0 deletions CommandPattern/src/org/sandeep/command/StopFan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sandeep.command;

public class StopFan implements Command {

private Fan fan ;
public StopFan(Fan fan){
this.fan= fan;
}

@Override
public void execute() {
// TODO Auto-generated method stub
this.fan.stopFan();
}

}
9 changes: 9 additions & 0 deletions CommandPattern/src/org/sandeep/command/Switcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public void addcommand(Command command){
this.listofcommands.add(command);
}

public void giveCommand(Command command){
for(Command x : this.listofcommands){
if(x.equals(command)){
command.execute();
System.out.println("This is:"+command.getClass());
}
}

}
public void executeCommand(){

for(Command c : this.listofcommands){
Expand Down
8 changes: 7 additions & 1 deletion CommandPattern/src/org/sandeep/command/app.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ public static void main(String args[]){
Light light = new Light();
Turnoffcommand off = new Turnoffcommand(light);
Turnoncommand on = new Turnoncommand(light);
Fan fan = new Fan();
RotateFan rotate = new RotateFan(fan);
StopFan stop = new StopFan(fan);


Switcher sw = new Switcher();
sw.addcommand(off);
sw.addcommand(on);
sw.addcommand(rotate);
sw.addcommand(stop);
sw.executeCommand();

sw.giveCommand(rotate);
}

}

0 comments on commit 1ff97a9

Please sign in to comment.