Object-Oriented Programming

Object-oriented (OO) programming.

Object Design

Object design concepts:

  • GRASP

GRASP

General Responsibility Assignment Software Patterns (GRASP) is a collection of nine general patterns to assign responsibilities in object oriented programming.

GRASP at Wikipedia

OO Design Patterns

You can read more about design patterns on this post.

Types of methods

Types of method:

  • Factory method
  • Constructor

Factory method

A factory method is a method that is usually called as a static function (though it is not mandatory) and creates and returns an object.

Constructor

A constructor is a method that is automatically called when a new object from a class is created. This methods initializes the object values.

Inheritance

Some authors, like in JavaScript, recommend using delegation or composition instead of inheritance. It means using objects of a different class within a different object instead of direct inheritance.

Inheritance can be declared using classes (like C++, Java, or Python) or using prototype (like Self or JavaScript):

Class-based inheritance in Python:

class Vehicle:
    def __init__(self, name):
        self.name = name

    def drive_to_work(self):
        print(f"{self.name} is driving to work.")

    def deliver_materials(self):
        print(f"{self.name} is delivering construction materials.")

class SportsCar(Vehicle):
    def deliver_materials(self):
        print(f"{self.name} can't deliver materials — it's a sports car!")

bobs_sports_car = SportsCar("Bob's Porsche 911")
bobs_car.deliver_materials()

Prototype-based inheritance in JavaScript:

// Generic vehicle prototype
let vehicle = {
  name: "Generic Vehicle",
  driveToWork: function() {
    console.log(`${this.name} is driving to work.`);
  },
  deliverMaterials: function() {
    console.log(`${this.name} is delivering construction materials.`);
  }
};

// Bob's car is a copy of a vehicle, but we override what's needed
let bobsCar = Object.create(vehicle);
bobsCar.name = "Bob's Porsche 911";
bobsCar.deliverMaterials = function() {
  console.log(`${this.name} can't deliver materials — it's a sports car!`);
};

// Now use it
bobsCar.driveToWork();        // Output: Bob's Porche 91 is driving...
bobsCar.deliverMaterials();   // Ouput: Bob's Porche 911 can't deliver...

Object-oriented Programming Language

OO programming languages featured on this post:

  • C++
  • Java
  • JavaScript
  • Scala
  • Python
  • Smalltalk

You can read this post about C++.

You can read this post about Java.

You can read this post about JavaScript.

You can read this post about Python.

Smalltalk was oriented to educaton..

You might also be interested in…

Leave a Reply

Your email address will not be published. Required fields are marked *