Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2009ACS-3913 Ron McFadyen1 From the Merriam-Webster’s online dictionary (www.m-w.com): Main Entry: an·thro·po·mor·phism Date: 1753 an interpretation.

Similar presentations


Presentation on theme: "Fall 2009ACS-3913 Ron McFadyen1 From the Merriam-Webster’s online dictionary (www.m-w.com): Main Entry: an·thro·po·mor·phism Date: 1753 an interpretation."— Presentation transcript:

1 Fall 2009ACS-3913 Ron McFadyen1 From the Merriam-Webster’s online dictionary (www.m-w.com): Main Entry: an·thro·po·mor·phism Date: 1753 an interpretation of what is not human or personal in terms of human or personal characteristics : HUMANIZATIONHUMANIZATION Anthropomorphism

2 Fall 2009ACS-3913 Ron McFadyen2 Anthropomorphism Anthropomorphism: Object-oriented programming works like human organizations. Each object will communicate with another one by sending messages. So the software objects work by just sending those messages.

3 Fall 2009ACS-3913 Ron McFadyen3 Responsibility-Driven Design (RDD) Detailed object design is usually done from the point of view of: –Objects have responsibilities –Objects collaborate –Similar to how we conceive of people In RDD we do object design such that we will ask questions such as: –What are the responsibilities of this object? –Who does it collaborate with?

4 Fall 2009ACS-3913 Ron McFadyen4 Architectural Layers

5 Fall 2009ACS-3913 Ron McFadyen5 Sample Problem 1 What object should receive this message? How should objects interact to fulfill the request? How do we justify out decision?

6 Fall 2009ACS-3913 Ron McFadyen6 Sample Problem 2 How should the objects interact in order for changes in the data to be reflected in the two displays? Graphic Display List Display Data User How do we justify our decision?

7 Fall 2009ACS-3913 Ron McFadyen7 idea was first put forth by Christopher Alexander (1977) in his work on architectural design principles a pattern is a named problem/solution pair that can be applied in new contexts advice from previous designers to help designers in new situations rules of thumb - not new ideas There are many books on the subject; examples: Design Patterns - Erich Gamma et al Java Design Patterns: a tutorial - James Cooper Design Patterns Java Workbook - Steven John Metsker Data Access Patterns - Clifton Nock Patterns of Enterprise Application Architecture – Martin Fowler Patterns

8 Fall 2009ACS-3913 Ron McFadyen8 Guiding principles to help us assign responsibilities GRASP: –General Responsibility Assignment Software Patterns –Very very fundamental, simple, basic principles of object design. –Developed by Craig Larman GRASP Patterns Fundamental Principles of Object Design

9 Fall 2009ACS-3913 Ron McFadyen9 GRASP Patterns Expert Creator Controller Low Coupling High Cohesion

10 Fall 2009ACS-3913 Ron McFadyen10 What object in the domain (or application layer) receives requests for work from the UI layer? Controller

11 Fall 2009ACS-3913 Ron McFadyen11 Basic Principle: Interface objects should not have responsibility for handling system events Controller: a non-user interface object responsible for receiving or handling a system event. In the Process Sale Use Case, there are several system events: makeNewSale, enterItem, endSale, makePayment The Controller doesn’t do much … it should delegate work to other objects … it receives the request & coordinates fulfillment

12 Fall 2009ACS-3913 Ron McFadyen12 makeNewSale enterItem endSale makePayment System Operations

13 Fall 2009ACS-3913 Ron McFadyen13 In general there are two candidates: –An object whose name reflects the use case. e.g. ProcessSaleHandler –An object whose name reflects the overall server, business, or large-scale entity. A kind of “façade” object e.g. Register, Store, Cashier Choose or invent an object in the application layer for this.

14 Fall 2009ACS-3913 Ron McFadyen14 F Register, Store, Cashier F Register is chosen in the text

15 Fall 2009ACS-3913 Ron McFadyen15 Allocating System Operations Register has been chosen to handle all system operations Ch 20 shows the code for this If Use Case handlers were chosen

16 Fall 2009ACS-3913 Ron McFadyen16 The Controller pattern promotes reuse UI code is not intertwined with system event code UI can be replaced Multiple UIs could be utilized When a legal sequence of operations must occur, state information must be kept … a Controller object is an excellent choice for this information

17 Fall 2009ACS-3913 Ron McFadyen17 F A controller is class whose job it is to coordinate the system events F The controller sees to it the messages are sent to the correct objects in the model – it delegates F The reason to have a controller is to separate the business model from the visual logic called a view

18 Fall 2009ACS-3913 Ron McFadyen18 F Advantage - is that the changes to the model do not affect the GUI (view) logic F Advantage - is that the changes to the GUI (view) do not affect the model logic – could have multiple GUIs – GUI is replaceable F It provides a buffer between the visual (view) and the business logic (model)

19 Fall 2009ACS-3913 Ron McFadyen19 desirable coupling of interface layer to domain layer

