Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Introduction to GRASP principles

From: Foundations of Programming: Object-Oriented Design

Video: Introduction to GRASP principles

Now let's explore one more set of object-oriented design principles. This is GRASP, short for General Responsibility Assignment Software Patterns. And as you will see, the principles here take a slightly different perspective than the five principles found in SOLID, although there is certainly some crossover. GRASP tends to take a responsibility focus as in who creates this object, who is in charge of how these objects talk to each other, who takes care of passing all messages received from a user interface? Now SOLID and GRASP don't conflict with each other, they are not competing sets, you might choose to use one or both or neither.

Introduction to GRASP principles

Now let's explore one more set of object-oriented design principles. This is GRASP, short for General Responsibility Assignment Software Patterns. And as you will see, the principles here take a slightly different perspective than the five principles found in SOLID, although there is certainly some crossover. GRASP tends to take a responsibility focus as in who creates this object, who is in charge of how these objects talk to each other, who takes care of passing all messages received from a user interface? Now SOLID and GRASP don't conflict with each other, they are not competing sets, you might choose to use one or both or neither.

And there are nine ideas in GRASP, but again, it's not about memorizing these. I'm just trying to provide an introduction to the fact that they exist. This is not dogma, these are not rules, but you may find GRASP a useful learning tool to keep in mind while drawing up UML diagrams to provide some techniques for figuring out the responsibilities of your objects. First up, I'm going to talk about the principle called Expert or Information Expert. This is one of the simplest, most basic ideas--refers to the idea that a class should be responsible for itself, that if there is a responsibility--and we are asking where does this go--we give it to the class that has the most information needed to fulfill it, whether that's storing a piece of data or performing some service.

Let's say we are working with three objects, Customer, Shopping Cart, Item. And we realize the Customer wants to know the current total of all items in the cart. Now there is nothing that would stop us from calculating that data and storing it inside Customer object, but if you think about it, it's the Shopping Cart that knows the most about all the items inside it, it's the Shopping Cart that should be responsible for that, an object who takes care of itself. That's the idea of the Information Expert or the Expert principle.

Next, we have the idea of CREATOR. Who is responsible for creating an object? We can often look at our Class diagram and ask this question about a lot of your classes. It's often easy to figure out that there are interactions between objects, but not so easy to determine how those objects are created in the first place, how they come into being. Sometimes drawing up a Sequence diagram can help, but if we are not sure, the CREATOR role asks a few questions, such as does one object contain another, the idea of composition, does one object very closely use another or will one object know enough to make another object? And if so, it would seem to make sense to nominate those objects as taking that CREATOR role and making it apparent which objects are responsible for creating other objects.

Next, we have the idea all LOW COUPLING and HIGH COHESION. Now LOW COUPLING is something that's a desirable, and you'll hear this phrase from developers who have never worked with GRASP or Principles or SOLID principles, it's a common phrase in development, and it simply means to reduce the amount of required connections between objects. If one object needs to connect tightly to five other objects and call 20 different methods just to work, you have HIGH COUPLING. Lots of dependencies meaning lots of potential for breaking things if you make a change to any of these objects.

The aim is for LOW COUPLING, reducing dependencies. Now LOW COUPLING does not mean no coupling. Objects do need to know about each other, but as much as possible they should do what they can with the minimum of other objects--as many as necessary but as few as possible. You will often hear about Coupling and Cohesion being talked about in the same sentence. Cohesion is a measure of how focused the internal behavior of a class is. Are all its behaviors related to that single responsibility? If so, it has HIGH COHESION.

If for example, it's a God Object with multiple behaviors, all of which have nothing to do with each other, it has LOW COHESION. What we want is LOW COUPLING, and we want HIGH COHESION, and you will find that these two ideas are actually related to several other principles as we go through these. For example, the next idea, something called CONTROLLER. If we have a user interface and also some business- related classes, we don't want to have HIGH COUPLING between them to actually tie them directly together, where the User Interface elements have to know about the Business Objects and the Business Objects have to know about the User Interface elements.

So it's very common to create a new class called a CONTROLLER class just for the purpose of going between them. So we don't expect the Business Object to update the actual screen, nor do we expect the screen--the buttons, and windows--to actually talk directly to the Business object. The CONTROLLER stands between them. And there is a common programming pattern called Model View Controller which is an example of using this. And it does lead to the idea that it's perfectly normal for object to exist that takes a role in a program that isn't a real world Business Object or concept, it's still just a well- described position, a well-described idea inside the application itself.

This is just a specific example of that, and it leads to the next idea, that of PURE FABRICATION. What if there's something that needs to exist in the application that doesn't announce itself as an obvious class or real-world object? What if you have behavior that doesn't naturally fit in existing classes? Well, rather than force that behavior into an existing class where it doesn't belong--which means we are decreasing Cohesion-- we instead invent, we fabricate a new class.

