Introducing
Single Responsibilty Pattern
OOP in the real world
OOP Principles
Abstraction
●
abstraction is the art of hiding implementation
details from the user and provide the user with
what they want.
●
It can be using interfaces or abstract class
https://javatutorial.net/java-abstraction-example
https://github.com/shiddiqeuy/Java-Abstraction
Encapsulation
●
The localization of knowledge within a module.
Because objects encapsulate data and
implementation, the user of an object can view the
object as a black box that provides services. Instance
variables and methods can be added, deleted, or
changed, but as long as the services provided by the
object remain the same, code that uses the object can
continue to use it without being rewritten.
https://docs.oracle.com/javase/tutorial/information/glossary.html#encapsulation
https://javatutorial.net/java-encapsulation-example
●
Inheritance
●
Polymorphism
The Common Problems
Spagheti Code
Loose vs Thight Coupled
Tighly Coupled
●
Tight coupling is when a group of classes are
highly dependent on one another.
●
See more on
https://intesar1994.weebly.com/types-of-
coupling-in-java.html
SOLID Principle
Robert C Martin a.k.a Uncle Bob
S.O.L.I.D
●
Single responsibility
●
Open closed principle
●
Liskov subtitution
●
Interface segregation
●
Dependency inversion
Single Responsibility
●
each class (microservice, code module,
function, method) should have only one single
responsibility.
●
Responsibility is used in the sense of having
only one reason to change.
Class User{
String name;
Int age;
public boolean isAgeAllowed(){
return age > 17;
}
}
Class User{
String name;
Int age;
}
Class UserValidation{
private User user;
public UserValidation(User _user){
this.user = _user;
}
public boolean isAgeAllowed(){
return this.age > 17;
}
}
good
better
class User
{
void CreatePost(Database db, string postMessage)
{
try
{
db.Add(postMessage);
}
catch (Exception ex)
{
db.LogError("An error occured: ", ex.ToString());
File.WriteAllText("LocalErrors.txt", ex.ToString());
}
}
}
Good
https://itnext.io/solid-principles-explanation-and-examples-715b975dcad4
class Post
{
private ErrorLogger errorLogger = new ErrorLogger();
void CreatePost(Database db, string postMessage)
{
try
{
db.Add(postMessage);
}
catch (Exception ex)
{
errorLogger.log(ex.ToString())
}
}
}
class ErrorLogger
{
void log(string error)
{
db.LogError("An error occured: ", error);
File.WriteAllText("LocalErrors.txt", error);
}
}
Better
<?php
class Car
{
public function speed()
{
return 'go fast';
}
public function steer()
{
return 'turn left';
}
}
$car = new Car;
$car->speed(); // go fast
$car->steer(); // turn left
class Accelerator
{
public function go()
{
return 'going fast';
}
}
$car = new Car(new Accelerator, new
Steering);
$car->speed(); // going fast
$car->steer(); // turn left
class Car
{
protected $speed;
lic function __construct(Accelerator $speed, Steering $steer)
{
$this->speed = $speed;
$this->steer = $steer;
}
public function speed()
{
echo $this->speed->go();
}
public function steer()
{
echo $this->steer->turn();
}
}
class Steering
{
public function turn()
{
return 'turn left';
}
}

Single responsibility pattern