Four Pillars of Object Oriented Programming
Four Pillars of Object-oriented Programming (OOP)
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Encapsulation
In object-oriented programming we group related variable and functions that operate on them into objects and it is called encapsulation. Encapsulation helps keep our properties and method code protected within the class and only accessible through a public programming interface. Let's look at the example.
• Reduce Complexity - We group related variables and functions together and this way we can reduce complexity.
• Increase re-usability - We can reuse these objects in different parts of a program or in different programs.
Abstraction
Abstraction helps us map the business requirements to an initial set of classes. Think of DVD player as an object. This DVD player has a complex logic board on the inside and a few buttons on the outside that you interact with. You simply press the play button and you don't care what happens on the inside. All that complexity is hidden from you. This is
• Simple Interface - We'll make the interface of those objects simpler. Understanding an object with a fewer properties and methods is easier than on object with several properties and methods.
• Reduce Complexity - With abstraction, we hide the details and the complexity and show only the essentials. This technique reduces complexity.
• Reduce or isolate the impact of change - Let's imagine that tomorrow we change inner or private methods of the object. None of these changes will leak to the outside because we don't have any code that touches these methods outside of their content object. We may delete a method or change its parameters but none of these changes will impact the rest of the applications code. So with abstraction we reduce the impact of change.
Inheritance
Inheritance is a mechanism that allows you to eliminate redundant code. It helps us leverage reuse. Here is an example to think of HTML elements like text boxes, drop-down lists, check boxes and so on. All these elements have a few things in common.
It should have properties like hidden and inner HTML and methods like click and focus. Instead of redefining all these properties and methods for every type of HTML elements we can define them once in a generic object. Lets called it HTML element and have other objects inherit these properties and methods.
Benefit: With
Polymorphism
Poly means many and morph means form.
If we want to render multiple HTMl elements with object orientation, we can implement a render method in each of these objects and render method will behave differently depending on the type of the object viewer referencing. We can use one line of code like this
Benefit: With
Source: Object-oriented Programming in JavaScript: Made Super Simple | Mosh Hamedani
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Encapsulation
In object-oriented programming we group related variable and functions that operate on them into objects and it is called encapsulation. Encapsulation helps keep our properties and method code protected within the class and only accessible through a public programming interface. Let's look at the example.
let employee = { baseSalary: 30000, overtime: 10, rate: 20, getWage: function() { return this.baseSalary + (this.overtime * this.rate) } }; employee.getWage();We don't have any parameters in the getWage function is because all these parameters are actually modelled as properties of this object. All these properties and getWage function are highly related so they are a part of one unit (i.e employee object). In object-oriented way, your functions end up having fewer and fewer parameters. If fewer the number of parameters the easier it is to use and maintain that function so this is called
encapsulation
.Encapsulation
gives a couple of benefits
as follows.• Reduce Complexity - We group related variables and functions together and this way we can reduce complexity.
• Increase re-usability - We can reuse these objects in different parts of a program or in different programs.
Abstraction
Abstraction helps us map the business requirements to an initial set of classes. Think of DVD player as an object. This DVD player has a complex logic board on the inside and a few buttons on the outside that you interact with. You simply press the play button and you don't care what happens on the inside. All that complexity is hidden from you. This is
abstraction
in practice. We can use same technique in our objects so we can hide some of the properties and methods from the outside and this gives a couple of benefits
.• Simple Interface - We'll make the interface of those objects simpler. Understanding an object with a fewer properties and methods is easier than on object with several properties and methods.
• Reduce Complexity - With abstraction, we hide the details and the complexity and show only the essentials. This technique reduces complexity.
• Reduce or isolate the impact of change - Let's imagine that tomorrow we change inner or private methods of the object. None of these changes will leak to the outside because we don't have any code that touches these methods outside of their content object. We may delete a method or change its parameters but none of these changes will impact the rest of the applications code. So with abstraction we reduce the impact of change.
Inheritance
Inheritance is a mechanism that allows you to eliminate redundant code. It helps us leverage reuse. Here is an example to think of HTML elements like text boxes, drop-down lists, check boxes and so on. All these elements have a few things in common.
It should have properties like hidden and inner HTML and methods like click and focus. Instead of redefining all these properties and methods for every type of HTML elements we can define them once in a generic object. Lets called it HTML element and have other objects inherit these properties and methods.
Benefit: With
inheritance
we can eliminate redundant code.Polymorphism
Poly means many and morph means form.
Polymorphism
means many forms. In object-oriented programming polymorphism is a technique that allows you to get rid of long if-and-else or Switch-case statements. So back to our HTML elements example, all these objects should have the ability to be rendered on a page but the way each element is rendered is different from others.
If we want to render multiple HTMl elements with object orientation, we can implement a render method in each of these objects and render method will behave differently depending on the type of the object viewer referencing. We can use one line of code like this
element.render();
Benefit: With
polymorphism
we can refactor ugly switch case statements.Source: Object-oriented Programming in JavaScript: Made Super Simple | Mosh Hamedani
Comments
Post a Comment