That class might not have existed in our conceptual model, but it needs to exist now. And there's nothing wrong with creating a class that represents pure functionality as long as you know why you're doing it. PURE FABRICATION leads us to the next idea of Indirection. This is the idea that we can decrease coupling between objects. If you have multiple objects that need to talk to each other, it's very easy to have HIGH COUPLING between them where there is a lot of dependencies. And what we can do instead is reduce those direct connections and put an indirection object in between them to simplify the amount of connections that each object has to make.

The next principle we've been exploring several times, which is POLYMORPHISM-- and I say automatically here because the opposite of this would be having to write code that checks the existence of an object and checks to see what particular type it is. We don't want to do that, we want to reduce that kind of checking as much as possible and just have it work polymorphically and automatically, which of course means we are thinking about Inheritance, we are thinking class design. And finally, we have Protected Variations. This is the widest, most overreaching idea.

How to design a system so that changes and variations have the minimum impact on what already exists. Here, as a principle it means we try to identify the most likely points of change so we can make doubly sure that we create a framework around our application that protects the most sensitive pieces. And here's the thing, most of the concepts we have been exploring all along are simply ways of doing this, things like encapsulation and data-hiding, making your attributes private. Well, that's just one way of protecting your classes from change.

Interfaces are another area where we can support formality, even add more later, but we are not enforcing specific behavior. The Liskov substitution principle, child classes should always work when treated as their parent classes is another. The open/closed principle in SOLID that we can add, but we try not to change code that works already is yet another. And even though they may be wrapped up in complex sounding terminology, this is what object-oriented design principles and patterns and practices are all designed to support, to allow us to figure out more approaches to use when we run into sticking points, and to write readable, maintainable, flexible code.

Show transcript

This video is part of

Image for Foundations of Programming: Object-Oriented Design
Foundations of Programming: Object-Oriented Design

47 video lessons · 49280 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 11m 35s
    1. Welcome
      1m 25s
    2. Who this course is for
      1m 15s
    3. What to expect from this course
      3m 6s
    4. Exploring object-oriented analysis, design, and development
      1m 41s
    5. Reviewing software development methodologies
      4m 8s
  2. 26m 14s
    1. Why we use object-orientation
      2m 42s
    2. What is an object?
      5m 22s
    3. What is a class?
      4m 43s
    4. What is abstraction?
      2m 45s
    5. What is encapsulation?
      3m 45s
    6. What is inheritance?
      3m 35s
    7. What is polymorphism?
      3m 22s
  3. 12m 16s
    1. Understanding the object-oriented analysis and design processes
      4m 13s
    2. Defining requirements
      6m 9s
    3. Introduction to the Unified Modeling Language (UML)
      1m 54s
  4. 23m 35s
    1. Understanding use cases
      6m 11s
    2. Identifying the actors
      4m 16s
    3. Identifying the scenarios
      5m 7s
    4. Diagramming use cases
      4m 18s
    5. Employing user stories
      3m 43s
  5. 16m 36s
    1. Creating a conceptual model
      1m 59s
    2. Identifying the classes
      2m 27s
    3. Identifying class relationships
      2m 38s
    4. Identifying class responsibilities
      6m 43s
    5. Using CRC cards
      2m 49s
  6. 22m 25s
    1. Creating class diagrams
      6m 11s
    2. Converting class diagrams to code
      4m 57s
    3. Exploring object lifetime
      5m 55s
    4. Using static or shared members
      5m 22s
  7. 19m 49s
    1. Identifying inheritance situations
      6m 49s
    2. Using inheritance
      2m 43s
    3. Using abstract classes
      2m 2s
    4. Using interfaces
      4m 20s
    5. Using aggregation and composition
      3m 55s
  8. 9m 23s
    1. Creating sequence diagrams
      5m 18s
    2. Working with advanced UML diagrams
      2m 3s
    3. Using UML tools
      2m 2s
  9. 10m 39s
    1. Introduction to design patterns
      2m 40s
    2. Example: the singleton pattern
      4m 53s
    3. Example: the memento pattern
      3m 6s
  10. 21m 47s
    1. Introduction to object-oriented design principles
      2m 50s
    2. Exploring general development principles
      3m 55s
    3. Introduction to SOLID principles
      6m 43s
    4. Introduction to GRASP principles
      8m 19s
  11. 7m 1s
    1. Reviewing feature support across different object-oriented languages
      3m 50s
    2. Additional resources
      2m 27s
    3. Goodbye
      44s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Foundations of Programming: Object-Oriented Design.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.