S.O.L.I.D Principles In Dart The First 5 Principles on Object Oriented Designs

 It’s an acronym for 5 design principles for writing code which is maintainable, scalable and easier to understand. These principles are given by Bob. C Martin (uncle bob) more familiar to the developer world.

What Problem these S.O.L.I.D principles solved?

1. Single Responsibility principle

As it’s clear from its name single responsible. A class should only be responsible for one thing that means a class could change for only one reason.

class Result
{

checkResult(int rollno)
{
bool isRollnoValidate = isRollnovalidate();
if(isRollnoValidate)
{

ResultModel resultModel = searchResult();
showResult(resultModel);
}
else{
return "Invalid rollno";
}

}

isRollnovalidate()
{

return true;
}
// get request
searchResult()
{
// return result;
}
//painting
showResult(ResultModel model)
{
// show result in pleasant way
}

}

class ResultModel
{

}

After applying the Single Responsibility principle


class Result {
checkResult(int rollno) {
bool isRollnoValidate = Validate().isRollnovalidate();
if (isRollnoValidate) {
ResultModel resultModel = NetworkApi().searchResult();
Printing().showResult(resultModel);
} else {
return "Invalid rollno";
}
}
}

class Validate {
// this is responsible for validate
isRollnovalidate() {
return true;
}
}

class ResultModel {}

class Printing {
// this class is responsible for printing
showResult(ResultModel model) {
// show result in pleasant way
}
}

class NetworkApi {
// this class is responsible for fetching result
searchResult() {
return ResultModel();
}
}

2. OPEN-CLOSED PRINCIPLE

An entity should be open for extensions but closed for modification

class Result
{
mechanicalCheckResult()
{
// some code
}

civilCheckResult()
{
// some code
}

}

abstract class Result {
checkResult();
}

class ComputerScience implements Result {
@override
checkResult() {
// some code
}
}

class Civil implements Result {
@override
checkResult() {
// some code
}
}

class Mechanical implements Result {
@override
checkResult() {
// some code
}
}

3. Liskov Substitution Principle

It means how good is your design from abstraction perspective

 abstract class Result {
checkResult();

codingTestResult();
}

class MechanicalBranch extends Result {
@override
checkResult() {
// some code
}

/*
* Here it is logically incorrect
* */
@override
codingTestResult() {
// some code
}
}

class ComputerScienceBranch extends Result {
@override
checkResult() {
// some codet
}

@override
codingTestResult() {
// some code
}
}

After Applying the Liskov Substitution Principle

abstract class OfflineResult {
checkResult();
}

abstract class CodingResult {
codingTestResult();
}

class MechanicalBranch implements OfflineResult {
@override
checkResult() {
// some code
}
}

class ComputerScienceBranch implements OfflineResult, CodingResult {
@override
checkResult() {
// somecode
}

@override
codingTestResult() {
// somecode
}
}

4. Interface Segregation Principle

It states that no client should be forced to depend on methods it does not use.

Bad practiCe :

abstract class Result {
checkResult();

codingTestResult();
}

class MechanicalBranch implements Result {
@override
checkResult() {
// some code
}

/*
* Here we exposed client with the method which none of his * business
* */
@override
codingTestResult() {
// some code
}
}

class ComputerScienceBranch implements Result {
@override
checkResult() {
// some codet
}

@override
codingTestResult() {
// some code
}
}
abstract class OfflineResult {
checkResult();
}

abstract class CodingResult {
codingTestResult();
}

class MechanicalBranch implements OfflineResult {
@override
checkResult() {
// some code
}
}

class ComputerScienceBranch implements OfflineResult, CodingResult {
@override
checkResult() {
// somecode
}

@override
codingTestResult() {
// somecode
}
}

5. Dependency Inversion Principle

Abstractions should not depend on details(concrete implementations). They should depend on abstractions.

Depending on abstractions gives the freedom to be independent of the implementations. Let's dig into it.

abstract class Payment {
payment();
}

class PaymentViaCreditCard implements Payment
{
@override
payment() {
// some code
}
}
class PaymentViaDebitCard implements Payment
{
@override
payment() {
// some code
}
}
class PaymentViaBhimUPI implements Payment
{
@override
payment() {
// some code
}
}


class Checkout
{
// our checkout class knows nothing about how payment works
// its knows pay.payment() is paying method
checkOut(Payment pay)
{
pay.payment();
}
}

I highly recommend watching this video. That’s the minimum thing I can do to express my gratitude.

Conclusion :

A warm welcome for all the queries and suggestion. If you find anything that could be improved please let me know, I would love to improve

Comments

Popular posts from this blog

Flutter for Single-Page Scrollable Websites with Navigator 2.0

A Data Science Portfolio is More Valuable than a Resume

Better File Storage in Oracle Cloud