package polymorphism;
public interface iTV {
void PowerOn();
void PowerOff();
void VolumeUp();
void VolumeDown();
}
package polymorphism;
public class ssTV implements iTV{
ssTV(){System.out.println("ssTV 호출");}
public void PowerOn() {
System.out.println("SS TV 전원 켜짐");
}
public void PowerOff() {
System.out.println("SS TV 전원 꺼짐");
}
public void VolumeUp() {
System.out.println("SS 볼륨 올림");
}
public void VolumeDown() {
System.out.println("SS 볼륨 내림");
}
@Override
public String toString() {
return "ssTV Test 완료";
}
}
package polymorphism;
public class lgTV implements iTV{
lgTV(){System.out.println("lgTV 호출");}
public void PowerOn() {
System.out.println("LG TV 전원 켜짐");
}
public void PowerOff() {
System.out.println("LG TV 전원 꺼짐");
}
public void VolumeUp() {
System.out.println("LG 볼륨 올림");
}
public void VolumeDown() {
System.out.println("LG 볼륨 내림");
}
@Override
public String toString() {
return "LGTV Test 완료";
}
}
package polymorphism;
public class TVTest01 {
public static void main(String[] args) {
iTV tv1 = new ssTV();
iTV tv2 = new lgTV();
iTV[] tvArrs = {tv1, tv2};
for(iTV tv: tvArrs) {
tv.PowerOn();
tv.PowerOff();
tv.VolumeUp();
tv.VolumeUp();
}
iTV[] TVs = { new ssTV(), new lgTV() };
for(iTV tv : TVs) { tv.PowerOn(); tv.PowerOff();
tv.VolumeUp(); tv.VolumeDown(); }
}
}
package polymorphism;
public class aptA {
private lgTV tv;
aptA(){tv = new lgTV();}
}
package polymorphism;
public class aptB {
private iTV tv;
public String setTV(iTV tv) {
this.tv = tv;
return tv.toString();
}
}
package polymorphism;
public class TVTest02 {
public static void main(String[] args) {
//생성과 동시에 주입 - DI(Dependency Injection)
aptA a1 = new aptA();
// 생성 후, 별도 주입
aptB b1 = new aptB();
System.out.println(b1.setTV(new ssTV()));
}
}
beanfactory는 인스턴스를 생성 및 설정하며, 많은 수의 bean을 관리하는 컨테이너라 한다.
본래 BeanFactory는 ApplicationContext의 xml을 사용해야 한다.
- 개념적으로 BeanFactory의 개념을 이해하기 위해 java 코드를 구현
- ApplicationContext의 형태로 다시 코드를 구현
예제 1) beanFactory 클래스 구현
package polymorphism;
public class BeanFactory {
public Object getBean(String beanname){
if(beanname.equals("SS")) {
return new ssTV();
}
else if(beanname.equals("LG")) {
return new lgTV();
}
return null;
}
}
package polymorphism;
public class TVTest03 {
public static void main(String[] args) {
BeanFactory factory = new BeanFactory();
// getBean을 써서 객체를 요청
iTV tv = (iTV)factory.getBean(args[0]); // SS or LG
tv.PowerOn(); tv.PowerOff();
tv.VolumeUp(); tv.VolumeDown();
}
}
package polymorphism;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class TVTest04 {
public static void main(String[] args) {
// 1. Spring 컨테이너 구동
AbstractApplicationContext factory =
new GenericXmlApplicationContext("applicationContext.xml");
// 2. Spring 컨테이너로부터 객체 요청(LookUp)
iTV tv = (iTV)factory.getBean("lTv");
tv.PowerOn(); tv.PowerOff();
tv.VolumeUp(); tv.VolumeDown();
tv = (iTV)factory.getBean("sTv");
tv.PowerOn(); tv.PowerOff();
tv.VolumeUp(); tv.VolumeDown();
// 3. Spring 컨테이너 종료
factory.close();
}
}
applicationContext.mxl
<?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.xsd">
<!-- 생성자 정의 -->
<bean id="sTv" class="polymorphism.ssTV"/>
<bean id="lTv" class="polymorphism.lgTV"/>
</beans>