Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns Today: Intro to Topic designpatterns.f12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger.

Similar presentations


Presentation on theme: "Design Patterns Today: Intro to Topic designpatterns.f12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger."— Presentation transcript:

1 Design Patterns Today: Intro to Topic http://www.soapatterns.org designpatterns.f12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger

2 – 2 – CS 121 Due This Week Finished ProposalFinished Proposal PrototypePrototype Review of Phase 1Review of Phase 1 2 nd Floor – who is using what?2 nd Floor – who is using what?

3 – 3 – CS 121 Design PracticesPrinciplesPatterns What are the characteristics of good design? What are good solutions to common design problems? How do we go about design and what do we produce? LAST TIME TODAY: Design patterns

4 – 4 – CS 121 Goals 1.Make it easy to build 2.Make it easy to test 3.Make it easy to maintain 4.Make it easy to change SIMPLE FLEXIBLE INTUITIVE

5 – 5 – CS 121 Design principles Don’t repeat yourself (D.R.Y) Use real world objects Single responsibility principle Encapsulate variation High cohesion/low coupling Program to an interface, not an implementation Law of Demeter (talk only to your friends) Favor composition over inheritance Open-closed principle Liskov Substitution Principle

6 – 6 – CS 121 Design Patterns Good solution to common problem Also illustrates good design principles in action An area of research in both hardware and software for years

7 – 7 – CS 121 Design Patterns The simplest way to describe a pattern is that it provides a proven solution to a common problem individually documented in a consistent format and usually as part of a larger collection.

8 – 8 – CS 121 Design Patterns are helpful because they: represent field-tested solutions to common design problems organize design intelligence into a standardized and easily "referencable" format are generally repeatable by most IT professionals involved with design can be used to ensure consistency in how systems are designed and built can become the basis for design standards are usually flexible and optional (and openly document the impacts of their application and even suggest alternative approaches) can be used as educational aids by documenting specific aspects of system design (regardless of whether they are applied) can sometimes be applied prior and subsequent to the implementation of a system can be supported via the application of other design patterns that are part of the same collection enrich the vocabulary of a given IT field because each pattern is given a meaningful name

9 – 9 – CS 121 Problem: I want a value in multiple places?....Mike CLASS: Ball double gravity 32.1 Private: CLASS: Enemy double gravity 32.1 Private: D.R.Y.!

10 – 10 – CS 121 How about this? Global double gravity 3.21

11 – 11 – CS 121 Why not use globals? A.They make code hard to understand. B.They make code hard to debug. C.They make code hard to modify.

12 – 12 – CS 121 Answer All of the above.

13 – 13 – CS 121 Solution: Singleton Pattern Problem: Ensure a class has only one instance and provide a global point of access to that instance. Way to control Global Variables Way to control Global Variables

14 – 14 – CS 121 Problem: I want … Lihue2 I want a 2D graphics library that supports the following functions for triangles: set color to r,g,b translate vertices by dx, dy rotate  degrees about the origin draw

15 – 15 – CS 121 What I Have I have a 3D graphics library with a triangle class with the following interface triangle() triangle(v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z) ~triangle() set color(r, g, b) rotate(vector, angle) translate(dx, dy, dz) scale(sx, sy, sz) draw() flip(planeA, planeB, planeC, planeD) texture(textureMap) standardize()

16 – 16 – CS 121 How About: Just use the 3d class Constructor: triangle t(v1x, v1y, 0, v2x, v2y, 0, v3x, v3y, 0) Rotate:t.rotate(<0,0,1>,alpha) Interface Segregation Principle The dependency of one class to another one should depend on the smallest possible interface. Liskov principle

17 – 17 – CS 121 Solution: Façade Pattern Problem: You need to use a subset of a complex system or you need to interact with the system in a particular way. You need to use a subset of a complex system or you need to interact with the system in a particular way.

18 – 18 – CS 121 Problem: What I want… Linue1 I want a physics engine that (among other things) detects collisions: cCollision cPhysicsEngine::detectCollision(cPath p, cTriangles t) cCollision cPhysicsEngine::detectCollision(cPath p, cTriangles t) I have a fast collision detection algorithm and a slower, more robust algorithm.

19 – 19 – CS 121 cPhysicsEngine cPhysicsFast How about this? cPhysicsSlow In the future I may want to use a super slow algorithm.

20 – 20 – CS 121 Solution: Strategy Design Pattern Problem: Want to be able to swap the algorithm used in an application.

