ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 10. oop6 - (1) JobDemo (추상 메서드)
    java/코드 리뷰 2020. 4. 2. 20:20
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    package oop6;
     
    public abstract class AbstractJobTemplate {
        
        String host;
        int port;
        String userid;
        String password;
        
        void setup() {
            System.out.println("### 전처리 작업 시작 ###");
            System.out.println("데이터베이스 서버에 연결 요청");
            System.out.println("데이터베이스 연결 완료");
            System.out.println("### 전처리 작업 완료 ###");
        }
        
        void destroy() {
            System.out.println("### 후처리 작업 시작 ###");
            System.out.println("데이터베이스 서버에 연결해제 요청");
            System.out.println("데이터베이스 연결해제 완료");
            System.out.println("### 후처리 작업 완료 ###");
        }
        
        // 추상 메서드
        // 자식 클래스에 실제 구현 위임을 위해 메서드를 설계(정의, 선언)
        abstract void work();
        
        // 작업을 처리하기 위해서 순서에 맞게 메서드를 실행하는 메서드
        void run() {
            setup();
            work();
            destroy();
        }
     
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    package oop6;
     
    public class InsertJob extends AbstractJobTemplate{
        
        @Override
        void work() {
            System.out.println("데이터베이스에 새로운 정보를 추가합니다.");
        }
        
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package oop6;
     
    public class RetrieveJob extends AbstractJobTemplate {
        
        @Override
        void work() {
            System.out.println("데이터베이스에서 정보를 검색합니다.");
        }
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package oop6;
     
    public class JobDemo {
        public static void main(String[] args) {
            
            InsertJob job1 = new InsertJob();
            job1.run();
            
            RetrieveJob job2 = new RetrieveJob();
            job2.run();
            
        }
    }
     
     
Designed by Tistory.