0% found this document useful (0 votes)
2 views5 pages

SOLID_Principles_in_JavaScript_1740388573

The document discusses the Single Responsibility Principle (SRP) and illustrates examples of bad code that violate this principle, alongside other 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 adherence to these principles. The examples provided demonstrate how improper coding practices can result in unexpected behavior and maintenance challenges.

Uploaded by

y2vvy2zp45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

SOLID_Principles_in_JavaScript_1740388573

The document discusses the Single Responsibility Principle (SRP) and illustrates examples of bad code that violate this principle, alongside other 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 adherence to these principles. The examples provided demonstrate how improper coding practices can result in unexpected behavior and maintenance challenges.

Uploaded by

y2vvy2zp45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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