Ir al contenido

03. Object Orientation

Object-Oriented Programming (OOP) is a paradigm that is based on the development of programs in the creation and management of elements...

0views

Object-Oriented Programming (OOP) is a paradigm that focuses on program development through the creation and management of independent elements called “objects,” simulating real-world objects. Each object is responsible for specific tasks and, through the creation of objects and the interaction between them, we advance the execution of the program.

Currently, JavaScript supports OOP through both the traditional “prototype” system and the modern class syntax introduced in ES6.

Object-Oriented Programming (OOP) is a programming paradigm that focuses on program development through the creation and management of independent elements called “objects,” which simulate real-world entities. Each object is responsible for specific tasks and, through the interaction between them, we advance the execution of the program.

In JavaScript, OOP can be applied in two ways:

  • The prototype system, where we create a prototype Object Literal and others inherit from this prototype.
  • The modern class syntax (introduced in ES6), which is closer to other languages and which we recommend.

It is the ability to represent a real-world entity in a simplified form within the code. This is achieved by defining classes that model characteristics (properties) and behaviors (methods) of objects and hiding unnecessary or complex details.

It is a template that allows the creation of objects. It defines the structure of the object by defining characteristics and behaviors.

It is an instance of a class (an element created from a class). It represents a real-world entity and has associated characteristics (properties) and behaviors (methods). Each object can have different property values, but they share the same methods defined in the class.

It is the mechanism for hiding an object's internal details (properties and methods), exposing only an interface to interact with it. Through private methods and properties, we control access to an object's properties and methods. Encapsulation ensures data integrity and allows its implementation (the code) to be hidden from external use.

Allows defining new classes from existing classes (superclass). The new classes (subclasses) inherit the properties and methods of the parent class. They can also add new functionalities or modify inherited behavior. Inheritance is useful for code reuse and establishing a class hierarchy.

It is the ability of an object to respond or behave differently to the same action or method (depending on the context). This allows a method with the same name to perform different tasks depending on the class that implements it. This process is implemented through inheritance and the ability to override methods in subclasses.

Constructor method: It is a special method that executes automatically when creating an object. It serves to initialize the object's properties.

Properties: Variables that represent the characteristics or the state of an object. They can be data types such as numbers, booleans, strings, arrays, other objects, etc.

Methods: Functions that define the behavior of an object. They can receive input parameters, return results, and manipulate the object's properties.

// Declaració d'una classe
class Jugador {
constructor(nom, vides) { // mètode constructor
this.nom = nom; // propietat
this.vides = vides;
this.posicio = 100;
this.punts = 0;
}
moureJugador() { // mètode públic
this.posicio += 5;
}
}

We will execute the code to instantiate and manage objects.

// Instanciació d'una classe
const jugador1 = new Jugador('Pepet', 3);
const jugador2 = new Jugador('Lucky', 5);
// Accés a propietats i ús de mètodes
console.log(`Nom Jugador 1:${jugador1.nom}`);
console.log(`Nom Jugador 2:${jugador2.nom}`);
jugador1.moureJugador();
jugador1.vides--;
for (let i = 0; i < 5; i++) {
jugador2.moureJugador();
}
console.log(`Vides Jugador 1:${jugador1.vides}`);
console.log(`Posicio Jugador 2:${jugador2.posicio}`);

We will establish inheritance and polymorphism in our classes.

// Classe base que representa un personatge del joc
class Entitat {
constructor(nom, vides, posicio) {
this.nom = nom;
this.vides = vides;
this.posicio = posicio;
}
moure() {
this.posicio.x += 1;
this.posicio.y += 1;
}
}
// Herència: Jugador hereta propietats i mètodes d'Entitat
class Jugador extends Entitat {
constructor(nom, vides, posicio) {
super(nom, vides, posicio); // crida al constructor de la classe base
this.punts = 0; // propietat específica de Jugador
}
// Polimorfisme: Jugador redefineix el mètode moure() de la classe base
moure() {
this.posicio.y += 1; // Moviment vertical
}
// Mètode propi del Jugador
sumarPunts(enemic) {
this.punts += 10;
enemic.vides--;
}
}
// Herència: Enemic hereta propietats i mètodes d'Entitat
class Enemic extends Entitat {
constructor(nom, vides, nivell) {
super(nom, vides);
this.nivell = nivell; // propietat específica de Enemic
}
// Polimorfisme: Enemic redefineix el mètode moure() de la classe base
moure() {
this.posicio.x -= 1; // Moviment horitzontal
}
// Mètode propi de l'Enemic
atacar(jugador) {
jugador.vides--;
jugador.punts = jugador.punts - this.nivell;
}
}
const jugador = new Jugador('Jugador1', 3, { x: 100, y: 100 });
const enemic = new Enemic('Enemic1', 2, { x: 300, y: 500 }, 1);
jugador.moure();
enemic.moure();
jugador.sumarPunts(enemic);
enemic.atacar(jugador);
console.log(`Vides Jugador: ${jugador.vides} i Posició: ${jugador.posicio.y}`);
console.log(`Vides Enemic: ${enemic.vides} i Posició: ${enemic.posicio.x}`);