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

Introduction to SOLID principles

From: Foundations of Programming: Object-Oriented Design

Video: Introduction to SOLID principles

I'm going to go through the five principles of Object-Oriented Design that are grouped under the acronym SOLID. These were put together by the author Robert Martin, also known as Uncle Bob. First, a word of warning: the names of these principles can sound excessively complex, the meanings behind them are not quite so bad. S is for the Single Responsibility Principle; O, the Open/Closed Principle; L, the Liskov Substitution Principle; I is the Interface Segregation Principle; and D is the Dependency Inversion Principle.

Introduction to SOLID principles

I'm going to go through the five principles of Object-Oriented Design that are grouped under the acronym SOLID. These were put together by the author Robert Martin, also known as Uncle Bob. First, a word of warning: the names of these principles can sound excessively complex, the meanings behind them are not quite so bad. S is for the Single Responsibility Principle; O, the Open/Closed Principle; L, the Liskov Substitution Principle; I is the Interface Segregation Principle; and D is the Dependency Inversion Principle.

Now, before you run screaming from the room, I will say that the concepts behind most of these are much easier to recall than the names, and it's not about remembering the names of these principles, that's really not important. So let's take them one by one. First, the Single Responsibility Principle. An object should have one primary responsibility, one reason to exist, and that reason entirely encapsulated within one class. It can have multiple behaviors, but all those behaviors are oriented around that core reason for existence, and everything it does should support that.

So one of the impacts here is we don't create God objects that take the place of several real world business entities, and neither do we split one focus concept's behavior across multiple objects. An object should always be responsible for itself. Next, we have the O, the Open/Closed Principle-- open for extension but closed to modification. And we're talking about software entities here, classes, modules, functions. What this means is that after you've written some working code, and then your requirements change, if you need to add additional behavior, you do it by writing new code, not by changing all code that already works.

One example is with Inheritance. Say we have some working code, and then we get a new business requirement, we can support it by adding a new class, and if that class needs some additional behavior we don't change the original superclass, we provide some new code. So we're not changing the existing code that already works, our system is open for extension, but closed for modification. L is for the Liskov Substitution Principle. What this means is objects in a program should be replaceable with instances of their subtypes--their subclasses or derived classes--without altering the correctness of the program.

It's an extension of that whole inheritance idea. Meaning that if we've created a whole bunch of derived classes or child classes or subclasses--whatever term you prefer to use for them--I should always be able to pass any of these around and treat them like their superclass, their parent class. I should never have the situation where we say, oh yes, we can do any of them except that one, that one has to be treated specially. If that's the case, it sounds like it's not a true subclass, and it's breaking this principle.

I is for the Interface Segregation Principle. The idea that many specific interfaces are better than one general purpose interface. Now, we're not talking here about user interfaces, but the Java style interfaces we talked about earlier in the course, also known as Protocols. The idea of having formalized lists of method names that a class can choose to support and implement. But the principle here is that interfaces, those lists of methods should be as small as possible.

And if they start to get bigger, they should be split up into smaller interfaces. Because classes can choose to implement multiple smaller interfaces, no class should be forced to support huge lists of methods that it doesn't need. And finally, the D, the Dependency Inversion Principle, depend on abstractions, not on concretions. Now, that sounds very vague, and this is often the toughest one of the five to grasp. What it describes is the value of not directly tying concrete objects together but making them deal with abstractions in order to minimize dependencies between our objects. Here is an example.

Let's say we have a store object, and our store allows people to buy digital audio files to download. So when we do, the store object is going to read the file using a very specialized class called AudioFileReader, and then writing it out using another very specialized class called AudioFileWriter. So the Store object, which we can consider a high-level object is very dependent on these two low-level objects that deal with the specifics of reading and writing this audio file. Now, the Dependency Inversion Principle means that we would like to disconnect these two very concrete classes.

So what I would do is remove those connections, and I'd insert a new layer here, which might be an abstract class that represents a Reader and Writer rather than something very specific like an AudioFileReader and an AudioFileWriter. So now the Store class is not dependent on the concrete AudioFileWriter and AudioFileReader, but on abstractions. We have this new layer of abstraction here. And the benefit of that is that we can now have flexibility to say these low-level classes could be any class that inherits from those abstract classes of Reader and Writer.

Perhaps MovieFileReader and MovieFileWriter, or later on, GameFileReader and GameFileWriter, or whatever else may come along. So it allows substitutions, flexibility going forward, being able to replace and extend without--in this case-- changing the Store object at all. However, do be aware that if you've got too many layers to try and future-proof your code, it can be argued that you're violating you ain't gonna need it rule, so something to bear in mind.

Now, as you can tell, these are principles that I might be able to summarize in a few minutes, but they could take years to appreciate how they would be applied in a variety of different situations. And you won't always be able to follow them, nor will you always want to, and nor are you intended to. These aren't rules, they aren't dogma, they are useful guidelines. SOLID can be viewed as a checklist, like using FURPS at the early stages of functional requirements, not necessarily something you'd expect to memorize, but certainly something worth revisiting now and again to prompt you in creating a concise and a well-made class design.

Show transcript

This video is part of

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

47 video lessons · 54123 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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.