Introduction
My wife Farhana wants to resume her career as a software developer (she started her career as a software developer,but couldn't proceed much because of our first child's birth),and these days,I am trying to help her learn Object Oriented Designs as I've got some experience in software design and development.
Since my early days in software development,I have always observed that no matter how hard a technical issue seems,it always becomes easier if explained from a real life perspective and discussed in a conversational manner. As we had some fruitful conversations on Object Oriented Designs,I thought I could share it because someone might find it an interesting way of learning OOD.
Following is how our OOD conversation took place:
Topic: Introducing OOD
Shubho: Darling,let's start learning Object Oriented Designs. You know Object Oriented Principles,right?
Farhana: You mean,Encapsulation,Inheritance,and Polymorphism,right? Yes,I know the principles.
Shubho: OK,I hope you already know how to use classes and objects. Let us learn Object Oriented Designs today.
Farhana: Hold on a second. Isn't Object Oriented Principles enough to do Object Oriented programming? I mean,I can define classes and encapsulate properties and methods. I can also define a class hierarchy based upon their relationship. So,what's left?
Shubho: Good question. Object Oriented Principles and Object Oriented Design are actually two different things. Let me give you a real life example to help you understand.
When you were child you learned alphabets first,17)"> Farhana: Yes.
Shubho: OK. You also learned words and how to assemble the alphabets to make those meaningful words. Along with that,you learned some basic grammar to assemble the words into sentences. For example,you had to maintain tenses,and you had to use prepositions,conjunctions,and others properly to create grammatically correct sentences. Say,a sentence like the following:
"I" (pronoun) "want" (Verb) "to" (Preposition) "learn" (Verb) "OOD" (Noun)
You see,you are assembling the words in some order,and you are also choosing the correct words to end up with a sentence that has some meaning.
Farhana: OK,so,what does this mean?
Shubho: This is analogous to Object Oriented Principles. OOP says about the basic principles and the core ideas to do Object Oriented programming. Here,OOP can be compared to the basic English grammar,where the basic grammar teaches you how to construct a meaningful and correct sentence using words,and OOP teaches you to construct classes,encapsulate properties and methods inside them,and also develop a hierarchy between them and use them in your code.
Farhana: Hm..I got the point. So,where does OOD fit here?
Shubho: You'll get your answer soon. Now,suppose you need to write articles and essays on some topics. You may also wish to write books on different subjects that you have expertise on. Knowing how to construct sentences is not enough to write good essays/articles or books,right? You need to write a lot,and need to learn to explain things in a good way so that readers can easily understand what you are trying to say.
Farhana: Seems interesting... carry on.
Shubho: Now,if you want to write a book on a particular subject (say,Learning Object Oriented Design),you got to know how to divide the subject into smaller topics. You also need to write chapters on those topics,and you need to write prefaces,introductions,explanations,examples,and many other paragraphs in the chapters. You need to design the overall book and learn some best practices of writing techniques so that the reader can easily understand what you are trying to explain. This is about the bigger picture.
In software development,OOD addresses the bigger picture. You need to design your software in a way that your classes and code are modularized,re-usable,and flexible,and there are some good guidelines to do that so that you don't have to re-invent the wheel. There are some design principles that you can apply to design your classes and objects. Makes sense?
Farhana: Hmm.. got the initial idea,but need to learn more.
Shubho: Don't worry,you will learn soon. Just let our discussion going.
Topic: Why OOD?
Shubho: This is a very important question. Why should we care about Object Oriented Design when we can create some classes quickly and finish development and deliver? Isn't that enough?
Farhana: Yes,I didn't know about OOD earlier,and still I was able to develop and deliver projects. So,what is the big deal?
"Walking on water and developing software from a specification are easy if both are frozen."
Farhana: You mean software development specifications keep changing constantly?
Shubho: Exactly! The universal truth of software is "your software is bound to change". Why?
Because,your software solves real life business problems and real life business processes evolve and change - always.
Your software does what it is supposed to do today and does it good enough. But,is your software smart enough to support "changes"? If not,you don't have a smartly designed software.
Shubho: "A smartly designed software can adjust changes easily; it can be extended,and it is re-usable."
And,applying a good "Object Oriented Design" is the key to achieve such a smart design. Now,when can you claim that you have applied good OOD in your code?
Farhana: That's my question too.
Shubho: You are doing Object Oriented Design if your code is:
- Of course,object oriented.
- Re-usable.
- Can be changed with minimal effort.
- Can be extended without changing existing code.
Farhana: And..?
Shubho: We are not alone. Many people have already given lots of thoughts and put a lot of effort on this issue,and they've tried to achieve good Object Oriented Design and identify some basic principles for doing Object Oriented Design (some basic inspirations which you can use to lay out your object oriented design). They also have identified some common design patterns that are applicable to common scenarios (based on the basic principles).
Farhana: Can you name some?
Shubho: Sure. There are many design principles out there,but at the basic level,there are five principles which are abbreviated as the SOLID principles (thanks toUncle Bob,the great OOD mentor).
S = Single Responsibility Principle
O = Opened Closed Principle
L = Liscov Substitution Principle
I = Interface Segregation Principle
D = Dependency Inversion Principle
In the following discussion,we will try to understand each of these in detail.
Topic: Single Responsibility Principle
Shubho: Let me show you the poster first. We should thank the person who made the posters,these are really interesting.
It says,"just because you can implement all the features in a single device,you shouldn't". Why? Because it adds a lot of manageability problems for you in the long run.
Let me tell you the principle in Object Oriented terms.
"There should never be more than one reason for a class to change."
Or,differently said: "A class should have one and only one responsibility".
Farhana: Can you please explain?
Shubho: Sure,this principle says that if you have a class that has more than one reason to change (or has more than one overall responsibility),you need to split the class into multiple classes based upon their responsibility.
Farhana: Hmm... does that mean that I can't have multiple methods in a single class?
Shubho: Not at all. Rather,you surely can have multiple methods in a single class. The issue is,they have to meet a single purpose. Now,why is splitting important?
It's important because:
- Each responsibility is an axis of change.
- Code becomes coupled if classes have more than one responsibility.
Farhana: Could you please give me an example?
Class hierarchy showing violation of SRP principle