Top Banner
CS 415 N-Tier Application Development By Umair Ashraf June 21 ,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design Patterns
89

CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Dec 31, 2015

Download

Documents

Amanda Black
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

CS 415 N-Tier Application Development

By Umair Ashraf

June 21 ,2013

National University of Computer and Emerging Sciences

Lecture # 2

Introduction to Design Patterns

Page 2: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Contents Introduction to Design Patterns Definition Strategy Pattern Command Pattern

Page 3: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

What is Design Pattern ?

In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

Page 4: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Design Pattern

Design pattern is not invented ,It is discovered !

Page 5: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Intro to design patterns

Coming to the real stuff

Page 6: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

SimUDuck app

Page 7: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Ducks need to FLY

Page 8: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Something went wrong…

Page 9: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Because...

A localized update to the code cause a non local side

effect (flying rubber ducks)!

Page 10: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Inheritance?

Page 11: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

How about an interface?

What do you think about this design?

Page 12: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 13: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 14: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 15: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Separating what changes from what stays the same

Page 16: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 17: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 18: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 19: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 20: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 21: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 22: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Integrating the duck behavior

Page 23: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 24: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 25: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Testing the code…

Page 26: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 27: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 28: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 29: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Setting behavior dynamically

Page 30: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 31: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 32: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 33: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 34: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 35: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 36: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 37: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 38: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 39: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 40: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 41: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 42: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 43: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 44: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 45: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 46: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 47: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 48: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 49: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 50: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 51: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 52: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 53: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 54: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class LightOnCommand implements Command {Light light;

public LightOnCommand(Light light) {this.light = light;

} public void execute() {

light.on(); }}

public interface Command {public void execute();

}

Page 55: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class SimpleRemoteControl {Command slot;

public SimpleRemoteControl() {} public void setCommand(Command command) {

slot = command;} public void buttonWasPressed() {

slot.execute();}

}

Page 56: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class RemoteControlTest {public static void main(String[] args) {

SimpleRemoteControl remote = new SimpleRemoteControl();

Light light = new Light();LightOnCommand lightOn = new

LightOnCommand(light);

remote.setCommand(lightOn);remote.buttonWasPressed();

}}

Page 57: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class GarageDoorOpenCommand implements Command {GarageDoor garageDoor;public GarageDoorOpenCommand(GarageDoor

garageDoor) {this.garageDoor = garageDoor;

}public void execute() {

garageDoor.up();}

}

Page 58: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 59: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 60: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 61: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 62: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 63: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 64: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 65: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class RemoteControl {Command[] onCommands;Command[] offCommands;

public RemoteControl() {

onCommands = new Command[7];offCommands = new Command[7];

Command noCommand = new NoCommand();for (int i = 0; i < 7; i++) {

onCommands[i] = noCommand;offCommands[i] = noCommand;

}}

public void setCommand(int slot, Command onCommand, Command offCommand) {

onCommands[slot] = onCommand;offCommands[slot] = offCommand;

}

Page 66: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public void onButtonWasPushed(int slot) {onCommands[slot].execute();

}

public void offButtonWasPushed(int slot) {offCommands[slot].execute();

}

public String toString() {StringBuffer stringBuff = new StringBuffer();stringBuff.append("\n------ Remote Control -------\n");for (int i = 0; i < onCommands.length; i++) {

stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()+ " "

+offCommands[i].getClass().getName() + "\n");

}return stringBuff.toString();

}}

Page 67: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class LightOffCommand implements Command {

Light light;

public LightOffCommand(Light light) {this.light = light;

}

public void execute() {light.off();

}}

Page 68: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class StereoOnWithCDCommand implements Command {

Stereo stereo; public StereoOnWithCDCommand(Stereo

stereo) {this.stereo = stereo;

} public void execute() {

stereo.on();stereo.setCD();stereo.setVolume(11);

}}