20 Fall 2009ACS-3913 Ron McFadyen20 Expert Assign a responsibility to the object that has the information necessary to fulfill it. –“That which has the information, does the work.” –Not a sophisticated idea - rather, it is common sense –E.g., What software object calculates grand total? What information is needed to do this? What object has the majority of this information.

21 Fall 2009ACS-3913 Ron McFadyen21 Expert Example. In the NextGEN POS application, it is necessary to know the grand total of a sale. Where should that responsibility be placed? {We will be assigning a few responsibilities in this example} Expert suggests that we should look for a class that has the information needed to determine the grand total. If our design is just beginning, we look at the Domain Model and bring the pertinent conceptual classes into the class model

22 Fall 2009ACS-3913 Ron McFadyen22 What information is needed to determine the grand total? It is necessary to know all the SalesLineItem instances of a sale and to sum their subtotals. A Sale instance is aware of these … Sale is the Expert choice for having the responsibility of knowing the grand total. Sale Sales LineItem Product Description contains Described by 1 1..* * 1

23 Fall 2009ACS-3913 Ron McFadyen23 Consider figure 17.15 … Expert leads us to place the method getTotal() in Sale

24 Fall 2009ACS-3913 Ron McFadyen24 Consider figure 17.16 … A line item knows its quantity and the associated Product, so it is the expert … SalesLineItem should determine the line item subtotal … another method, getSubTotal() required in SalesLineItem

25 Fall 2009ACS-3913 Ron McFadyen25 Consider Figure 17.17 … Only the ProductDescription knows the price; so ProductDescription needs a method... … another method getPrice() required in ProductDescription

26 Fall 2009ACS-3913 Ron McFadyen26 Creator What object should have the responsibility to create an X? –Ignores special-case patterns such as Factory… later GoF Choose an object C, such that: –C contains or compositely aggregates X –C closely uses X –C records instances of X objects –C has the initializing data for X

27 Fall 2009ACS-3913 Ron McFadyen27 Example: Who should be responsible for creating a SalesLineItem? Since Sale “contains” SalesLineItems, Creator suggests Sale as the candidate for this responsiblity Sale Sales LineItem Product Description contains Described by 1 1..* * 1

28 Fall 2009ACS-3913 Ron McFadyen28 :Register :SalesLineItem makeLineItem(qty) create(qty) :Sale We assign the responsibility of creating a SalesLineItem to Sale – Sale will have a method makeLineItem

29 Fall 2009ACS-3913 Ron McFadyen29 Low Coupling High Cohesion We use the same example, “creating a payment”, for both of these patterns. Both patterns happen to suggest the same collaboration

30 Fall 2009ACS-3913 Ron McFadyen30 Low Coupling When we need to assign a responsibility to a class, we should do so such that coupling remains low. Coupling: a measure of how strongly one element is connected to, has knowledge of, or relies on other elements. Low coupling: not dependent on too many other elements High coupling results in –classes that are harder to understand in isolation –changes to related classes force local changes –classes that are harder to reuse

31 Fall 2009ACS-3913 Ron McFadyen31 Low Coupling Example Assume we need to create a Payment instance … what class should do this?

32 Fall 2009ACS-3913 Ron McFadyen32 Creator pattern suggests Register Register creates Payment (Collaboration diagram)

33 Fall 2009ACS-3913 Ron McFadyen33 An alternative design Which of the two designs supports lower coupling?

34 Fall 2009ACS-3913 Ron McFadyen34 High Cohesion How do we assign responsibilities so that cohesion remains high? Cohesion: a measure of how strongly related and focused the responsibilities of an element are. –Cohesion is a measure of how single purpose the attributes and behavior within a class are –It is better that attributes and behaviour in classes be related. A class with highly related responsibilities and which does not do excessive amounts of work has high cohesion

35 Fall 2009ACS-3913 Ron McFadyen35 High Cohesion Rule of Thumb (ROT): A class with high cohesion has a relatively small number of methods, with highly related functionality, and does not do too much work … it collaborates with others to get work done. Low cohesion results in classes that –are hard to comprehend –hard to reuse – hard to maintain – delicate - constantly affected by change

36 Fall 2009ACS-3913 Ron McFadyen36 High Cohesion Example Assume we need to create a Payment instance … what class should do this?

37 Fall 2009ACS-3913 Ron McFadyen37 First solution

38 Fall 2009ACS-3913 Ron McFadyen38 second solution Which solution supports higher cohesion for Register?

39 Fall 2009ACS-3913 Ron McFadyen39 Note, when considering which class to assign the responsibility of creating a payment, the text arrives at the same solution when applying both principles


Download ppt "Fall 2009ACS-3913 Ron McFadyen1 From the Merriam-Webster’s online dictionary (www.m-w.com): Main Entry: an·thro·po·mor·phism Date: 1753 an interpretation."

Similar presentations


Ads by Google