jgs CSE 564 Software Design Lecture 18: More Patterns II Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
jgs 564 00010010 Strategy Context context; // Three contexts following different strategies context = new Context(new ConcreteStrategyA()); context.ContextInterface(); context = new Context(new ConcreteStrategyB()); context.ContextInterface(); context = new Context(new ConcreteStrategyC()); context.ContextInterface();
jgs Composite Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly
jgs 564 00010010 Adapter class RectangleAdapter implements Shape { private Rectangle adaptee; public RectangleAdapter(Rectangle rectangle) { this.adaptee = rectangle; } public void draw(int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x2 - x1); int height = Math.abs(y2 - y1); adaptee.draw(x, y, width, height); } }
jgs 564 00010010 Adaptee class Rectangle { public void draw(int x, int y, int width, int height) { System.out.println("Rectangle with coordinate left-down point”); } }
jgs 564 00010010 Bridge Abstraction ab = new RefinedAbstraction(); // Set implementation and call ab.setImplementor (new ConcreteImplementorA() ); ab.Operation(); // Change implemention and call ab.setImplementor (new ConcreteImplementorB() ); ab.Operation();
jgs 564 00010010 Strategy vs Bridge Bridge Pattern makes a distinction between an abstraction and an implementation in such a way that the two can vary independently Car - Engine changing the behavior of an object at run time. Game: Weapons
jgs CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.