Software Design More Design Patterns (202110)
jgsCSE 564Software DesignLecture 17: More PatternsDr. Javier Gonzalez-Sanchez[email protected]javiergs.engineering.asu.edu | javiergs.comPERALTA 230UOffice Hours: By appointment
View Slide
jgs564 00010001GoF Patterns
jgsFactoryWrapper of a constructorOne entire object to be built in a single method call
jgs564 00010001Abstract Factory
jgs564 00010001Productspublic interface Shape {void draw();}public class RoundedRectangle implements Shape {public void draw() {System.out.println("Inside RoundedRectangle/draw() method.");}}public class RoundedSquare implements Shape {public void draw() {System.out.println("Inside RoundedSquare/draw() method.");}}public class Rectangle implements Shape {public void draw() {System.out.println("Inside Rectangle/draw() method.");}}
jgs564 00010001Factoriespublic abstract class AbstractFactory {Shape getShape(String shapeType) ;}public class ShapeFactory extends AbstractFactory {public Shape getShape(String shapeType){if(shapeType.equalsIgnoreCase("RECTANGLE")){return new Rectangle();}else if(shapeType.equalsIgnoreCase("SQUARE")){return new Square();}return null;}}
jgs564 00010001Factoriespublic class RoundedShapeFactory extends AbstractFactory {public Shape getShape(String shapeType){if(shapeType.equalsIgnoreCase("RECTANGLE")){return new RoundedRectangle();}else if(shapeType.equalsIgnoreCase("SQUARE")){return new RoundedSquare();}return null;}}
jgs564 00010001Clientpublic class Client {public static void main(String[] args) {AbstractFactory shapeFactory = new ShapeFactory();//get an object of Shape RectangleShape shape1 = shapeFactory.getShape("RECTANGLE");//call draw method of Shape Rectangleshape1.draw();//get an object of Shape SquareShape shape2 = shapeFactory.getShape("SQUARE");//call draw method of Shape Squareshape2.draw(); ]//get shape factoryAbstractFactory shapeFactory1 = new RoundedShapeFactory();//get an object of Shape RectangleShape shape3 = shapeFactory1.getShape("RECTANGLE");//call draw method of Shape Rectangleshape3.draw();//get an object of Shape SquareShape shape4 = shapeFactory1.getShape("SQUARE");//call draw method of Shape Squareshape4.draw();}}
jgsTest Yourselves
jgs564 00010001Workspace.java (part 1 / 2)
jgs564 00010001UserDBAC
jgs564 00010001The SystemDBACrandom
jgsChain of ResponsibilityAvoid coupling the sender of a request to its receiverby giving more than one object a chance to handle therequest.
jgs564 00010001Chain of Responsibility
jgs564 00010001Chain of Responsibility// Setup Chain of ResponsibilityHandler h1 = new ConcreteHandler2();Handler h2 = new ConcreteHandler2();Handler h3 = new ConcreteHandler1();h1.SetSuccessor(h2);h2.SetSuccessor(h3);// Generate and process requestint[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };foreach (int i=0; ih1.HandleRequest(request[i]);}
jgs564 00010001Handlerabstract class Handler {public abstract void HandleRequest(int request);}class ConcreteHandler1 extends Handler {public override void HandleRequest(int request) {if (request >= 0 && request < 10) {// code here…} else if (successor != null) {successor.HandleRequest(request);}}}
jgs564 00010001Handlerclass ConcreteHandler2 extends Handler {protected Handler successor;public void setSuccessor(Handler successor) {this.successor = successor;}public override void HandleRequest(int request) {if (request >= 10 && request < 20) {// code here} else if (successor != null) {successor.HandleRequest(request);}}}
jgs564 00010001Chain of Responsibility vs Composite vs Decorator
jgs564 00010001The SystemDBACDBACClusteringTSP -Nearest Neighbor
jgs564 00010001Questions
jgsCSE 564 Software DesignJavier Gonzalez-Sanchez, Ph.D.[email protected]Fall 2021Copyright. These slides can only be used as study material for the class CSE564 at ASU.They cannot be distributed or used for another purpose.