Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Design Pattern Explained CH1

Overview CH1

  • Login to see the comments

  • Be the first to like this

Design Pattern Explained CH1

  1. 1. CH1. Object-Oriented Paradigm © Taka Wang, 2017 1
  2. 2. Naive Problem If I were to give you the task of wri2ng code to access a descrip2on of shapes that were stored in a database and then display them. © Taka Wang, 2017 2
  3. 3. Structural Programming thinking - possible steps 1. Locate the list of shapes in the database. ( ) 2. Open up the list of shapes. ( ) 3. Sort the list according to some rules. ( ) 4. Display the individual shapes on the monitor. ( ) © Taka Wang, 2017 3
  4. 4. Func%onal Decomposi%on ( ) 4a. Iden)fy the type of shape. ( ) 4b. Get the loca-on of the shape. ( ) 4c. Call the appropriate func2on that will display the shape, giving it the shape’s loca2on. ( Func2on) © Taka Wang, 2017 4
  5. 5. Func%onal Decomposi%on 1. (subprograms) subprograms (Why not delega7on?) << >> 2. Func7on module Func7on module << >> © Taka Wang, 2017 5
  6. 6. 1. ( ) 2. (OO ) 3. © Taka Wang, 2017 6
  7. 7. Dealing with Changes: Func2onal Decomposi2on 4c. Call the appropriate func2on that will display the shape, giving it the shape’s loca2on. ( Func2on) © Taka Wang, 2017 7
  8. 8. Dealing with Changes: Func2onal Decomposi2on 4c. Call the appropriate func2on that will display the shape, giving it the shape’s loca2on. ( Func2on) Using Modularity to Contain Varia2on function: display shape input: type of shape, description of shape action: switch (type of shape) case square: put display function for square here case circle: put display function for circle here © Taka Wang, 2017 8
  9. 9. 1. ( ) ( ) 2. type of shape, descrip5on of shape ( array of points ) © Taka Wang, 2017 9
  10. 10. Func%onal Decomposi%on 1. Weak cohesion (Cohesion refers to how “closely the opera6ons in a rou6ne are related; cohesion as clarity because the more that opera6ons are related in a rou6ne (or a class), the easier it is to understand things.) 2. Tight Coupling (Coupling refers to “the strength of a connec6on between two rou6nes. Coupling is a complement to cohesion.) The goal is to create rou/nes with internal integrity (strong cohesion) and small, direct, visible, and flexible rela/ons to other rou/nes (loose coupling). © Taka Wang, 2017 10
  11. 11. You are an instructor at a conference. People in your class have another class to a5end following yours, but don’t know where it is located. One of your responsibili>es is to make sure everyone knows how to get to the next class. © Taka Wang, 2017 11
  12. 12. 1. Get list of people in the class. 2. For each person on this list, do the following: 1. Find the next class he or she is taking. 2. Find the loca;on of that class. 3. Find the way to get from your classroom to the person’s next class. 4. Tell the person how to get to his or her next class. © Taka Wang, 2017 12
  13. 13. 1. A way of ge,ng the list of people in the class. 2. A way of ge,ng the schedule for each person in the class. 3. A program that gives someone direc<ons from your classrom to any other classroom. 4. A control program that works for each person in the class and does the reuqired steps for each person. © Taka Wang, 2017 13
  14. 14. ! You would probably post direc2ons to go from this classroom to the other classrooms and then tell everyone in the class, I have posted the loca.ons of the classes following this in the back of the room, as well as the loca.ons of the other classrooms. Please use them to go to your next classroom. © Taka Wang, 2017 14
  15. 15. 1. Giving explicit direc+ons to everyone. 2. Giving general instruc+ons and then expect the each person will figure out how to do the task individually. ( delega+on) © Taka Wang, 2017 15
  16. 16. 1. 2. : © Taka Wang, 2017 16
  17. 17. 1. 2. ( ) 3. © Taka Wang, 2017 17
  18. 18. 1. Conceptual: domain concepts What am I responsibile for? 2. Specifica9on: interface How am I used? 3. Implementa9on: How do I fulfill my responsibili9es?" © Taka Wang, 2017 18
  19. 19. (you are telling people what you want, not how to do it.) ( ) © Taka Wang, 2017 19
  20. 20. Object-Oriented Paradigm © Taka Wang, 2017 20
  21. 21. The objects were iden*fied by looking at the en**es in the problem domain. I iden*fied the responsibili1es (or methods) for each object by looking at what these en**es need to do. © Taka Wang, 2017 21
  22. 22. Apply Fowler's Perspec/ves • At the conceptual level, an object is a set of responsibili5es. ( ) • At the specifica-on level, an object is a set of methods (behaviors) that can be invoked by other objects or by itself. ( ) • At the implementa-on level, an object is code and data and computa5onal interac5ons between them. © Taka Wang, 2017 22
  23. 23. Object-Oriented Approach 1. Start the control program. 2. Instan4ate the collec4on of students in the classroom. 3. Tell the collec4on to have the students go to their next class. 4. The collec4on tells each student to go to his or her next class. 5. Each student: 1. Finds where his next class is. 2. Determines how to get there. 3. Goes there. 6. Done. © Taka Wang, 2017 23
  24. 24. Abstract and Polymorphism • Student abstract class, GraduageStudent and RegularStudent classes. • Abstract classes define what other, related, classes can do. Abstract classes act as placeholders for other classes. • Collec;on Student Concept (Abstract) Student (Concrete) • Derived Class (Polymorphism) © Taka Wang, 2017 24
  25. 25. 1. Locate the list of shapes in the database. ( ) 2. Open up the list of shapes. ( ) 3. Sort the list according to some rules. ( ) 4. Display the individual shapes on the monitor. ( ) © Taka Wang, 2017 25
  26. 26. Object-Oriented Approach (1/2) © Taka Wang, 2017 26
  27. 27. Object-Oriented Approach (2/2) 1. ShapeDataBase 2. database object instan0ate collec0on . 3. Collec0on 4. Collec0on 5. Collec0on 6. © Taka Wang, 2017 27
  28. 28. • ( ) 1. Concrete Class 2. Derived Class • 1. Collec6on Sor6ng algorithm The object-oriented approach has limited the impact of changing requirements. © Taka Wang, 2017 28
  29. 29. 1. 2. ( depend on interface, not implementa4on) 3. The internals of an object are unknown to other objects. ( < >) © Taka Wang, 2017 29
  30. 30. Take away 1. 2. ( ) 3. OO 4. (placeholder and delega5on) 5. ( ) © Taka Wang, 2017 30
  31. 31. Thank you1 1 ( ) © Taka Wang, 2017 31

×