느슨한결합에대한이해 + 스프링 DI할용
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
package spring.di;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import spring.di.entity.Exam;
|
||||
import spring.di.entity.NewlecExam;
|
||||
import spring.di.ui.ExamConsole;
|
||||
import spring.di.ui.GridExamConsole;
|
||||
import spring.di.ui.InlineExamConsole;
|
||||
|
||||
public class Program {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Exam exam = new NewlecExam();
|
||||
ExamConsole console = new InlineExamConsole(exam); // DI
|
||||
//ExamConsole console = new InlineExamConsole(exam); // DI
|
||||
//ExamConsole console = new GridExamConsole(exam);
|
||||
|
||||
/*
|
||||
@@ -21,6 +22,19 @@ public class Program {
|
||||
같은 메서드를 호출하게되는 위 코드의 수정을 대신해줄 수 있도록 하는 기능이 Spring의 DI다.
|
||||
|
||||
*/
|
||||
|
||||
// 해당 클래스 사용하기위해서
|
||||
// Maven 프로젝트로 바꿔서 빌드툴을 사용하도록 하였고
|
||||
// Maven Repository에가서 Spring context를 dependency해주었음.
|
||||
ApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("spring/di/setting.xml");
|
||||
|
||||
|
||||
//ExamConsole console = (ExamConsole)context.getBean("console");
|
||||
ExamConsole console = context.getBean(ExamConsole.class);
|
||||
console.setExam(exam);
|
||||
|
||||
|
||||
console.print();
|
||||
|
||||
}
|
||||
|
||||
22
spring/di/Student.java
Normal file
22
spring/di/Student.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package spring.di;
|
||||
|
||||
import spring.di.entity.CableMouse;
|
||||
import spring.di.entity.Mouse;
|
||||
import spring.di.ui.Computer;
|
||||
|
||||
public class Student {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Computer computer = new Computer();
|
||||
|
||||
Mouse mouse = new CableMouse(); // 유선마우스
|
||||
//Mouse mouse = new WireLessMouse(); // 무선마우스
|
||||
|
||||
// 작업을 위한 준비. 마우스 연결
|
||||
computer.connectMouse(mouse);
|
||||
// 연결확인
|
||||
computer.mouseCheck();
|
||||
|
||||
}
|
||||
}
|
||||
20
spring/di/entity/CableMouse.java
Normal file
20
spring/di/entity/CableMouse.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package spring.di.entity;
|
||||
|
||||
public class CableMouse implements Mouse{
|
||||
@Override
|
||||
public void clickR() {
|
||||
System.out.println("[ 유선 R ]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clickL() {
|
||||
System.out.println("[ 유선 L ]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseCheck() {
|
||||
System.out.println(" 유선마이스 연결 확인 ");
|
||||
clickR();
|
||||
clickL();
|
||||
}
|
||||
}
|
||||
10
spring/di/entity/Mouse.java
Normal file
10
spring/di/entity/Mouse.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package spring.di.entity;
|
||||
|
||||
public interface Mouse {
|
||||
|
||||
void clickR();
|
||||
void clickL();
|
||||
|
||||
void mouseCheck();
|
||||
|
||||
}
|
||||
22
spring/di/entity/WireLessMouse.java
Normal file
22
spring/di/entity/WireLessMouse.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package spring.di.entity;
|
||||
|
||||
public class WireLessMouse implements Mouse {
|
||||
|
||||
@Override
|
||||
public void clickR() {
|
||||
System.out.println("[ 무선 R ]");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clickL() {
|
||||
System.out.println("[ 무선 L ]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseCheck() {
|
||||
System.out.println(" 무선마이스 연결 확인 ");
|
||||
clickR();
|
||||
clickL();
|
||||
}
|
||||
}
|
||||
16
spring/di/setting.xml
Normal file
16
spring/di/setting.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?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">
|
||||
|
||||
<!-- Exam exam = new NewlecExam(); -->
|
||||
<bean id ="exam" class="spring.di.entity.NewlecExam" />
|
||||
|
||||
<!-- ExamConsole console = new GridExamConsole(); -->
|
||||
<bean id ="console" class ="spring.di.ui.InlineExamConsole" >
|
||||
<!-- console.setExam(exam); -->
|
||||
<property name ="exam" ref="exam" />
|
||||
</bean>
|
||||
|
||||
|
||||
</beans>
|
||||
14
spring/di/ui/Computer.java
Normal file
14
spring/di/ui/Computer.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package spring.di.ui;
|
||||
|
||||
import spring.di.entity.Mouse;
|
||||
|
||||
public class Computer {
|
||||
private Mouse mouse ;
|
||||
|
||||
public void connectMouse(Mouse mouse) {
|
||||
this.mouse=mouse;
|
||||
};
|
||||
public void mouseCheck() {
|
||||
mouse.mouseCheck();
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package spring.di.ui;
|
||||
|
||||
import spring.di.entity.Exam;
|
||||
|
||||
public interface ExamConsole {
|
||||
void print();
|
||||
void setExam(Exam exam);
|
||||
}
|
||||
|
||||
@@ -6,9 +6,13 @@ public class GridExamConsole implements ExamConsole {
|
||||
|
||||
private Exam exam;
|
||||
|
||||
public GridExamConsole() {
|
||||
}
|
||||
|
||||
public GridExamConsole(Exam exam) {
|
||||
this.exam = exam;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print() {
|
||||
System.out.println("┌──────────┬──────────┐");
|
||||
@@ -18,4 +22,11 @@ public class GridExamConsole implements ExamConsole {
|
||||
System.out.println("└──────────┴──────────┘");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExam(Exam exam) {
|
||||
this.exam = exam;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,9 @@ public class InlineExamConsole implements ExamConsole {
|
||||
|
||||
private Exam exam;
|
||||
|
||||
public InlineExamConsole() {
|
||||
}
|
||||
|
||||
public InlineExamConsole(Exam exam) {
|
||||
this.exam = exam;
|
||||
}
|
||||
@@ -15,4 +18,9 @@ public class InlineExamConsole implements ExamConsole {
|
||||
System.out.printf("total is %d, avg is %f\n", exam.total(), exam.avg());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExam(Exam exam) {
|
||||
this.exam=exam;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user