How to do it...

  1. Open your command-line application and navigate to your workspace.
  2. Create a new folder named 09-02-assembling-instances-with-builders.
  1. Create a main.js file that defines a new class named Mission, which that takes a name constructor argument and assigns it to an instance property. Also, create a describe method that prints out some details:
// main.js 
class Mission { 
  constructor (name) { 
    this.name = name; 
  } 
 
  describe () { 
    console.log(`
The ${this.name} mission will be launched by a
${this.rocket.name}
rocket, and deliver a ${this.payload.name} to
${this.destination.name}. `); } }
  1. Create classes named Destination, Payload, and Rocket, which receive a name property as a constructor parameter and assign it to an instance property:
// main.js 
 
class Destination { 
  constructor (name) { 
    this.name = name; 
  } 
} 
 
class Payload { 
  constructor (name) { 
    this.name = name; 
  } 
} 
 
class Rocket { 
  constructor (name) { 
    this.name = name; 
  } 
} 
  1. Create a MissionBuilder class that defines the setMissionName, setDestination, setPayload, and setRocket methods:
// main.js 
class MissionBuilder { 
 
  setMissionName (name) { 
    this.missionName = name; 
    return this; 
  } 
 
  setDestination (destination) { 
    this.destination = destination; 
    return this; 
  } 
 
  setPayload (payload) { 
    this.payload = payload; 
    return this; 
  } 
 
  setRocket (rocket) { 
    this.rocket = rocket; 
    return this; 
  } 
} 
  1. Create a build method that creates a new Mission instance with the appropriate properties:
// main.js 
class MissionBuilder { 
  build () { 
    const mission = new Mission(this.missionName); 
    mission.rocket = this.rocket; 
    mission.destination = this.destination; 
    mission.payload = this.payload; 
    return mission; 
  } 
}  
  1. Create a main function that uses MissionBuilder to create a new mission instance:
// main.js 
export function main() { 
  // build an describe a mission 
  new MissionBuilder() 
    .setMissionName('Jade Rabbit') 
    .setDestination(new Destination('Oceanus Procellarum')) 
    .setPayload(new Payload('Lunar Rover')) 
    .setRocket(new Rocket('Long March 3B Y-23')) 
    .build() 
    .describe(); 
}  
  1. Start your Python web server and open the following link in your browser: http://localhost:8000/.
  2. Your output should appear as follows:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.89.18