Challenge: Solution Review

This lesson will explain the solution to the problem from the last coding challenge.

We'll cover the following

Solution #

Press + to interact
class Dress{
constructor(serialNumber,type,color,designer,availability){
this.serialNumber = serialNumber
this.type = type
this.color = color
this.designer = designer
this.availability = availability
this.price = 0
}
dressPrice(){
if(this.type == "maxi"){
this.price = 1000
}
if(this.type == "gown"){
this.price = 2000
}
if(this.type = "skirt"){
this.price = 500
}
return this.price
}
}
class DressFactory {
constructor() {
this.existingDresses = {}
}
createDress(serialNumber,type,color, designer, availability) {
var exists = this.existingDresses[serialNumber]
if (!!exists) {
return this.existingDresses[serialNumber]
}
else {
var dress = new Dress(serialNumber,type,color, designer, availability)
this.existingDresses[serialNumber]= dress
return dress
}
}
}
const factory = new DressFactory()
const pinkdress1 = factory.createDress("#123","skirt","pink","Zara","yes")
const pinkdress2 = factory.createDress("#123","skirt","pink","Zara","yes")
console.log(pinkdress1 === pinkdress2)
console.log(pinkdress1.dressPrice())
console.log(pinkdress2.dressPrice())

Explanation

As explained in the problem statement, we need to implement a functionality that doesn’t allow dresses with the same serialNumber to be made more than once. For this purpose, a flyweight factory can be used. Before we get into that, let’s look into the Dress class first.

class Dress{
  constructor(serialNumber,type,color,designer,availability){
    this.serialNumber = serialNumber
    this.type = type
    this.color = color
    this.designer = designer
    this.availability = availability
    this.price = 0
  }
  //code... 
}

A Dress instance contains the properties: serialNumber (a unique ID), a type (maxi, gown, or skirt), color, the designer who made the dress, its availability, and price.

The price is initialized to 0 as it is set later on using the dressPrice function. Let’s look into its implementation:

dressPrice(){
    if(this.type == "maxi"){
      this.price = 1000
    }
    if(this.type == "gown"){
      this.price = 2000
    }
    if(this.type = "skirt"){
      this.price = 500
    }
    return this.price
} 

Depending on the dress, we set the price equal to the price mentioned in the question and return it.

The next step is to set up a flyweight factory that monitors the number of dresses made corresponding to a serialNumber. We start by defining a class DressFactory.

class DressFactory{
  constructor() {
    this.existingDresses = {}
  }
  //code....
}

Since it is a factory that produces a lot of dresses, in its constructor, we initialize a variable existingDresses, that will store all the unique dresses made.

Next, we define the createDress function, which is responsible for monitoring the dress creation process.

class DressFactory{
  //code....
  createDress(serialNumber,type,color, designer, availability) {
  var exists;
    exists = this.existingDresses[serialNumber]
  
    if (!!exists) {
      return this.existingDresses[serialNumber]
    }
    else {
        var dress = new Dress(serialNumber,type,color, designer, availability)
        this.existingDresses[serialNumber]= dress
        return dress
      }
    }
}

The function accepts as parameters all the properties of a dress: serialNumber, type, color, designer, availability. Whenever a request to create a dress is invoked, the function first checks whether such a dress already exists or not. If not, it creates a new Dress instance, adds it to the list of existingDresses, and then returns it. In case an instance already exists, it is returned.

As you can see, if the same dress is instantiated twice, a new object is not created the second time.

console.log(pinkdress1 === pinkdress2) //true

Hence, the DressFactory prevents unnecessary clutter of objects. The Dress instance acts as our flyweight object containing the intrinsic information such as the dressPrice function.


Let’s discuss the proxy pattern in the next lesson.