Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Open/Closed principle */
- /* open for extensions, close for modification */
- /* the problem is we cant add new ice cream flavor without editing iceCreamFlavorsArray */
- /* we can easly fix that adding addFlavor function*/
- /* BAD */
- const iceCreamFlavors = ["chocolate", "vanilla"];
- const iceCreamMaker = {
- makeIceCream(flavor) {
- if (iceCreamFlavors.includes(flavor)) {
- console.log("You have a ice cream.")
- } else {
- console.log("no ice cream for you.")
- }
- }
- }
- iceCreamMaker.makeIceCream("chocolate")
- iceCreamMaker.makeIceCream("banana")
- /* GOOD */
- const iceCreamFlavors = ["chocolate", "vanilla"];
- const iceCreamMaker = {
- makeIceCream(flavor) {
- if (iceCreamFlavors.includes(flavor)) {
- console.log("You have a ice cream.")
- } else {
- console.log("no ice cream for you.")
- }
- },
- addFlavor(flavor) {
- iceCreamFlavors.push(flavor)
- }
- }
- iceCreamMaker.addFlavor("banana")
- iceCreamMaker.makeIceCream("banana")
Advertisement
Add Comment
Please, Sign In to add comment