Notice
Recent Posts
Recent Comments
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

DeFacto-Standard IT

관계, UML, 소스코드 예시 본문

Design Pattern/References

관계, UML, 소스코드 예시

defacto standard 2017. 12. 10. 21:43

1. 연관 관계 (Association)

클래스들이 개념상 서로 연결. 한 클래스가 다른 클래스에서 제공하는 기능을 사용

 

1-1) 단방향 연관 관계 (Uni-Directional Relationship)

 - UML : 화살표 + 실선

 

 - 소스코드

public class Person {

	private Car car;
	
	public void doIt() {
		car.doIt();
	}
	
}
public class Car {
	
	public void doIt() {
		// ...
	}
	
}

 

1-2) 양방향 연관 관계 (Bi-Directional Relationship)

 - UML : 실선

 

 - 소스코드

public class Member {
	
	private Team team;
	
}
public class Team {

	private ArrayList<Member> member;

}

 

 

2. 의존 관계 (Dependency)

한 클래스가 다른 클래스에서 제공하는 기능을 사용 ( = 연관관계)

 차이점 : 두 클래스의 관계가 매우 짧은 시간만 유지.

 

 - UML : 화살표 + 점선

 

 - 소스코드

public class Car {
	
	public void doIt(GasPump gasPump) {
		gasPump.charge();
	}
	
}
public class GasPump {

	public void charge() {
		// ...
	}
	
}


 

 

3. 일반화 관계 (Generalization)

상속 관계 (IS-A 관계).  한 클래스가 다른 클래스를 포함하는 상위 개념

 - UML : 빈 삼각형 + 실선

 

 - 소스코드

public class SuperClass {

}
public class SubClass1 extends SuperClass{

}
public class SubClass2 extends SuperClass{

}
public class SubClass3 extends SuperClass{

}


 

 

4. 실체화 관계 (Realization)

인터페이스와 이를 실제로 실현한 클래스들 사이의 관계. 인터페이스 변수를 두어 구체적 클래스를 할당한다면, 추상적인 개체로 사용할 수 있다.


단, 인터페이스 내에 필드를 선언할 수 없고(static변수만 선언 가능), 일관된 로직이라면 구체 클래스에서 많은 소스코드의 중복이 발생할 여지가 존재한다.


 - UML1 : 빈 삼각형 + 점선

 

 - UML2

 

- 소스코드

 

public interface Interface {
	public void implementationMethod();
}
public class ConcreteClass1 implements Interface{

	@Override
	public void implementationMethod() {
		// TODO Auto-generated method stub
		
	}

}
public class ConcreteClass2 implements Interface{

	@Override
	public void implementationMethod() {
		// TODO Auto-generated method stub
		
	}

}


5. 집합 관계

클래스들 사이의 전체/부분 관계를 나타냄.


5-1) 집약관계 (Aggregation)


Computer를 구성하는 4개의 부품은, 외부에서 주입받는다(Dependency Injection).


따라서 Computer 객체가 GC에 의해 메모리 상에서 사라져도, 외부에서 해당 부품이 다른 레퍼런스가 조회하고 있다면 메모리 상에서 사라지지 않는다.


즉, 객체의 LifeCycle을 제어할 수 있다.




 - UML : 빈 마름모 + 실선

 

 - 소스코드

public class Computer {
	
	private MainBoard mainBoard;
	private CPU cpu;
	private Memory memory;
	private PowerSupply powerSupply;
	
	public Computer(MainBoard mainBoard, CPU cpu, Memory memory, PowerSupply powerSupply) {
		this.mainBoard = mainBoard;
		this.cpu = cpu;
		this.memory = memory;
		this.powerSupply = powerSupply;
	}
	
}

public class MainBoard {...}
public class CPU {...}
public class Memory {...}
public class PowerSupply {...}

 

 

 

5-2) 합성관계 (Composition)


Computer를 구성하는 4개의 부품은, Compuer 클래스 안에서 직접 만들어진다.


따라서 Computer 객체가 GC에 의해 메모리 상에서 사라진다면, 각 부품 모두 메모리 상에서 사라지게 된다.


즉, 객체의 LifeCycle을 제어할 수 없다.


 - UML : 꽉찬 마름모 + 실선

 

 - 소스코드

public class Computer {
	
	private MainBoard mainBoard;
	private CPU cpu;
	private Memory memory;
	private PowerSupply powerSupply;
	
	public Computer() {
		this.mainBoard = new MainBoard();
		this.cpu = new CPU();
		this.memory = new Memory();
		this.powerSupply = new PowerSupply();
	}
	
}

public class MainBoard {...}
public class CPU {...}
public class Memory {...}
public class PowerSupply {...}

 


Comments