It's #FrontendFriday

It's #FrontendFriday - Factory Method - Creates objects in a central location

,

Hello #FrontendFriday-reader. In today's issue, we will be delving into the Factory Method.

The Factory Method was already mentioned in the blog from the previous issue on Coding Patterns JavaScript addressed.

We look together at what exactly it is and how it is constructed.

What is Factory Method?

This is an approach in web development to create objects without having to specify the exact class of these objects.

This makes it flexible and convenient to exchange these objects in the future, as well as during implementation. Depending on the use case, these objects can be specified in an interface and implemented in the child class.

As we know from other use cases, these objects can be overwritten by derived classes if they are implemented in the base class.

Should every web developer use it? Relevance?

As every application in development can have both advantages and disadvantages, I have developed a "concept" for myself.

When is it useful to use this

  • As soon as our object or component setup has a high degree of complexity.
  • If we need to easily create different instances of objects depending on the environment.
  • When we work with many small objects or components that have the same properties.

When would it no longer be useful

If this pattern is applied to the wrong type of problem, it can lead to unnecessarily high complexity of an application.

If providing an interface for object creation is not a design goal for the library or framework. For this, I would suggest sticking with explicit constructors. This way you can avoid the unnecessary overhead.

As the process of object creation is effectively abstracted behind an interface, this can also lead to problems with unit testing.

Code example

In the following example you can see how such a method factory can be easily set up:

// Erstellen wir eine Fahrzeug-Factory, wo eine Farbe ausgewählt werden kann
// Hierbei erstellen wir unseren Konstruktor, um die Farbe des Fahrzeugs festzustellen.

class CarFactory {
  constructor() {
    this.createCar = function(color) {
      let carColor;
      if (color === 'black'){
          carColor = new BlackPaint();
      }  
      else if (color === 'white'){
          carColor = new WhitePaint();
      } 
      else if (color === 'red'){
          carColor = new RedPaint();
      }
      return carColor;
    };
  }
}

// Die Eigenschaftsklassen werden somit erstellen. Hierbei handelt es sich um die Außenfarbe vom Fahrzeug
class BlackPaint {
  constructor() {
    this.carColorcolor = "black";
    this.message = function() {
      return `You choose the ${this.carColorcolor} color for your car.`;
    };
  }
}

class WhitePaint {
  constructor() {
    this.carColorcolor = "white";
    this.message = function() {
      return `You choose the ${this.carColorcolor} color for your car.`;
    };
  }
}

class RedPaint{
  constructor() {
    this.carColorcolor = "red";
    this.message = function() {
      return `You choose the ${this.carColorcolor} color for your car.`;
    };
  }
}

// Nun erstellen wir Objekte aus der Factory
const carFactory = new CarFactory();

const black = carFactory.createCar('black');
const white = carFactory.createCar('white');
const red = carFactory.createCar('red');

console.log(black.message()); // You choose the black color for your car.
console.log(white.message()); // You choose the white color for your car.
console.log(red.message()); // You choose the red color for your car.

 

The Webfrontend FG wishes you lots of fun & a wonderful weekend!

 

Learn more about Java programming

Bekir Erpolat

About ME

Bekir Erpolat is an IT specialist for application development and has experience in various IT projects for web and backend development, which he brings to doubleSlash. He has a broad base of technical skills, such as Java (EE/Spring) or HTML, CSS and JS. Bekir is passionate about turning customer requirements into technical solutions and is involved in software projects at ZF and BMW active. He likes working in a team and also enjoys working with new technologies.

All contributions from Bekir Erpolat

Learn more

Further information on our website and in our newsletter

Arrow up