21 – 21 – CS 121 cPhysicsEnginecDetectCollision cDetectCollisionFast Strategy Design Pattern cDetectCollisionSlow encapsulate change single responsibility open-closed principle Favor composition over inheritance

22 – 22 – CS 121 Problem: I want … Sycamore I am developing software that (among other things) displays geometric primitives Initially I only need to support lines In the future I may need to add spheres, triangles, and squares Rather than reinvent the wheel, I am going to use an existing program. Actually, I really want the option of choosing at run time between two different drawing programs, one that is better at low resolutions and one that is better at high resolutions

23 – 23 – CS 121 …current design shape line draw()

24 – 24 – CS 121 Drawing APIs Package 1: drawLine(x1, y1, x2, y2) Package 2: drawALine(x1, x2, y1, y2) Notice difference in Parameter order

25 – 25 – CS 121 What about… line.draw() { if (resolution == high) drawLine(v1.x, v1.y, v2.x, v2.y) else drawALine(v1.x, v2.x, v1.y, v2.y) }

26 – 26 – CS 121 Comments: Advantages simple to implement simple to understand Disadvantages is knowing about resolution really a responsibility of shape? D.R.Y.

27 – 27 – CS 121 What about line lineDP1lineDP2

28 – 28 – CS 121 Comments: Advantages simple to implement simple to understand Disadvantages as additional shapes and drawing programs are added the number of classes becomes LARGE

29 – 29 – CS 121 Solution: Bridge Design Pattern Problem: Want to support multiple implementation that have different interfaces in an extensible way.

30 – 30 – CS 121 Shape TriangleLine Drawer Low ResHi Res Solution: Bridge Design Pattern defines the interface shapes use to draw “adapters” for specific drawing interfaces

31 – 31 – CS 121 Bridge Pattern vs. Strategy Pattern Shape Drawer Low Res Hi Res cPhysicsEnginecDetectCollision cDetectCollisionFastcDetectCollisionSlow Different intents: bridge allows implementation to vary and includes adapters strategy allows algorithms (behavior) to vary DP hi res

32 – 32 – CS 121 Problem: I want …. Rio de Valle I am building a drawing program. The user enters keystrokes to change modes (Add, Delete, Move) and mouse input that is interpreted based on the current mode.

33 – 33 – CS 121 Drawer processKey processMouse DeleteMode Drawer AddMode Drawer MoveMode Drawer How about this? What are its disadvantages?

34 – 34 – CS 121 Drawer processKey processMouse Mode processMouse DeleteAddMove State Design Pattern 1 supports open-closed and single responsibility principles

35 – 35 – CS 121 Drawer processKey processMouse Mode processMouse DeleteAddMove State Design Pattern 1 ModeManager processKey 11 1 1 Mode mgr. returns pointer to correct mode

36 – 36 – CS 121 Solution: State Design Pattern Problem: want to allow an object to alter its behavior when its internal state changes

37 – 37 – CS 121 State Pattern vs. Strategy Pattern Drawer processKey processMouse Mode processMouse DeleteAddMove 1 ModeManager processKey 11 1 1 cPhysicsEnginecDetectCollision cDetectCollisionFastcDetectCollisionSlow Different intents: state allows behaviors to vary dynamically strategy typically used when algorithm is selected at start

38 – 38 – CS 121 Problem…. Continued…Mike I also want to support “Undo” Help!

39 – 39 – CS 121 Command MouseKeyMenu Solution: Command Design Pattern

40 – 40 – CS 121 Command Design Pattern Encapsulate a request as an object to permit logging, queuing, un-doing etc.

41 – 41 – CS 121 Problem: I want I want a 2D drawing program that supports triangle and lines I want to be able to add, delete, draw, and move primitives. I want to be also want to be able to group primitives into a “widget” and treat the widget as a primitive. I want to be able to add and delete primitives from a widget

42 – 42 – CS 121 What about…. WidgetShape * TriangleLine What is the difference between a triangle and a widget holding a triangle?

43 – 43 – CS 121 Composite Design Pattern Widget Shape * TriangleLine

44 – 44 – CS 121 Observer Design Pattern

45 – 45 – CS 121 Observer Design Pattern

46 – 46 – CS 121 Other design patterns wikipedia!

47 – 47 – CS 121 Why Design Patterns? When you have a thorny problem and you think someone must have run into it before … think design patterns

48 – 48 – CS 121 The End

49 – 49 – CS 121 Solution Triangle2DTriangle3D implements the 2d triangle interface


Download ppt "Design Patterns Today: Intro to Topic designpatterns.f12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger."

Similar presentations


Ads by Google