CSC/ECE 517 Fall 2007/wiki3 6 pm: Difference between revisions
No edit summary |
|||
(31 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
''Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.'' | ''Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.'' | ||
__TOC__ | |||
=Design Patterns= | =Design Patterns= | ||
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern] refers to a named description of a problem and a solution that can be applied in different contexts. Patterns are ''proven solutions to common problems''. | A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern] refers to a named description of a problem and a solution that can be applied in different contexts. Patterns are ''proven solutions to common problems''. | ||
A pattern addresses a recurring design problem that arises in specific design situations and presents a solution to it. This is done by defining a set of rules which describes how to solve the problem at hand. | |||
==GRASP Patterns== | ==GRASP Patterns== | ||
[http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design) GRASP] refers to '''G'''eneral '''R'''esponsibility '''A'''ssignment '''S'''oftware '''P'''atterns. It includes a systematic approach to [http://en.wikipedia.org/wiki/Object_oriented_design Object Oriented Design], while assigning responsibilities to classes. | [http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design) GRASP] refers to '''G'''eneral '''R'''esponsibility '''A'''ssignment '''S'''oftware '''P'''atterns. It includes a systematic approach to [http://en.wikipedia.org/wiki/Object_oriented_design Object Oriented Design], while assigning responsibilities to classes. | ||
GRASP Patterns are more of responsibility driven design. | |||
Responsibilities of an Object include two types : ''Knowing'' and ''Doing'' | |||
''Doing'' responsibilities of an object include: | |||
*Doing something itself, such as creating an object or doing a calculation | |||
*Initiating action in other objects | |||
*Controlling and coordinating activities in other objects | |||
''Knowing'' responsibilities of an object include: | |||
*Knowing about private encapsulated data | |||
*Knowing about related objects | |||
*Knowing about things it can derive or calculate | |||
The GRASP Patterns include: | The GRASP Patterns include: | ||
Line 20: | Line 41: | ||
Some of these, such as "Low Coupling" and "High Cohesion" are merely good design principles, and not design patterns. | Some of these, such as "Low Coupling" and "High Cohesion" are merely good design principles, and not design patterns. | ||
=The Controller Pattern= | =The Controller Pattern= | ||
Line 31: | Line 51: | ||
* A class that represents a ''use case scenario'' within which the system event occurs. These are often named <usecasename>Handler, <usecasename>Controller, <usecasename>Manager,and so forth. | * A class that represents a ''use case scenario'' within which the system event occurs. These are often named <usecasename>Handler, <usecasename>Controller, <usecasename>Manager,and so forth. | ||
To choose between facade and use-case controllers: | |||
1. The ''Facade controller'' is suitable when there are few system events. | |||
2. The ''Use-case controller'' is applicable when there are many system events and the facade controller would have become too large | |||
and incohesive. | |||
Wikipedia succinctly states: ''"The [http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design) Controller] pattern assigns the responsibility of dealing with system events to a non-UI class that represent the overall system or a use case scenario. A use case controller should be used to deal with all system events of a use case, and may be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate use case controllers)"'' | Wikipedia succinctly states: ''"The [http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design) Controller] pattern assigns the responsibility of dealing with system events to a non-UI class that represent the overall system or a use case scenario. A use case controller should be used to deal with all system events of a use case, and may be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate use case controllers)"'' | ||
Line 41: | Line 64: | ||
*''Reuse'' of domain layer software by plugging in various interfaces | *''Reuse'' of domain layer software by plugging in various interfaces | ||
*It is possible to ''control'' out-of-sequence system events | *It is possible to ''control'' out-of-sequence system events | ||
== Controller vs MVC == | |||
This page disambiguates the MVC part as well: | |||
[http://fredrik.nsquared2.com/ViewPost.aspx?PostId=420 Controller Pattern and MVC] | |||
=Examples= | =Examples= | ||
== | ==Example 1== | ||
[http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/responsibilities.html Reference 1] contains an excellent description of the controller pattern. | [http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/responsibilities.html Reference 1] contains an excellent description of the controller pattern. | ||
Line 55: | Line 83: | ||
*A system operation refers to the response of the system to a system event. | *A system operation refers to the response of the system to a system event. | ||
[[Image:controller6.jpg]] | |||
The figure below shows allocation of different operations | |||
[[Image:controller4.jpg]] | |||
[[Image:controller5.jpg]] | |||
The | == Example 2 == | ||
The game of [http://www.academic.marist.edu/~jzbv/SoftwareDevelopment/GRASP.ppt Monopoly] is an excellent example of a controller pattern. Actors, such as the human player in Monopoly, generate UI events (such as clicking a button with a mouse to play a game or make a move). The UI software objects (such as a JFrame window and a JButton) must process the event and cause the game to play. When objects in the UI layer pick up an event, they must delegate the request to an object in the domain layer. | |||
The problem in this case can be formulated as follows: | |||
What first object beyond the UI layer should receive the message from the UI layer? | |||
== | As per the controller pattern the solution is as follows: | ||
Assign the responsibility to an object representing either of following: | |||
- Represents the overall “system” – a root object | |||
- Represents a use case scenario within which the system operation occurs. | |||
[[Image:monopoly.jpg]] | |||
==Example 3== | |||
This article contains an [http://www.davidhayden.com/blog/dave/archive/2004/12/10/680.aspx E-Commerce example] of the Controller Pattern. The author classifies controllers as '''Use case Controllers''' and '''Facade Controllers'''. | This article contains an [http://www.davidhayden.com/blog/dave/archive/2004/12/10/680.aspx E-Commerce example] of the Controller Pattern. The author classifies controllers as '''Use case Controllers''' and '''Facade Controllers'''. | ||
Line 83: | Line 128: | ||
ProductCatalogHandler and ShoppingCartHandler are highly cohesive, embodied by the Controller Pattern. | ProductCatalogHandler and ShoppingCartHandler are highly cohesive, embodied by the Controller Pattern. | ||
== Example 4== | |||
== | |||
[http://209.85.165.104/search?q=cache:Mgu-Um9Lx1sJ:www.soc.napier.ac.uk/module.php3%3Fop%3Dgetresource%26cloaking%3Dno%26resourceid%3D5938601+controller+pattern+GRASP&hl=en&ct=clnk&cd=15&gl=us Reference 6] contains the following solution to the problem of who handles the system event: "If a program receives events from external sources other than its graphical interface, add an event class to decouple the event source(s) from the objects that actually handle the events." | [http://209.85.165.104/search?q=cache:Mgu-Um9Lx1sJ:www.soc.napier.ac.uk/module.php3%3Fop%3Dgetresource%26cloaking%3Dno%26resourceid%3D5938601+controller+pattern+GRASP&hl=en&ct=clnk&cd=15&gl=us Reference 6] contains the following solution to the problem of who handles the system event: "If a program receives events from external sources other than its graphical interface, add an event class to decouple the event source(s) from the objects that actually handle the events." | ||
Line 111: | Line 151: | ||
*The artificial handler of all system events in the use case [BuyItemsHandler] | *The artificial handler of all system events in the use case [BuyItemsHandler] | ||
[[Image: | [[Image:controller2.jpg]] | ||
The choice made should reflect high cohesion and low coupling, as shown below, in which the UI layer is decoupled from the problem domain. | The choice made should reflect high cohesion and low coupling, as shown below, in which the UI layer is decoupled from the problem domain. | ||
[[Image: | [[Image:controller.jpg]] | ||
= | ==Example 5== | ||
This [http://codebetter.com/blogs/david.hayden/archive/2005/04/08/61648.aspx Blog] mentions some examples of the Controller Pattern in the context of blogs. It demonstrates how the controller class beyond the UI layer "hands off" the message to another domain layer object to act upon the message. | |||
Another related [http://davidhayden.com/blog/dave/archive/2004/11/28/648.aspx article] disambiguates between the Controller Pattern and the [http://en.wikipedia.org/wiki/Model-view-controller Model-View-Controller] Architecture. The author defines the controller as "the first object beyond the UI layer that is responsible for receiving or handling a system operation message". As entailed above, the controller is the coordinator responsible for delegating work to other objects that actually accomplish the task at hand. | |||
=External Links= | |||
1. [http://www.amazon.com/Applying-UML-Patterns-Introduction-Object-Oriented/dp/0130925691 Applying UML and Patterns: Craig Larman] | |||
2. [http://people.msoe.edu/~blessing/cs489/cs489-13ch18.pdf Grasp Design Patterns] | |||
3. [http://www.galileo.cs.uwindsor.ca:8000/60-322/LectureSlides/GRASPPatterns.ppt GRASP Patterns] | |||
4. [http://staff.cs.utu.fi/kurssit/Programming-III/GRASP(9).pdf GRASP Controller] | |||
5. [http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/pdf/CS4233%20Class%204.pdf Controller Pattern] | |||
=References= | =References= | ||
Line 139: | Line 193: | ||
8. [http://www.dcs.bbk.ac.uk/~niki/lecture5.ppt Presentation on Design Patterns] | 8. [http://www.dcs.bbk.ac.uk/~niki/lecture5.ppt Presentation on Design Patterns] | ||
9. [http://www.academic.marist.edu/~jzbv/SoftwareDevelopment/GRASP.ppt Monopoly Example] |
Latest revision as of 02:52, 20 November 2007
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.
Design Patterns
A Design Pattern refers to a named description of a problem and a solution that can be applied in different contexts. Patterns are proven solutions to common problems.
A pattern addresses a recurring design problem that arises in specific design situations and presents a solution to it. This is done by defining a set of rules which describes how to solve the problem at hand.
GRASP Patterns
GRASP refers to General Responsibility Assignment Software Patterns. It includes a systematic approach to Object Oriented Design, while assigning responsibilities to classes.
GRASP Patterns are more of responsibility driven design.
Responsibilities of an Object include two types : Knowing and Doing
Doing responsibilities of an object include:
- Doing something itself, such as creating an object or doing a calculation
- Initiating action in other objects
- Controlling and coordinating activities in other objects
Knowing responsibilities of an object include:
- Knowing about private encapsulated data
- Knowing about related objects
- Knowing about things it can derive or calculate
The GRASP Patterns include:
- Information Expert
- Creator
- Controller
- Low Coupling
- High Cohesion
- Polymorphism
- Pure Fabrication
- Indirection
- Protected Variations
Some of these, such as "Low Coupling" and "High Cohesion" are merely good design principles, and not design patterns.
The Controller Pattern
Introduction
The Controller pattern addresses the question: Who handles a system event? It assigns responsibilities for handling system events to different objects.
Sometimes, an event is initiated from an external source. A class receives the event, but it may not handle the event. These events should be handled by classes representing one of the following choices:
- A class that represents the overall system, device, or subsystem (facade controller).
- A class that represents a use case scenario within which the system event occurs. These are often named <usecasename>Handler, <usecasename>Controller, <usecasename>Manager,and so forth.
To choose between facade and use-case controllers:
1. The Facade controller is suitable when there are few system events.
2. The Use-case controller is applicable when there are many system events and the facade controller would have become too large and incohesive.
Wikipedia succinctly states: "The Controller pattern assigns the responsibility of dealing with system events to a non-UI class that represent the overall system or a use case scenario. A use case controller should be used to deal with all system events of a use case, and may be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate use case controllers)"
Benefits
- Reuse of domain layer software by plugging in various interfaces
- It is possible to control out-of-sequence system events
Controller vs MVC
This page disambiguates the MVC part as well: Controller Pattern and MVC
Examples
Example 1
Reference 1 contains an excellent description of the controller pattern.
The controller is "A non-user interface object responsible for receiving or handling a system event, and that defines the method for the system operation".
- A system event is said to be an event generated by an "external actor"
- A system operation refers to the response of the system to a system event.
The figure below shows allocation of different operations
Example 2
The game of Monopoly is an excellent example of a controller pattern. Actors, such as the human player in Monopoly, generate UI events (such as clicking a button with a mouse to play a game or make a move). The UI software objects (such as a JFrame window and a JButton) must process the event and cause the game to play. When objects in the UI layer pick up an event, they must delegate the request to an object in the domain layer.
The problem in this case can be formulated as follows: What first object beyond the UI layer should receive the message from the UI layer?
As per the controller pattern the solution is as follows: Assign the responsibility to an object representing either of following: - Represents the overall “system” – a root object - Represents a use case scenario within which the system operation occurs.
Example 3
This article contains an E-Commerce example of the Controller Pattern. The author classifies controllers as Use case Controllers and Facade Controllers.
Use case controllers are useful to handle messages from the User Interface Layer when there is little coupling between messages. A facade controller to handle all messages will not suffice in this case.
The E-Commerce application in the example considers the following messages:
Get Categories Get Product in Category X Get Items in Shopping Cart Add Item to Shopping Cart
The first 2 messages are related to a ProductCatalog to ProductCatalogHandler and the last 2 messages are related to the shopper's cart to ShoppingCartHandler:
ProductCatalogHandler -Get Categories -Get Product in Category X ShoppingCartHandler -Get Items in Shopping Cart -Add Item to Shopping Cart
ProductCatalogHandler and ShoppingCartHandler are highly cohesive, embodied by the Controller Pattern.
Example 4
Reference 6 contains the following solution to the problem of who handles the system event: "If a program receives events from external sources other than its graphical interface, add an event class to decouple the event source(s) from the objects that actually handle the events."
The author also mentions the benefits of using the Controller Pattern as an increased potential for reuse. It leads to decoupling between the external sources of events and the internal event handlers.
Consider the example of buying items at a supermarket. The cashier enters the items to be purchased and generates a bill for the customer to make the payment at the end of the sale. The system events include:
enterItem() endSale() makePayment()
The problem can be phrased as: Who is responsible for enterItem()?
Using the Controller Pattern to solve our problem, we can choose among the following:
- The overall system [POST]
- The overall business(sub-system) [STORE]
- Someone in the real world who is active in the task [CASHIER]
- The artificial handler of all system events in the use case [BuyItemsHandler]
The choice made should reflect high cohesion and low coupling, as shown below, in which the UI layer is decoupled from the problem domain.
Example 5
This Blog mentions some examples of the Controller Pattern in the context of blogs. It demonstrates how the controller class beyond the UI layer "hands off" the message to another domain layer object to act upon the message.
Another related article disambiguates between the Controller Pattern and the Model-View-Controller Architecture. The author defines the controller as "the first object beyond the UI layer that is responsible for receiving or handling a system operation message". As entailed above, the controller is the coordinator responsible for delegating work to other objects that actually accomplish the task at hand.
External Links
1. Applying UML and Patterns: Craig Larman
References
3. Blog on GRASP Controller Pattern
7. DESIGN MODEL: USE-CASE REALIZATIONS WITH GRASP PATTERNS