좋은 객체 지향 설계
객체 지향 프로그래밍
객체
들의 모임을 파악다형성
역할과 구현
으로 구분하면 단순, 유연해짐 인터페이스
구현체
SRP(Single Responsibility Principle)
변경
에 대한 파급 효과가 적어야 함OCP(Open/Closed Principle)
LSP(Liskov Substitution Principle)
ISP(Interface Segregation Principle)
DIP(Dependency Inversion Principle)
변경에 유연
한 프로그래밍을 할 수 있음interface MemberRepository {}
class MemoryMemberRepository implements MemberRepository {}
class JpaMemberRepository implements MemberRepository {}
다형성
을 이용, MemoryMembeRepository를 MemberRepository 타입에 초기화class MemberSerivce() {
MemberReository memberRepository = new MemoryMemberRepository();
}
class MemberSerivce() {
MemberReository memberRepository = new JpaMemberRepository();
}
POJO (Plain Old Java Object)
무거운 객체
를 만들어 코드의 가독성, 확장성, 유지보수가 떨어지는 단점을 보안하기 위해 등장한 개념getter/setter
로 구성된 순수한 형태의 기본 클래스public class Score{
private int kor;
private int eng;
private int mat;
public int getKor() {return kor;}
public void setKor(int kor) {this.kor = kor;}
public int getEng() {return eng;}
public void setEng(int eng) {this.eng = eng;}
public int getMat() {return mat;}
public void setMat(int mat) {this.mat = mat;}
}
DI (DependencyInjection)
객체 안
에서 생성하는 것은 강한 결합도(높은 의존성)를 가짐public class RecordViewImpl implements RecordView{
private RecordImpl record = new RecordImpl();
}
외부에서
객체를 생성하여 객체를 주입public class RecordViewImpl implements RecordView{
private RecordImpl record;
//[생성자 DI(의존성 주입) 방식
public RecordViewImpl(RecordImpl record){
super();
this.record= record;
}
}
public class RecordViewImpl implements RecordView{
//프로퍼티 방식
public void setRecord(RecordImpl record){
this.record = record;
}
}
IoC (Inversion Of Control)
개발자의 코드를 호출
BeanFactory
라고 함<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="record" class="di.RecordImpl"></bean>
<bean id="rvi" class="di.RecordViewImpl">
<constructor-arg ref="record"></constructor-arg>
</bean>
</beans>
<bean id="record" class="di.RecordImpl"></bean>
<bean id="rvi" class="di.RecordViewImpl">
<constructor-arg ref="record"></constructor-arg>
</bean>
<bean id="rvi" class="di.RecordViewImpl">
<constructor-arg>
<ref bean="record"/>
</constructor-arg>
</bean>
<bean id="rvi" class="di.RecordViewImpl">
<property name="record" ref="record"></property>
</bean>
class Test {
public static void main(String[] args) {
String resourceLocations = "applicationContext.xml";
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(resourceLocations);
RecordViewImpl rvi = (RecordViewImpl) ctx.getBean("rvi");
}
}
applicationContext.xml
xml 파일로 GenericXmlApplicationContext
객체 생성 하여 스프링 컨테이너 안에 Bean 객체 생성import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
RecordImpl record = new RecordImpl();
@Bean
public RecordImpl record() {
return new RecordImpl();
}
@Bean(name = "rvi")
public RecordViewImpl getRecordViewImpl() {
RecordViewImpl rvi = new RecordViewImpl();
rvi.setRecord(record()); // setter 프로퍼티를 통해서 DI
return rvi;
}
}
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
RecordViewImpl rvi = ctx.getBean("rvi", RecordViewImpl.class);
Config.java
에서 어노테이션 설정AnnotationConfigApplicationContext
객체 생성 시 해당 .class파일 설정 하여 IoC컨테이너 생성 → 빈 객체 생성 주입하는 코딩<context:component-scan base-package="스캔할 패키지명"></context:component-scan>
context:component-scan
태그로 스캔할 패키지명 설정context:annotation-config
태그는 자동 연결하는 태그이나 위 태그 하나로 처리 가능@Component
public class RecordImpl implements Record{
//
}
@Component
public class RecordViewImpl implements RecordView{
@Autowired
private RecordImpl record = null;
public RecordViewImpl(RecordImpl record){this.record= record;}
public void setRecord(RecordImpl record){this.record = record;}
}
String resourceLocations = "applicationContext.xml";
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(resourceLocations);
RecordViewImpl record = (RecordViewImpl)ctx.getBean("recordViewImpl");
AOP (Apect Oriented Programming)
Aspect | 핵심기능을 제외하고 공통의 기능 |
Advice | Aspect이 실행되는 위치를 구현한 것 |
Jointpoint | Advice가 Target에 적용되는 시점 |
Point cut | Joint point의 상세 |
Target | Aspect가 적용되는 대상 |