Presentation is loading. Please wait.

Presentation is loading. Please wait.

MAHARISHI INTERNATIONAL UNIVERSITY 1971-1995 M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature.

Similar presentations


Presentation on theme: "MAHARISHI INTERNATIONAL UNIVERSITY 1971-1995 M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature."— Presentation transcript:

1 MAHARISHI INTERNATIONAL UNIVERSITY 1971-1995 M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature

2 CS Faculty Computer Science Department Advanced Software Development

3 Lab 8: Patterns: Bridge Lab

4 Bridge : Motivation 2

5

6

7

8 Bridge : Structure

9 Bridge : Example 2

10

11 class Client { public static void main (String argv[]) { Rectangle s1, s2; s1= new V1Rectangle( (double)1,(double)1,(double)2,(double)2); s2= new V2Rectangle( (double)1,(double)1,(double)3,(double)3); s1.draw(); s2.draw(); } Bridge : Example 2

12 public abstract class Rectangle { double _x1, _y1,_x2,_y2; public Rectangle( double x1, double y1, double x2, double y2) { _x1= x1; _y1= y1; _x2= x2; _y2= y2; } public void draw () { drawLine(_x1, _y1, _x1, _y2); drawLine(_x1, _y1, _x2, _y1); drawLine(_x1, _y2, _x2, _y2); drawLine(_x2, _y1, _x2, _y2); } abstract void drawLine ( double x1, double x2, double y1, double y2); } public class V1Rectangle extends Rectangle { public V1Rectangle( double x1, double y1, double x2, double y2) { super(x1,y1,x2,y2); } public void drawLine( double x1, double y1,double x2, double y2) { DP1.draw_a_line( x1,y1,x2,y2); } class V2Rectangle extends Rectangle { public V2Rectangle( double x1, double y1, double x2, double y2) { super(x1,y1,x2,y2); } public void drawLine(double x1, double y1,double x2, double y2) { DP2.drawline( x1,x2,y1,y2); }

13 class DP1 { static void draw_a_line ( double x1, double y1,double x2, double y2) { String sx1=String.valueOf(x1); String sy1=String.valueOf(y1); String sx2=String.valueOf(x2); String sy2=String.valueOf(y2); System.out.println("DP1: draw line: x1="+sx1+" y1="+sy1+" x2="+sx2+" y2="+sy2); } static void draw_a_circle( double x, double y, double radius) { String sx=String.valueOf(x); String sy=String.valueOf(y); String sradius=String.valueOf(radius); System.out.println("DP1: draw circle: x="+sx+" y="+sy+" radius="+sradius); }} class DP2 { static void drawline ( double x1, double y1, double x2, double y2) { String sx1=String.valueOf(x1); String sy1=String.valueOf(y1); String sx2=String.valueOf(x2); String sy2=String.valueOf(y2); System.out.println("DP2: draw line: x1="+sx1+" y1="+sy1+" x2="+sx2+" y2="+sy2); } static void drawcircle( double x, double y,double radius) { String sx=String.valueOf(x); String sy=String.valueOf(y); String sradius=String.valueOf(radius); System.out.println("DP2: draw circle: x="+sx+" y="+sy+" radius="+sradius); }

14 Bridge : Example 2

15 Get New Requirements Told we must now handle Circles I decide to handle it in a similar way. Make a V1Circle class and a V2Circle class. Tie Circles and Rectangles together by having them derive from new Shape class. Bridge : Example 2

16

17 class Client { public static void main (String argv[]) { Shape s1, s2, s3, s4; s1= new V1Rectangle( (double)1,(double)1,(double)2,(double)2); s2= new V2Rectangle( (double)1,(double)1,(double)3,(double)3); s3=new V1Circle((double)1,(double)1,(double)3); s4=new V2Circle((double)4,(double)1,(double)2); s1.draw(); s2.draw(); s3.draw(); s4.draw(); } abstract class Shape { abstract public void draw (); } Bridge : Example 2

18 public abstract class Rectangle extends Shape { double _x1, _y1,_x2,_y2; public Rectangle( double x1, double y1, double x2, double y2) { _x1= x1; _y1= y1; _x2= x2; _y2= y2; } public void draw () { drawLine(_x1, _y1, _x1, _y2); drawLine(_x1, _y1, _x2, _y1); drawLine(_x1, _y2, _x2, _y2); drawLine(_x2, _y1, _x2, _y2); } abstract void drawLine ( double x1, double x2, double y1, double y2); } public abstract class Circle extends Shape { protected double _x, _y, _radius; public Circle (double x, double y, double radius) { _x= x; _y= y; _radius= radius; } public abstract void draw (); }

19 Bridge : Example 2 public class V1Rectangle extends Rectangle { public V1Rectangle( double x1, double y1, double x2, double y2) { super(x1,y1,x2,y2); } public void drawLine( double x1, double y1,double x2, double y2) { DP1.draw_a_line( x1,y1,x2,y2); } class V2Rectangle extends Rectangle { public V2Rectangle( double x1, double y1, double x2, double y2) { super(x1,y1,x2,y2); } public void drawLine(double x1, double y1,double x2, double y2) { DP2.drawline( x1,x2,y1,y2); }

20 Bridge : Example 2 class V1Circle extends Circle { public V1Circle (double x, double y, double radius){ super(x, y, radius); } public void draw () { DP1.draw_a_circle(_x,_y,_radius); } class V2Circle extends Circle { public V2Circle (double x, double y, double radius){ super(x, y, radius); } public void draw () { DP2.drawcircle( _x,_y,_radius); }

21 Bridge : Example 2 Problems With This Approach Doesn’t scale well –if add another type of Shape, we have 6 implementations –number of implementations will equal # types of Shapes times # of drawing programs Redundancy –clearly accessing each of the drawing programs across the different types of Shapes will involve duplication or require greater modularization Confusing and complex

22 Bridge : Example 2 Changing the inheritance hierarchy doesn’t get rid of the problems

23 Bridge : Lab Solution: Use the Bridge pattern. Exercise: Draw the class diagram of our drawing application with the Bridge pattern applied. Lab: implement the drawing application with the Bridge pattern applied.

24 © 2000 Maharishi University of Management, Fairfield, Iowa, USA This tape may not be copied, duplicated, or distributed without written consent from Maharishi University of Management, Fairfield, Iowa, USA.

25 ® Science of Creative Intelligence is a service mark registered in the United States Patent and Trademark Office, licensed to Maharishi Vedic Education Development Corporation, and is used under sublicense.


Download ppt "MAHARISHI INTERNATIONAL UNIVERSITY 1971-1995 M AHARISHI U NIVERSITY of M ANAGEMENT Engaging the Managing Intelligence of Nature."

Similar presentations


Ads by Google