From dd1cbc26462aa0ee4b6293a69579a181bf24a519 Mon Sep 17 00:00:00 2001 From: rodrigorvsn Date: Fri, 18 Aug 2023 23:13:28 -0300 Subject: [PATCH] feat(solid): move examples to readme file --- .../dependency_inversion_principle/README.md | 80 +++++++++++++++++- .../example-bad.js | 27 ------ .../example-good.js | 39 --------- .../interface_segregation_principle/README.md | 83 ++++++++++++++++++- .../example-bad.java | 36 -------- .../example-good.java | 33 -------- docs/SOLID/open_closed_principle/README.md | 72 +++++++++++++++- .../open_closed_principle/example-bad.jsx | 32 ------- .../open_closed_principle/example-good.jsx | 29 ------- .../single_responsibility_principle/README.md | 76 ++++++++++++++++- .../example-bad.java | 34 -------- .../example-good.java | 28 ------- 12 files changed, 307 insertions(+), 262 deletions(-) delete mode 100644 docs/SOLID/dependency_inversion_principle/example-bad.js delete mode 100644 docs/SOLID/dependency_inversion_principle/example-good.js delete mode 100644 docs/SOLID/interface_segregation_principle/example-bad.java delete mode 100644 docs/SOLID/interface_segregation_principle/example-good.java delete mode 100644 docs/SOLID/open_closed_principle/example-bad.jsx delete mode 100644 docs/SOLID/open_closed_principle/example-good.jsx delete mode 100644 docs/SOLID/single_responsibility_principle/example-bad.java delete mode 100644 docs/SOLID/single_responsibility_principle/example-good.java diff --git a/docs/SOLID/dependency_inversion_principle/README.md b/docs/SOLID/dependency_inversion_principle/README.md index 93f31a6..31a54c2 100644 --- a/docs/SOLID/dependency_inversion_principle/README.md +++ b/docs/SOLID/dependency_inversion_principle/README.md @@ -11,4 +11,82 @@ ___ ## Why? - We need to create specific interfaces instead a generic interface -- With specific interfaces, the classes do not require useless information \ No newline at end of file +- With specific interfaces, the classes do not require useless information + +## Examples (JS) + +### Bad example + +```js +class InventoryRequester { + constructor() { + this.REQ_METHODS = ["HTTP"]; + } + + requestItem(item) { + // ... + } +} + +class InventoryTracker { + constructor(items) { + this.items = items; + + // We have created a dependency on a specific request implementation. + this.requester = new InventoryRequester(); + } + + requestItems() { + this.items.forEach(item => { + this.requester.requestItem(item); + }); + } +} + +const inventoryTracker = new InventoryTracker(["apples", "bananas"]); +inventoryTracker.requestItems(); +``` + +### Good example + +```js +class InventoryTracker { + constructor(items, requester) { + this.items = items; + this.requester = requester; + } + + requestItems() { + this.items.forEach(item => { + this.requester.requestItem(item); + }); + } +} + +class InventoryRequesterV1 { + constructor() { + this.REQ_METHODS = ["HTTP"]; + } + + requestItem(item) { + // ... + } +} + +class InventoryRequesterV2 { + constructor() { + this.REQ_METHODS = ["WS"]; + } + + requestItem(item) { + // ... + } +} + +/* Constructing our dependencies externally and injecting them, we can easily substitute our request module for another */ +const inventoryTracker = new InventoryTracker( + ["apples", "bananas"], + new InventoryRequesterV2() +); +inventoryTracker.requestItems(); +``` \ No newline at end of file diff --git a/docs/SOLID/dependency_inversion_principle/example-bad.js b/docs/SOLID/dependency_inversion_principle/example-bad.js deleted file mode 100644 index 2bd7426..0000000 --- a/docs/SOLID/dependency_inversion_principle/example-bad.js +++ /dev/null @@ -1,27 +0,0 @@ -class InventoryRequester { - constructor() { - this.REQ_METHODS = ["HTTP"]; - } - - requestItem(item) { - // ... - } -} - -class InventoryTracker { - constructor(items) { - this.items = items; - - // ! We have created a dependency on a specific request implementation. - this.requester = new InventoryRequester(); - } - - requestItems() { - this.items.forEach(item => { - this.requester.requestItem(item); - }); - } -} - -const inventoryTracker = new InventoryTracker(["apples", "bananas"]); -inventoryTracker.requestItems(); \ No newline at end of file diff --git a/docs/SOLID/dependency_inversion_principle/example-good.js b/docs/SOLID/dependency_inversion_principle/example-good.js deleted file mode 100644 index 3dfb4b9..0000000 --- a/docs/SOLID/dependency_inversion_principle/example-good.js +++ /dev/null @@ -1,39 +0,0 @@ -class InventoryTracker { - constructor(items, requester) { - this.items = items; - this.requester = requester; - } - - requestItems() { - this.items.forEach(item => { - this.requester.requestItem(item); - }); - } -} - -class InventoryRequesterV1 { - constructor() { - this.REQ_METHODS = ["HTTP"]; - } - - requestItem(item) { - // ... - } -} - -class InventoryRequesterV2 { - constructor() { - this.REQ_METHODS = ["WS"]; - } - - requestItem(item) { - // ... - } -} - -/* // ! Constructing our dependencies externally and injecting them, we can easily substitute our request module for another */ -const inventoryTracker = new InventoryTracker( - ["apples", "bananas"], - new InventoryRequesterV2() -); -inventoryTracker.requestItems(); \ No newline at end of file diff --git a/docs/SOLID/interface_segregation_principle/README.md b/docs/SOLID/interface_segregation_principle/README.md index 8d6ea48..534b010 100644 --- a/docs/SOLID/interface_segregation_principle/README.md +++ b/docs/SOLID/interface_segregation_principle/README.md @@ -11,4 +11,85 @@ ___ ## Why? - We need to create specific interfaces instead a generic interface -- With specific interfaces, the classes do not require useless information \ No newline at end of file +- With specific interfaces, the classes do not require useless information + +## Examples (Java) + +### Bad example + +```js +interface Birds { + public function setLocale($longitude, $latitude); + + public function setAltitude($altitude); + + public function render(); +} + +class Parrot implements Birds { + public function setLocale($longitude, $latitude) { + // bla bla bla + } + + public function setAltitude($altitude) { + // bla bla bla + } + + public function render() { + // bla bla bla + } +} + +/* Here the "setAltitude" is required to penguim, but penguins doesnt fly! */ +class Penguim implements Birds { + public function setLocale($longitude, $latitude) { + // bla bla bla + } + + public function setAltitude($altitude) { + // ! bla bla bla + } + + public function render() { + // bla bla bla + } +} +``` + +### Good example + +```js +interface Birds { + public function setLocale($longitude, $latitude); + public function render(); +} + +/* Here we're implementing a specific class to be used by birds who fly */ +interface BirdsFly extends Birds { + public function setAltitude($altitude); +} + +class Parrot implements BirdsFly { + public function setLocale($longitude, $latitude) { + // bla bla bla + } + + public function setAltitude($altitude) { + // bla bla bla + } + + public function render() { + // bla bla bla + } +} + +class Penguim implements Birds { + public function setLocale($longitude, $latitude) { + // bla bla bla + } + + public function render() { + // bla bla bla + } +} +``` diff --git a/docs/SOLID/interface_segregation_principle/example-bad.java b/docs/SOLID/interface_segregation_principle/example-bad.java deleted file mode 100644 index 79a0382..0000000 --- a/docs/SOLID/interface_segregation_principle/example-bad.java +++ /dev/null @@ -1,36 +0,0 @@ -interface Birds { - public function setLocale($longitude, $latitude); - - public function setAltitude($altitude); - - public function render(); -} - -class Parrot implements Birds { - public function setLocale($longitude, $latitude) { - // bla bla bla - } - - public function setAltitude($altitude) { - // bla bla bla - } - - public function render() { - // bla bla bla - } -} - -/* Here the "setAltitude" is required to penguim, but penguins doesnt fly! */ -class Penguim implements Birds { - public function setLocale($longitude, $latitude) { - // bla bla bla - } - - public function setAltitude($altitude) { - // ! bla bla bla - } - - public function render() { - // bla bla bla - } -} \ No newline at end of file diff --git a/docs/SOLID/interface_segregation_principle/example-good.java b/docs/SOLID/interface_segregation_principle/example-good.java deleted file mode 100644 index 5365970..0000000 --- a/docs/SOLID/interface_segregation_principle/example-good.java +++ /dev/null @@ -1,33 +0,0 @@ -interface Birds { - public function setLocale($longitude, $latitude); - public function render(); -} - -/* // ! Here we're implementing a specific class to be used by birds who fly */ -interface BirdsFly extends Birds { - public function setAltitude($altitude); -} - -class Parrot implements BirdsFly { - public function setLocale($longitude, $latitude) { - // bla bla bla - } - - public function setAltitude($altitude) { - // bla bla bla - } - - public function render() { - // bla bla bla - } -} - -class Penguim implements Birds { - public function setLocale($longitude, $latitude) { - // bla bla bla - } - - public function render() { - // bla bla bla - } -} \ No newline at end of file diff --git a/docs/SOLID/open_closed_principle/README.md b/docs/SOLID/open_closed_principle/README.md index 49fbb7f..9458407 100644 --- a/docs/SOLID/open_closed_principle/README.md +++ b/docs/SOLID/open_closed_principle/README.md @@ -10,4 +10,74 @@ ___ ## Why? -- With that, we extends the behavior using heritage, interfaces and compositions, with more abstraction and less coupling \ No newline at end of file +- With that, we extends the behavior using heritage, interfaces and compositions, with more abstraction and less coupling + + +## Examples (React) + +### Bad example + +```jsx +const Header = () => { + const { pathname } = useRouter() + + return ( +
+ + + {/* Here we are letting the component take the responsability from the + consumer. If we need to define a new route, f.e., we need to change this + file and insert a new code line. So, we have a bad coupling */} + {pathname === '/dashboard' && Create event} + {pathname === '/' && Go to dashboard} + +
+ ) +} + +const HomePage = () => ( + <> +
+ + +) + +const DashboardPage = () => ( + <> +
+ + +) +``` + +### Good example + +```jsx +const Header = ({ children }) => ( +
+ + + {/* Now we use composition to modify by outside the content of this component, instead changing direct in this component */} + {children} + +
+) + +const HomePage = () => ( + <> +
+ Go to dashboard +
+ + +) + +const DashboardPage = () => ( + <> +
+ Create event +
+ + +) +``` \ No newline at end of file diff --git a/docs/SOLID/open_closed_principle/example-bad.jsx b/docs/SOLID/open_closed_principle/example-bad.jsx deleted file mode 100644 index c115064..0000000 --- a/docs/SOLID/open_closed_principle/example-bad.jsx +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint-disable */ - -const Header = () => { - const { pathname } = useRouter() - - return ( -
- - - {/* Here we are letting the component take the responsability from the - consumer. If we need to define a new route, f.e., we need to change this - file and insert a new code line. So, we have a bad coupling */} - {pathname === '/dashboard' && Create event} - {pathname === '/' && Go to dashboard} - -
- ) -} - -const HomePage = () => ( - <> -
- - -) - -const DashboardPage = () => ( - <> -
- - -) diff --git a/docs/SOLID/open_closed_principle/example-good.jsx b/docs/SOLID/open_closed_principle/example-good.jsx deleted file mode 100644 index 9afa25e..0000000 --- a/docs/SOLID/open_closed_principle/example-good.jsx +++ /dev/null @@ -1,29 +0,0 @@ -/* eslint-disable */ - -const Header = ({ children }) => ( -
- - - {/* Now we use composition to modify by outside the content of this component, instead changing direct in this component */} - {children} - -
-) - -const HomePage = () => ( - <> -
- Go to dashboard -
- - -) - -const DashboardPage = () => ( - <> -
- Create event -
- - -) diff --git a/docs/SOLID/single_responsibility_principle/README.md b/docs/SOLID/single_responsibility_principle/README.md index f1d332b..64b6cda 100644 --- a/docs/SOLID/single_responsibility_principle/README.md +++ b/docs/SOLID/single_responsibility_principle/README.md @@ -13,4 +13,78 @@ ___ - Helps the maintenance - Help to refactor - Help to make a reusable code -- Less git conflicts \ No newline at end of file +- Less git conflicts + +## Examples (Java) + +### Bad example + +```js +public class Facture { + private Book book; + private int quantity; + private double percDiscount; + private double percTax; + private double total; + + public Facture(Book book, int quantity, double percDiscount, double percTax) { + this.book = book; + this.quantity = quantity; + this.percDiscount = percDiscount; + this.percTax = percTax; + this.total = this.calculateTotal(); + } + + public double calculateTotal() { + double price = ((book.price - book.price * percDiscount) * this.quantity); + double taxPrice = price * (1 + percTax); + return taxPrice; + } + + // Print facture should not be the Facture class responsability! + public void printFacture() { + System.out.println(quantity + "x " + book.nome + " " + book.price + "$"); + System.out.println("Discount percentage: " + percDiscount); + System.out.println("Tax percentage: " + percTax); + System.out.println("Total: " + total); + } + + // Save facture should not be the Facture class responsability! + public void salveToFile(String nomeArquivo) { + // bla bla bla + } +} +``` + +### Good example + +```js +// Print Facture now have his own class and responsability! +public class PrintFacture { + private Facture facture; + + public PrintFacture(Facture facture) { + this.facture = facture; + } + + public void printFacture() { + System.out.println(quantity + "x " + book.nome + " " + book.price + "$"); + System.out.println("Discount percentage: " + percDiscount); + System.out.println("Tax percentage: " + percTax); + System.out.println("Total: " + total); + } +} + +// Save Facture now have his own class and responsability! +public class SaveFacture { + Facture facture; + + public SaveFacture(Facture facture) { + this.facture = facture; + } + + public void saveToFile(String filename) { + // bla bla bla + } +} +``` \ No newline at end of file diff --git a/docs/SOLID/single_responsibility_principle/example-bad.java b/docs/SOLID/single_responsibility_principle/example-bad.java deleted file mode 100644 index 43641fe..0000000 --- a/docs/SOLID/single_responsibility_principle/example-bad.java +++ /dev/null @@ -1,34 +0,0 @@ -public class Facture { - private Book book; - private int quantity; - private double percDiscount; - private double percTax; - private double total; - - public Facture(Book book, int quantity, double percDiscount, double percTax) { - this.book = book; - this.quantity = quantity; - this.percDiscount = percDiscount; - this.percTax = percTax; - this.total = this.calculateTotal(); - } - - public double calculateTotal() { - double price = ((book.price - book.price * percDiscount) * this.quantity); - double taxPrice = price * (1 + percTax); - return taxPrice; - } - - // Print facture should not be the Facture class responsability! - public void printFacture() { - System.out.println(quantity + "x " + book.nome + " " + book.price + "$"); - System.out.println("Discount percentage: " + percDiscount); - System.out.println("Tax percentage: " + percTax); - System.out.println("Total: " + total); - } - - // Save facture should not be the Facture class responsability! - public void salveToFile(String nomeArquivo) { - // bla bla bla - } -} \ No newline at end of file diff --git a/docs/SOLID/single_responsibility_principle/example-good.java b/docs/SOLID/single_responsibility_principle/example-good.java deleted file mode 100644 index aaa98f2..0000000 --- a/docs/SOLID/single_responsibility_principle/example-good.java +++ /dev/null @@ -1,28 +0,0 @@ -// Print Facture now have his own class and responsability! -public class PrintFacture { - private Facture facture; - - public PrintFacture(Facture facture) { - this.facture = facture; - } - - public void printFacture() { - System.out.println(quantity + "x " + book.nome + " " + book.price + "$"); - System.out.println("Discount percentage: " + percDiscount); - System.out.println("Tax percentage: " + percTax); - System.out.println("Total: " + total); - } -} - -// Save Facture now have his own class and responsability! -public class SaveFacture { - Facture facture; - - public SaveFacture(Facture facture) { - this.facture = facture; - } - - public void saveToFile(String filename) { - // bla bla bla - } -} \ No newline at end of file