Page 69: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class RemoteLoader { public static void main(String[] args) {

RemoteControl remoteControl = new RemoteControl(); Light livingRoomLight = new Light("Living Room");

Light kitchenLight = new Light("Kitchen");CeilingFan ceilingFan= new CeilingFan("Living

Room");GarageDoor garageDoor = new GarageDoor("");Stereo stereo = new Stereo("Living Room");

LightOnCommand livingRoomLightOn =

new LightOnCommand(livingRoomLight);

LightOffCommand livingRoomLightOff = new

LightOffCommand(livingRoomLight);LightOnCommand kitchenLightOn =

new LightOnCommand(kitchenLight);

LightOffCommand kitchenLightOff = new

LightOffCommand(kitchenLight);

Page 70: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan);

CeilingFanOffCommand ceilingFanOff = new

CeilingFanOffCommand(ceilingFan);

GarageDoorUpCommand garageDoorUp =new

GarageDoorUpCommand(garageDoor);GarageDoorDownCommand garageDoorDown =

new GarageDoorDownCommand(garageDoor);

StereoOnWithCDCommand stereoOnWithCD =new

StereoOnWithCDCommand(stereo);StereoOffCommand stereoOff =

new StereoOffCommand(stereo);

Page 71: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);

remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);

remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);remoteControl.setCommand(3, stereoOnWithCD, stereoOff);

System.out.println(remoteControl);

remoteControl.onButtonWasPushed(0);remoteControl.offButtonWasPushed(0);remoteControl.onButtonWasPushed(1);remoteControl.offButtonWasPushed(1);remoteControl.onButtonWasPushed(2);remoteControl.offButtonWasPushed(2);remoteControl.onButtonWasPushed(3);remoteControl.offButtonWasPushed(3);

}}

Page 72: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 73: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class NoCommand implements Command {public void execute() { }

}

Command noCommand = new NoCommand();for (int i = 0; i < 7; i++) {

onCommands[i] = noCommand;offCommands[i] = noCommand;

}

Page 74: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 75: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 76: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 77: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public interface Command {public void

execute();public void undo();

}public class LightOnCommand implements Command {

Light light;

public LightOnCommand(Light light) {this.light = light;

}

public void execute() {light.on();

}

public void undo() {light.off();

}}

Page 78: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class LightOffCommand implements Command {

Light light;

public LightOffCommand(Light light) {this.light = light;

}

public void execute() {light.off();

}

public void undo() {light.on();

}}

Page 79: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class RemoteControlWithUndo {Command[] onCommands;Command[] offCommands;Command undoCommand;

public RemoteControlWithUndo() {onCommands = new Command[7];offCommands = new Command[7];

Command noCommand = new NoCommand();for(int i=0;i<7;i++) {

onCommands[i] = noCommand;offCommands[i] = noCommand;

}undoCommand = noCommand;

}

public void setCommand(int slot, Command onCommand, Command offCommand) {

onCommands[slot] = onCommand;offCommands[slot] = offCommand;

}

Page 80: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public void onButtonWasPushed(int slot) {onCommands[slot].execute();undoCommand = onCommands[slot];

}

public void offButtonWasPushed(int slot) {offCommands[slot].execute();undoCommand = offCommands[slot];

}

public void undoButtonWasPushed() {undoCommand.undo();

}

public String toString() {// toString code here…

}}

Page 81: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class CeilingFan {public static final int HIGH = 3;public static final int MEDIUM = 2;public static final int LOW = 1;public static final int OFF = 0;String location;int speed;

public CeilingFan(String location) {this.location = location;speed = OFF;

}

public void high() {speed = HIGH;System.out.println(location + " ceiling fan is on high");

}

Page 82: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public void medium() {speed = MEDIUM;System.out.println(location + " ceiling fan is on medium");

}

public void low() {speed = LOW;System.out.println(location + " ceiling fan is on low");

}

public void off() {speed = OFF;System.out.println(location + " ceiling fan is off");

} public int getSpeed() {

return speed;}

}

Page 83: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

public class CeilingFanHighCommand implements Command {CeilingFan ceilingFan;int prevSpeed; public CeilingFanHighCommand(CeilingFan ceilingFan) {

this.ceilingFan = ceilingFan;} public void execute() {

prevSpeed = ceilingFan.getSpeed();ceilingFan.high();

} public void undo() {

if (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();

} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();

} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();

} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();

}}

}

Page 84: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 85: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 86: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 87: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 88: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
Page 89: CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.

Reference Material

Text Book :Head First Design Patterns by GOF(EBook uploaded on website )