const Database = (function () {
let instance;
function createDatabaseInstance() {
return new Object('Database Instance');
}
function getDatabaseInstance() {
if (!instance) {
instance = createDatabaseInstance();
}
return instance;
}
return { getDatabaseInstance }
})();
const databaseInstance1 = Database.getDatabaseInstance();
const databaseInstance2 = Database.getDatabaseInstance();
console.log(databaseInstance1 === databaseInstance2);
const coffeeFactory = (function() {
const types = {};
return {
addType: function(type, Coffee) {
if (Coffee.property.order) {
types[type] = Coffee;
}
},
create: function(type, options) {
const Coffee = types[type];
return (Coffee ? new Coffee(options) : undefined);
}
}
})
const Coffee = (function() {
function Coffee(options) {
this.name = options.name;
this.price = options.price;
}
Coffee.prototype.order = function() {
console.log(`${this.name}를 ${price}에 주문합니다`);
}
Coffee.prototype.orderCancel = function(reason) {
console.log(`${this.name}를 ${reason}의 사유로 주문을 취소합니다. ${this.price}를 환불합니다.`);
}
})
coffeeFactory.addType('Ameracano', Coffee);
coffeeFactory.addType('ColdBrew', Coffee);
coffeeFactory.addType('Latte', Coffee);
const IceAmeracano = coffeeFactory.create('Ameracano', { name: 'Ice Americano', price: 3500 });
const HotAmeracano = coffeeFactory.create('Ameracano', { name: 'Hot Americano', price: 3000 });
const BanilaColdBrew = coffeeFactory.create('ColdBrew', { name: 'Banila Cold Brew', price: 4000 });
const ChocoColdBrew = coffeeFactory.create('ColdBrew', { name: 'Choco Cold Brew', price: 4500 });
function animalFactory() {
this.createAnimal = function(animalType) {
let animal;
switch(animalType) {
case 'dog':
animal = new Dog();
break;
case 'cat':
animal = new Cat();
break;
case 'horse':
animal = new Horse();
break;
default:
animal = new Monkey();
break;
}
return animal;
}
}
const Dog = function() {
this.makeSound = () => {
console.log('woof woof!');
}
}
const Cat = function() {
this.makeSound = () => {
console.log('prrr prrr meow!');
}
}
const Horse = function() {
this.makeSound = () => {
console.log('neeeighhh!')
}
}
const Monkey = function() {
this.makeSound = () => {
console.log('ooooh ahh oooh oooh!');
}
}
const factory = new animalFactory();
const jojo = factory.createAnimal('dog');
jojo.makeSound();
const smokey = factory.createAnimal('cat');
smokey.makeSound();
const princess = factory.createAnimal('horse');
princess.makeSound();
const kong = factory.createAnimal('monkey');
kong.makeSound();
class Person {
constructor(name, age, mother) {
this.name = name;
this.age = age;
this.mother = mother;
}
}
const tim = new Person('Tim', 31, null);
const tina = new Person('Tina', 57, null);
tim.mother = tina;
const grandma = new Person('Sherry', 80, null);
tina.mother = grandma;
// DagymServiceLibrary
export default class GymServiceMap {
id: string;
...
businessLicenseCopy: string;
constructor(gym: GymDoc) {
this.id = String(gym._id);
...
}
}
async findOneById(_id: string): Promise<GymServiceMap | null> {
if (!_id) {
return null;
}
const gymDoc: GymDoc = await this.Collection.findOne({
_id,
});
if (!gymDoc) {
return null;
} else {
**return new GymServiceMap(gymDoc);**
}
}
function CustomerPrototype(proto) {
this.proto = proto;
this.clone = function () {
var customer = new Customer();
customer.first = proto.first;
customer.last = proto.last;
customer.status = proto.status;
return customer;
};
}
function Customer(first, last, status) {
this.first = first;
this.last = last;
this.status = status;
this.say = function () {
console.log("name: " + this.first + " " + this.last +
", status: " + this.status);
};
}
function run() {
var proto = new Customer("n/a", "n/a", "pending");
var prototype = new CustomerPrototype(proto);
var customer = prototype.clone();
customer.say();
}
참고
https://coding-factory.tistory.com/708
https://www.hanbit.co.kr/channel/categor/category_view.html?cms_code=CMS8616098823
https://effortguy.tistory.com/182
https://www.hanbit.co.kr/channel/category/category_view.html?cms_code=CMS6705039023&cate_cd=
https://devfarming.tistory.com/4
devh.kr/2021/Design-Patterns-In-JavaScript/
https://tech-people.github.io/2020/01/08/java-design-pattern-creational/
https://velog.io/@ha0kim/Design-Pattern-생성-패턴Creational-Patterns
https://tech-people.github.io/2020/01/08/java-design-pattern-creational/