Java - 팩토리 메소드
exam 은 newlecexam 이 책임지게 하자. examconsole에서 추상메스드를 만들어서 상속하고있는 클래스를 만들것이다.
- 팩토리 메서드 : 객체를 생성하는 부분을 자식에게 위임해서 자식이 객체생성하는 부분을 책임질수록 있도록하는 메서드
ExamConsole 에 protected abstract Exam makeExam(); 추상메서드를 구현했다.
NewlecExamConsole클래스를 새로만들어서 추상메서드를 추가한다.
@Override
protected Exam makeExam() {
return new NewlecExam();
}
이렇게만하고 출력을 해보려고 메인에
ExamConsole console = new NewlecExamConsole();
console.input();
console.print();
출력을해보면 컴퓨터 과목이 포함되지않은채 출력이된다. 컴퓨터과목을 추가할 방법을 찾아보자. print() 와 input()을 최대한 재사용해야한다.
위와 같이 밑부분을 추가할것이다.
public void onInput() {
컴퓨터 입력
}
- 이벤트 메소드 :어떤 사건에 기반해서 실행되는 메소드
Main
public class Program {
public static void main(String[] args) {
ExamConsole console = new NewlecExamConsole();
console.input();
console.print();
}
}
NewlecExamConsole
public class NewlecExamConsole extends ExamConsole {
@Override
protected Exam makeExam() {
return new NewlecExam();
}
@Override
protected void onPrint(Exam exam) {
NewlecExam newlecExam = (NewlecExam)exam;
int com = newlecExam.getCom();
System.out.printf("컴퓨터 :%d\n", com);
}
@Override
protected void onInput(Exam exam) {
NewlecExam newlecExam = (NewlecExam)exam;
Scanner scan = new Scanner(System.in);
int com;
do {
System.out.printf("컴퓨터 :");
com = scan.nextInt();
if(com < 0 || 100 < com)
{
System.out.println("컴퓨터성적은 0~100까지의 범위만 입력이 가능합니다.");
}
}while(com<0 || 100 < com);
newlecExam.setCom(com);
}
}
ExamConsole
public abstract class ExamConsole {
private ExamList list = new ExamList();
public void print() {
print(list.size());
}
public void print(int size) {
System.out.println("┌──────────────────┐");
System.out.println("│ 성적 출력 │");
System.out.println("└──────────────────┘");
System.out.println();
// get ========================================================
for(int i= 0; i<size; i++ ) {
Exam exam = list.get(i);//;
int kor = exam.getKor();
int eng = exam.getEng();
int math = exam.getMath();
int total = exam.total();//kor+eng+math;
float avg= exam.avg();//total/3.0f;
System.out.printf("국어 :%d\n", kor);
System.out.printf("영어 :%d\n", eng);
System.out.printf("수학 :%d\n", math);
onPrint(exam);
System.out.printf("총점 : %3d\n", total);
System.out.printf("평균 : %6.2f\n", avg);
System.out.println("────────────────────────");
}
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("┌──────────────────┐");
System.out.println("│ 성적 입력 │");
System.out.println("└──────────────────┘");
System.out.println();
int kor, eng, math;
do {
System.out.printf("국어 :");
kor = scan.nextInt();
if(kor < 0 || 100 < kor)
{
System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
}
}while(kor<0 || 100 < kor);
do {
System.out.printf("영어 :");
eng = scan.nextInt();
if(eng < 0 || 100 < eng)
{
System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
}
}while(eng<0 || 100 < eng);
do {
System.out.printf("수학 :");
math = scan.nextInt();
if(math < 0 || 100 < math)
{
System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
}
}while(math<0 || 100 < math);
// Exam exam = new Exam(kor,eng,math);
Exam exam = makeExam();
exam.setKor(kor);
exam.setEng(eng);
exam.setMath(math);
onInput(exam);
list.add(exam);
System.out.println("────────────────────────");
}
protected abstract void onPrint(Exam exam);
protected abstract void onInput(Exam exam);
protected abstract Exam makeExam();
}
출력
어렵다 특히 형변환 하는 곳에서 띠용! 했음 다시 한번 더들을것
출처:https://youtu.be/WEm6Bhc3vOc
댓글남기기