Week 13 Activity PDF
Week 13 Activity PDF
Tutorial Activity: 13
They will help you to create a clean and modular design, which would
be easy to test, debug, and maintain in the future.
I have not put examples, just to keep the article short but you can find
a lot of examples of these design principles on the internet and even
on my Java blog, just use the search bar at the top of the page.
If you are not able to understand a design principle, you should try to
do more than one example because sometimes we connect to another
Btw, I have also shared relevant and useful courses and books, both
free and paid, and I will earn some money if you buy something which
is not free. They are the resources I have used to learn SOLID design
principles and Programming in general and quite useful for learning
these principles in depth.
If you have a block of code in more than two places consider making it
a separate method, or if you use a hard-coded value more than one
time make them public final constant. The benefit of this Object-
oriented design principle is in maintenance.
It's important not to abuse it, duplication is not for code, but for
functionality.
It means if you have used common code to validate OrderId and SSN
it doesn't mean they are the same or they will remain the same in the
future.
You can also read the classical book Pragmatic Programmer by Dave
Thomas and Andrew Hunt where it first coined this term and explains
what exactly DRY means
There is only one thing that is constant in the software field and that is
"Change", So, encapsulate the code you expect or suspect to be
changed in the future.
The benefit of this OOP Design principle is that It's easy to test and
maintain proper encapsulated code.
If you are coding in Java then follow the principle of making variables
and methods private by default and increasing access step by step like
from a private to protected and not public.
The key benefit of this design principle is that already tried and tested
code is not touched which means they won't break.
Here is a Java code example which violates the Open Closed Design
Principle of Programming:
Ideally, if you are adding new functionality only then your code should
be tested and that's the goal of Open Closed Design principle.
By the way, the Open-Closed principle is "O" from the SOLID acronym.
If you want to learn more about this principle, the SOLID Principles of
Object-Oriented Design and Architecture course on Udemy is one
of the best resources to consult.
For example, If you put more than one functionality in one Class in
Java it introduces coupling between two functionality and even if you
change one functionality there is a chance you broke coupled
functionality, which requires another round of testing to avoid any
surprise on the production environment.
The beauty of this design principle is that any class that is injected by
DI framework is easy to test with the mock object and easier to
maintain because object creation code is centralized in the framework
and client code is not littered with that.
You can see that AppManager depends upon EventLogWriter which is tightly
coupled with the AppManager. If you need to use another way to notify
Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 7
20CS43P : Object Oriented Programming and Design with Java
your client like by sending push notifications, SMS, or E-mail, you need
to change the AppManager class.
You can further see Using SOLID Principles to Write Better Code --- A
Crash Course on Udemy to learn more about the Dependency
Inversion Principle and how to solve such problems.
In OOP, There are two general ways to reuse the code you have
already written, Inheritance and Composition, both have their own
advantage and disadvantages, but, in general, you should always favor
composition over inheritance, if possible.
Some of you may argue this, but I found that Composition is the lot
more flexible than Inheritance.
And, if you keep forgetting this rule, here is a nice cartoon to put on
your desk :-)
It's free to explore and learn but you will be charged if you also want
to participate in exercises, assignments, evaluations, and need
Certification to show in your LinkedIn profile.
If you are joining this course to get Coursera certificate then you need
to either enroll into the specialization or take a subscription plan
like Coursera Plus which provides unlimited access to more than
5000+ Coursera courses, projects, and professional certificates.
This is not a design principle but rather than a best practice while
coding in object oriented programming.
If a class has more functionality then subclass might not support some
of the functionality and does violate LSP.
The reason the LSP is violated is that the behavior of Square does not
match that of Rectangle, like, calling setWidth changes the height as
well, which is not the case for Rectangle.
This happens mostly when one interface contains more than one
functionality, and the client only needs one functionality and no other.
Another benefit of this design principle in Java is, the interface has the
disadvantage of implementing all methods before any class can use it
so having single functionality means less method to implement.
If you don't get the benefit of the interface in coding then I suggest
you read my blog post, the real usage of an interface in Java to learn
more.
Kashinath Bedare, Lecturer in Govt. Women Polytechnic, Shiralakoppa. Page 10
20CS43P : Object Oriented Programming and Design with Java
A programmer should always program for the interface and not for
implementation this will lead to flexible code which can work with any
new implementation of the interface.
I mean
List numbers= getNumbers();
instead of
ArrayList numbers = getNumbers();
This has also been advised in many Java books including in Effective
Java and Head First design pattern book.
In order to compare two objects for equality, we ask the class itself to
do a comparison instead of the Client class doing that check.
Summary
The theory is the first step, but what is most important is to develop
the ability to find out when to apply these design principles.
Once you get hold of that, the next step is to learn Design patterns in
Java, which uses these design patterns to solve common problems of
application development and software engineering.