0% found this document useful (0 votes)
23 views

SOLID Principles in JavaScript

The document discusses the Single Responsibility Principle (SRP) and illustrates bad code examples that violate various object-oriented design principles such as Open/Closed Principle (OCP), Liskov Substitution Principle (LSP), Interface Segregation Principle (ISP), and Dependency Inversion Principle (DIP). It highlights issues in class design and method implementation that lead to poor maintainability and functionality. The examples provided demonstrate how these principles can be misapplied in programming.

Uploaded by

Venna Brahmakoti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

SOLID Principles in JavaScript

The document discusses the Single Responsibility Principle (SRP) and illustrates bad code examples that violate various object-oriented design principles such as Open/Closed Principle (OCP), Liskov Substitution Principle (LSP), Interface Segregation Principle (ISP), and Dependency Inversion Principle (DIP). It highlights issues in class design and method implementation that lead to poor maintainability and functionality. The examples provided demonstrate how these principles can be misapplied in programming.

Uploaded by

Venna Brahmakoti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Single Responsibility Principle (SRP)

FOLLOW
Sina Riyahi
--- Bad Code

cLass AjaxAdapter extends Adapter {


constructor() {
Open/Closed Principle (OCP)
G
super();
th-Ls.name = "ajaxAdapter";
}
}

cLass NodeAdapter extends Adapter {


constructor() {
super();
th-Ls.name = "nodeAdapter";
}
}

cLass HttpRequester {
constructor(adapter) {
th-Ls.adapter = adapter;
}

fetch(urL) {
i._f (th-Ls.adapter . name "ajaxAdapter") {
return makeAjaxCaLL(UrL).then(response => {
// t,-ansfo,-m ,-esponse and ,-etu,-n
} );
} eLse i._f (th-Ls.adapter.name === "nodeAdapter") {
return makeHttpCaLL(UrL).then(response => {
// transfor-rn ,-esponse and ,-etur-n
} );
}
}
}

function makeAjaxCaLL(UrL) {
// request and return promise
}

function makeHttpCaLL(UrL) {
// request and return promise
}

FOLLOW
Sina Riyahi
---
CLass RectangLe
constructor()
{
{
Bad Code
Liskov Substitution Principle (LSP)
0
th"i..s .wi.dth ©;
th"i..s.hei.ght = ©;
}-

setCoL□r(c□L□r) {
//
}-

render(area) {
//
}-

setWi.dth(wi.dth) {
th"i..s .wi.dth = wi.dth;
}-

setHei.ght(hei.ght {
th"i..s .hei.ght hei.ght;
}-

getArea( {
return thi.s .wi.dth * thi.s .hei.ght;
}-
}-

CLass Square extends RectangLe {


setWi.dth(wi.dth) {
thi.s.wi.dth = wi.dth;
thi.s.hei.ght = wi.dth;
}-

setHei.ght(hei.ght) {
th"i..s.wi.dth hei.ght;
thi.s .hei.ght hei.ght;
}-
}-

functi.on renderLargeRectangLes(rectangLes) {
rectangLes.forEach(rectangLe => {
rectangLe. setWi.dth(4);
rectangLe. setHei.ght(S);
canst area = rectang�e.getArea( ); // BAD: Returns 25
for Square . ShOULd be 20 .
rectangLe.render(area);
}- ) ;
}-

canst rectangLes [new RectangLe( ), new RectangLe( ),


new Square( ) ] ;
renderLargeRectangLes(rectangLes);

FOLLOW
Interface Segregation Principle (ISP)

FOLLOW
Sina Riyahi
Dependency Inversion Principle (DIP)

You might also like