Full Transcript

SWE202 SOFTWARE CONSTRUCTION WEEK 12 - 13 Grasp Patterns and their application Oluwaseun Priscilla Olawale (MSc.) Objectives Define patterns. Learn to apply four of the GRASP patterns. The most likely way for the world to be destroyed, most experts agree, is...

SWE202 SOFTWARE CONSTRUCTION WEEK 12 - 13 Grasp Patterns and their application Oluwaseun Priscilla Olawale (MSc.) Objectives Define patterns. Learn to apply four of the GRASP patterns. The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents. - Nathaniel Borenstein GRASP GRASP (General Responsibility Assignment Software Patterns) is an acronym created by Craig Larman to encompass nine object‐oriented design principles related to creating responsibilities for classes. These principles can also be viewed as design patterns. GRASP is an attempt to document what expert designers probably know intuitively. Four GRASP patterns will be presented and briefly discussed GRASP as a Methodical Approach to Learning Basic Object Design GRASP patterns are a learning aid to help talk about and methodically do fundamental object design. In other words, they help one understand essential object design, and apply design reasoning in a methodical, rational, explainable way. The assignment of responsibilities and design of collaborations are very important and creative steps during design, either while diagraming or while programming. This approach to understanding and using design principles is based on patterns of assigning responsibilities. Responsibilities and Methods The UML defines a responsibility as "a contract or obligation of a classifier“. Responsibilities are related to the obligations of an object in terms of its behavior. Basically, these responsibilities are of the following two types: ○ knowing ○ doing Responsibilities– 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 Responsibilities– Knowing Knowing responsibilities of an object include: ○ knowing about private encapsulated data ○ knowing about related objects ○ knowing about things it can derive or calculate GRASP Patterns Experienced object-oriented developers (and other software developers) build up a repertoire of both general principles and idiomatic solutions that guide them in the creation of software. These principles and idioms, if codified in a structured format describing the problem and solution, and given a name, may be called patterns. Most simply, a pattern is a named problem/solution pair that can be applied in new context, with advice on how to apply it in novel situations and discussion of its trade- offs. GRASP Patterns - Example Pattern Name: Information Expert Solution: Assign a responsibility to the class that has the information needed to fulfill it. Problem It Solves: What is a basic principle by which to assign responsibilities to objects? GRASP Patterns There are nine GRASP patterns: ○ Creator ○ Information Expert (or just Expert) ○ Low Coupling ○ Controller ○ High Cohesion ○ Polymorphism ○ Pure Fabrication ○ Indirection ○ Protected Variations GRASP Patterns- Creator Assign the responsibility of creating instances of a class to the class that has the most knowledge about when and how to create them. Pattern name: Creator Problem: Decide which class should be responsible for creating instances of a new bank account. Solution: Assign the responsibility to a Bank class or AccountManager class, as they have the necessary knowledge and access to create new instances of BankAccount. GRASP Patterns - Information Expert (or just Expert) Assign a responsibility to the class that has the necessary information to fulfill it, promoting high cohesion and minimizing coupling. Pattern name: Information Expert Problem: Determine which class should be responsible for calculating interest on a bank account. Solution: Assign the responsibility to the BankAccount class since it holds all the necessary information about the account balance and interest rates. GRASP Patterns - Low Coupling Aims for classes to have minimal dependencies on each other, facilitating easier maintenance and flexibility in the system Pattern name: Low coupling Problem: Minimize dependencies between classes to allow for easier maintenance and modification. Solution: Implement an Observer pattern to notify interested parties (e.g., GUI, Logging module) of changes in bank account balances without tightly coupling them to the BankAccount class. GRASP Patterns - Controller Assign the responsibility of handling system events or coordinating activities to a controller class. Pattern name: Controller Problem: Determine which class should handle the process of transferring funds between bank accounts. Solution: Assign the responsibility to a TransactionController class, which coordinates the transfer process by interacting with the BankAccount objects involved. Assignment – Explain the following Grasp principles with the problem statement you previously generated. High Cohesion Polymorphism Pure Fabrication Indirection Protected Variations

Use Quizgecko on...
Browser
Browser