JavaScript(8) 클래스 관계와 UML

김재홍·2022년 5월 20일
0

UML: Unified Modeling Language

통합 모델링 언어
시스템을 모델로 표현해주는 대표적인 모델링 언어

  1. 상속 (generalization)
  • 일반화된 사물과 특수화된 사물과의 관계를 표현한다.

구현 소스

function Coffee() {
	this.name= "Coffee"
}

Coffee.prototype.getName = function() {
	return this.name
};

Coffee.prototype.setName = function(name) {
	this.name
};

Coffee.prototype.display = function() {
	console.log(this.name)
};

let coffee = new Coffee();

function Espresso() {
	Coffee.call(this);
}

// Coffee 생성자 상속
Espresso.prototype = Object.create(Coffee.proto, { 			constructor: {
		value: Espresso,
		configurable: true,
		enumerable: true,
		writable: true
	}
});

let espresso = new Espresso();
espresso.setName("Espresso");
  1. 구현(realizatino)
  • 추상화된 인터페이스와 실제로 구현한 사물과의 관계를 표현

구현 소스

function ICoffee() {

}

ICoffee.prototype.getName = function() {
	throw new Error(' You have to implement the method doSomething!');
};

ICoffee.prototype.setName = function(name) {
	throw new Error(' You have to implement the method doSomething!');
};

ICoffee.prototype.display = function() {
	throw new Error(' You have to implement the method doSomething!');
};

function Espresso() {
	this.name = "Espresso"
}

// 	ICoffee 생성자 구현
Espresso.prototype(ICoffee.prototype, { 
	constructor: {
    	value: Espresso,
        configurable: true,
        enumerable: true,
        writable: true
    }
});

// Espresso 함수 오버라이딩

Espresso.prototype.getName = function() {
	return this.name
};

Espresso.prototype.setName = function(name) {
	this.name
};

Espresso.prototype.display = function() {
	console.log(this.name)
};

var espresso = new Espresso();

console.log(espresso.getName());
// > Espresso

espresso.display();
// > Espresso
  1. 연관(association)
  • 한 사물의 객체가 다른 사물 객체와 연결되어 있음을 표현한다.

구현 소스

function Espresso() {
	this.name = "Espresso";
}

Espresso.prototype.display = fucntion() {
	console.log(this.name);
};

fucntion Baristar() {
	// Coffee 객체 프로퍼티로 정의
    this.espressoMachine = null;
}

Barista.prototype.setEspressoMachine = fucntion(espressoMachine) {
	this.espressoMachine = espressoMachine;
}

Barista.prototype.makeEspresso = function() {
	var espresso = this.espressoMachine.makeEspresso();
    
    return espresso;
}

function EspressoMachine() {
	this.price = 3000000
}

EspressoMachine.prototype.makeEspresso = function() {
	return new Espresso();
}

var barista = new Barista();
var espressoMachine = new EspressoMachine();

barista.setEspressoMachine(espressoMachine);

var espresso = barista.makeEspresso();
espresso.display();
// > Espresso
  1. 집합
  • 전체와 부분간의 관계를 표현

구현 소스

fucntion CoffeBeans() {
	this.countryOfOrigin = "Colombia";
}

CoffeeBeans.prototype.toString = function() {
	return "CoffeeBeans ["+ this.countryOfOrigin+"]";
}

function Espresso() {
	this.name = "Espresso";
}

Espresso.prototype.toTring = function() {
	return "Espresso";
}

function Milk() {
	this.name = "Milk";
}

Milk.prototype.toString() {
	return "Milk";
}

function CafeLatte() {
	this.name = "CafeLatte';
    
    // Coffee 객체 프로퍼티로 정의
    this.espresso = null;
   	this.milk = null;
}

// Coffe 객체 프로퍼티를 외부에서 설정하는 함수를 제공한다.
CafeLatte.prototype.setEspresso = function(espresso) {
	this.espresso = espresso;
}

CafeLatte.prototype.setMilk = function(milk) {
	this.milk = milk;
};

CafeLatte.prototype.display = function() {
	console.log(this.name + "(" + this.espresso + "+" + this.milk + ")");
};

function Barista() {
	this.espressoMachine = null;
}

Barista.prototype.setEspressoMachine = function(espressoMachine) { 
	this.espressoMachine = espressoMachine;
}

Barista.prototype.makeEspresso = function() {
	var coffeBeans = new CoffeeBeans();
    var espresso = this.espressoMachine.makeEspresso(coffeBeans);
    
    return espresso;
};

Barista.prototype.makeCafeLatte = function() {
	var coffeeBeans = new CoffeBeans();
   	var espresso = this.espressoMachine.makeEspresso(coffeBeans);
    var milk = new Milk();
    var cafeLatte = new CafeLatte();
    
    cafeLatte.setEspresso(espresso);
    cafeLatte.setMilk(milk);
    
    return cafeLatte;
};

function EspresooMachine() {
	this.price = 3000000;
}

EspressoMachine.prototype.makeEspresso = function(coffeBeans) {
	fonsole.log(""+coffeBeans);
    
    return new Espresso();
}

var barista = new Barista();

var espressoMachine = new EspressoMachine();

barista.setEspressoMachine(espressoMachine);

var cafeLatte = barista.makeCafeLatte();

cafeLatte.display();
// > CafeLatte(Espresso+Milk)
  1. 합성(composition)
  • 전체와 부분간의 관계를 표현한다
  • 전체가 소멸될 떄 부분도 함께 소멸되는 특징이 있다.

  1. 의존(dependency)
  • 한 사물이 변경되면 이것을 사용하는 다른 사물에 영향을 미치는 관계를 표현한다.

0개의 댓글

관련 채용 정보