0% found this document useful (0 votes)
4 views

creational.prototype.prototype_factory.js

The document defines a set of JavaScript classes for managing employee addresses and serialization. It includes classes for Address, Employee, and Serializer, allowing for the creation and cloning of employee objects with their associated addresses. The EmployeeFactory class provides methods to create new employees for main and auxiliary offices, utilizing a serializer to handle object cloning and reconstruction.

Uploaded by

qsp41768
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

creational.prototype.prototype_factory.js

The document defines a set of JavaScript classes for managing employee addresses and serialization. It includes classes for Address, Employee, and Serializer, allowing for the creation and cloning of employee objects with their associated addresses. The EmployeeFactory class provides methods to create new employees for main and auxiliary offices, utilizing a serializer to handle object cloning and reconstruction.

Uploaded by

qsp41768
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

class Address

{
constructor(suite, streetAddress, city)
{
this.suite = suite;
this.streetAddress = streetAddress;
this.city = city;
}

toString()
{
return `Suite ${this.suite}, ` +
`${this.streetAddress}, ${this.city}`;
}
}

class Employee // renamed


{
constructor(name, address)
{
this.name = name;
this.address = address; //!
}

toString()
{
return `${this.name} works at ${this.address}`;
}

greet()
{
console.log(
`Hi, my name is ${this.name}, ` +
`I work at ${this.address.toString()}` //!
);
}
}

class Serializer
{
constructor(types){
this.types = types;
}

markRecursive(object)
{
// anoint each object with a type index
let idx = this.types.findIndex(t => {
return t.name === object.constructor.name;
});
if (idx !== -1)
{
object['typeIndex'] = idx;

for (let key in object)


{
if (object.hasOwnProperty(key) && object[key] != null)
this.markRecursive(object[key]); // ^^^^^^^^^^ important
}
}
}

reconstructRecursive(object)
{
if (object.hasOwnProperty('typeIndex'))
{
let type = this.types[object.typeIndex];
let obj = new type();
for (let key in object)
{
if (object.hasOwnProperty(key) && object[key] != null) {
obj[key] = this.reconstructRecursive(object[key]);
}
}
delete obj.typeIndex;
return obj;
}
return object;
}

clone(object)
{
this.markRecursive(object);
let copy = JSON.parse(JSON.stringify(object));
return this.reconstructRecursive(copy);
}
}

class EmployeeFactory
{
static _newEmployee(proto, name, suite)
{
let copy = EmployeeFactory.serializer.clone(proto);
copy.name = name;
copy.address.suite = suite;
return copy;
}

static newMainOfficeEmployee(name, suite)


{
return this._newEmployee(
EmployeeFactory.main, name, suite
);
}

static newAuxOfficeEmployee(name, suite)


{
return this._newEmployee(
EmployeeFactory.aux, name, suite
);
}
}

EmployeeFactory.serializer = new Serializer([Employee, Address]);


EmployeeFactory.main = new Employee(null,
new Address(null, '123 East Dr', 'London'));
EmployeeFactory.aux = new Employee(null,
new Address(null, '200 London Road', 'Oxford'));
let john = EmployeeFactory.newMainOfficeEmployee('John', 4321);
let jane = EmployeeFactory.newAuxOfficeEmployee('Jane', 222);

console.log(john.toString());
console.log(jane.toString());

You might also like