I'm new to SOLID principle but I understand it. My main problem is having a hard time designing my classes to follow the SOLID specially the Dependency Inversion. Sometimes it's easy to write the whole logic into procedural pattern rather than to use SOLID.
For example:
Let's say that we are creating an Attendance Monitoring System, and we have logic(or procedure) that scan the employee fingerprint, get it's ID, determine whether or not it's valid or not, determine what time he was in, write the login info to the database, and show if it's successful or not.
It is easy to write this in a procedural manner with a bunch of 'if else', loop and switch. But in future I'm gonna suffer the 'code debt'.
If we applying SOLID principle here. I know that we need to have a some kind of object like 'AttendanceServiceClass' that has a method like 'scanEmployeeID()', 'processthislogin()' or 'isItsucessful()'. And I know that this class has a dependency to a repository, userinfo, and other objects.
Basically my problem is analyzing about the design of the class and its dependencies
What is the step by step way of analyzing the design of your class?
sorry for my English.
I cannot agree more, it is easier for us programmer to handle code in procedural pattern. This makes OOP hard for programmer who accustomed to procedural programming.
However I found it easier to write the general interface and consumer first rather than breaking the interface designed to smaller modules. This is, a kind of
Test First Development -> Red, green, refactor
practice. (please note that, if you want to achieve theneat design
, consider following TDD instead this guide. This guide is just a small section of doing TDD)Say that we want to create the
ServiceAttendance
to doscanEmployeeID
. We will has interface like (please notice the example is in C# naming):Please noted that I decide the method to return bool instead of void to determine success/failure operation. Please notice the consumer example below does not implement any DI because I just want to show how to consume it. Then in the consumer, we can have:
This concludes the consumer. Now we move to implementation. Say that you can develop it using procedural programming and got the monolithic code block. You can state the implementation with pseu-like statement.
Now we have 4 steps to be done in this one operation. My principal is, not to do over 3 facade process in one method so I can simply refactor the 3 and 4 to one process. Now we have
This, we have 3 main operation. We can analyze whether we need to create a smaller module or not by breaking down the operation. Say we want to break the second operation. We can get:
The breakdown operation itself is obvious enough to break the second module into another smaller module. For
2.2
and2.3
, we need a smaller module to be injected. Simply because it will need dependency to repository, thus need to be injected. The same case apply for operation step1 scan the employee id
, because it will need dependency to fingerprint scanner, so the scanner handler must be implemented in separated module.We can always breakdown the operation, as we can do it in
2.1
:Now I am unsure if
2.1.1
and2.1.2
need to be broken down into 2 separated modules, it is up to you to decide. And now we got the interfaces, then we can start the implementation. Expect to throwexceptions
during validation or you will need to pass custom class to handle error messages.First of all, solid is not ONE principle, it stands for 5 different principles:
A
andB
. Inheritance is suitable whenever all objects of a derived classB
can be replaced by objects of their parent classA
without any loss of funcionality;These principles are guides, but it does not mean you have to use them strictly every time.
From your description, I can see your main difficulty is to think OO. You are still thinking about how to do things and this is a procedural mindset. But in OOP it is more important decide who will do these things.
Thinking about DI, using your example, let's see your scenario:
What is the problem here?
Well, first of all, this code violates SRP: What if the authentication process changes? If the company decided that name tags are insecure and install a biometrical recognition system? Well, there's here a reason for your class to change, but this class does not do just authentication, it does other things, so, there will be another reasons for it to change. SRP states that your classes should have just ONE reason to change.
It also violates OCP: What if there's another authentication method avaiable and I want to be able to used as I wish? I can't. To change the auth method, I have to modify the class.
It violates ISP: Why a
ServiceAttendance
object has a method for employee authentication if it should just provide service attendance?Let's improve it a little bit:
Now that's a little bit better. We solved the problems with SRP and ISP, but if you think better, it still violates OCP and now violates DIP. The problem is that
AttendanceService
is tightly coupled withBarCodeAuth
. I still can't change the auth method without touchingAttendanceService
.Now let's apply OCP and DIP together:
Now I can do:
To change the behaviour, I don't need to touch the class. If some other auth method appears, I just need to implement it, respecting the interface and it is ready to use (remember OCP?). This is due to me being using DIP on
ServiceAttendance
. Although it needs an authentication method, it is not its responsability to create one. In deed, for this object, it does not matter the method of authentication, it just need to know if the caller (user) is or is not authorized to do what he's trying to do.This is all about DIP is: your components should depend on abstractions, not implementations.
Not specifically about SOLID, but worth mentioning as a very interesting OOP-training approach by Jeff Bay: Object Oriented Calisthenics. The idea is that you can try to follow set of very strict rules on a non-real-life, small project.
First off, think of different parts of the attendance system. User interface, finger print scanner, database repository, login process and workflow. To design this system we can start designing parts in isolation and connect them as a system.
A rough design could be around following parts of the system:
In the following code listing certain aspects of design principles will be visible already:
Based on this much thought, the system might work like this:
[You can further improve on it and add missing logic, I am providing a very quick design outline with brief implementation.]
Code Listing
Certainly, procedural programming is much easier for people who are used to writing code procedurally. For those used to writing well factored object oriented code, procedural code is actually harder.
Yes, well factored object oriented code often results in more work, and more actual code. But if done correctly, it makes the code easier to maintain, easier to extend, easier to debug (and more importantly easier to test).