역할을 분담하는 것
응집도는 강하게 결합은 느슨하게
다음과 같은 예시
Person 코드
package model;
import java.time.LocalDate;
public class Person {
private String firstName;
private String lastName;
private LocalDate dob;
private Address address;
private Person spouse;
public Person getSpouse() {
return spouse;
}
public void setSpouse(Person spouse) {
this.spouse = spouse;
}
public Person(Person spouse) {
this.spouse = spouse;
}
public Person(String firstName, String lastName, LocalDate dob) {
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public LocalDate getDob() {
return dob;
}
}
Address 코드
package model;
public class Address {
private String address1;
private String address2;
private String city;
private String state;
public Address(String address1, String address2, String city, String state) {
this.address1 = address1;
this.address2 = address2;
this.city = city;
this.state = state;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
메인 코드
import model.Person;
import java.time.LocalDate;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
Person tom = new Person("Tome","Smith", LocalDate.of(1990,6,15));
Person janet = new Person("Janet", "Jackson", LocalDate.of(1985,12,4));
tom.setSpouse(janet);
}
}
메인코드에서 person을 불러온다
반응형
'Programming > Java, Spring' 카테고리의 다른 글
[Java 기초문법] constructors / getters & setters (0) | 2022.10.17 |
---|---|
[Java 기초문법] object Superclass / Member Visibility / 클래스 계층구조 이해 (0) | 2022.10.17 |
[Java 기초문법] 배열 Array 기초 / Methods / vararg / static (0) | 2022.10.17 |
[Java 기초문법] 변수 (0) | 2022.10.17 |
[Java 기초문법] 클래스 class 이해 (0) | 2022.10.17 |