Skip to content

Instantly share code, notes, and snippets.

@mosdevly
Last active July 18, 2019 14:22
Show Gist options
  • Select an option

  • Save mosdevly/c5961b4c6943ac5268376bc16cb8017a to your computer and use it in GitHub Desktop.

Select an option

Save mosdevly/c5961b4c6943ac5268376bc16cb8017a to your computer and use it in GitHub Desktop.

Revisions

  1. Proto revised this gist Jul 18, 2019. 2 changed files with 20 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions solid_bad.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    """Open/Closed Principle
    Example inspired by this wonderful piece here: http://joelabrahamsson.com/a-simple-example-of-the-openclosed-principle/
    """

    # Area calculator: calculate area of a rectangle
    class Rectangle:
    def __init__(self, width, height):
    self.width = width
    self.height = height

    def area(self):
    return self.width * self.height

    # Calculate area of multiple rectangles

    class AreaCalculator:

    3 changes: 3 additions & 0 deletions solid_good.py
    Original file line number Diff line number Diff line change
    @@ -39,3 +39,6 @@ def __init__(self, radius):
    def area(self):
    import math
    return (self.radius**2)*math.pi
    class AreaCalculator:
    def calculate(self, shapes):

  2. Proto revised this gist Jul 18, 2019. 2 changed files with 41 additions and 15 deletions.
    15 changes: 0 additions & 15 deletions solid.py
    Original file line number Diff line number Diff line change
    @@ -1,15 +0,0 @@
    """
    Single Responsibility Principle
    Every module, class, or function should have responsibility over a single part
    of the functionality provided by the software.
    User class handles one thing: the identity of a user. It's sole method does exactly 1 thing: changes the name property.
    """
    class User:
    id = 1;
    name = '';

    def updateName(newName):
    this.name = newName
    console.log('Name updated.')
    41 changes: 41 additions & 0 deletions solid_good.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    """
    Single Responsibility Principle
    Every module, class, or function should have responsibility over a single part
    of the functionality provided by the software.
    User class handles one thing: the identity of a user. It's sole method does exactly 1 thing: changes the name property.
    """
    class User:
    id = 1;
    name = '';

    def updateName(newName):
    this.name = newName
    console.log('Name updated.')


    """Open/Closed Principle
    Example inspired by this wonderful piece here: http://joelabrahamsson.com/a-simple-example-of-the-openclosed-principle/
    """
    class Shape:
    def area(self):
    NotImplementedError('You must implement this method!')


    class Rectangle(Shape):
    def __init__(self, width, height):
    self.width = width
    self.height = height

    def area(self):
    return self.width * self.height


    class Circle(Shape):
    def __init__(self, radius):
    self.radius = radius

    def area(self):
    import math
    return (self.radius**2)*math.pi
  3. Proto revised this gist Jul 18, 2019. 2 changed files with 15 additions and 16 deletions.
    16 changes: 0 additions & 16 deletions solid.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +0,0 @@
    /**
    * Single Responsibilit Principle
    * @desc every module, class, or function should have responsibility over a single part
    * of the functionality provided by the software.
    *
    * User class handles one thing: the identity of a user. It's sole method does exactly 1 thing: changes the name property.
    */
    class User {
    id = 1;
    name = '';

    updateName(newName) {
    this.name = newName
    console.log('Name updated.')
    }
    {
    15 changes: 15 additions & 0 deletions solid.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    """
    Single Responsibility Principle
    Every module, class, or function should have responsibility over a single part
    of the functionality provided by the software.
    User class handles one thing: the identity of a user. It's sole method does exactly 1 thing: changes the name property.
    """
    class User:
    id = 1;
    name = '';

    def updateName(newName):
    this.name = newName
    console.log('Name updated.')
  4. Proto revised this gist Jul 18, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions solid.js
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@
    * @desc every module, class, or function should have responsibility over a single part
    * of the functionality provided by the software.
    *
    * Person class handles one thing: the identity of a person. It's sole method does exactly 1 thing: changes the name property.
    * User class handles one thing: the identity of a user. It's sole method does exactly 1 thing: changes the name property.
    */
    class Person {
    class User {
    id = 1;
    name = '';

  5. Proto revised this gist Jul 18, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions solid.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,12 @@
    /**
    * Single Responsibilit Principle
    * @desc every module, class, or function should have responsibility over a single part
    * of the functionality provided by the software.
    *
    * Person class handles one thing: the identity of a person. It's sole method does exactly 1 thing: changes the name property.
    */
    class Person {
    id = 1;
    name = '';

    updateName(newName) {
  6. Proto created this gist Jul 18, 2019.
    8 changes: 8 additions & 0 deletions solid.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    class Person {
    name = '';

    updateName(newName) {
    this.name = newName
    console.log('Name updated.')
    }
    {