package com.debuglife.codelabs;

import java.util.ResourceBundle;

/**
 * 1. read the resource file to get the string of dynamically generated class,
 * and create a new instance of class instead of using new XXX() method
 * 
 * 2. design the arch for java framework
 * @author roger
 *
 */
public class TestPolymorphism {
    private static String resourceBundleBaseName = "";
    private static final String TEST2 = "Test2";
    
    
    public static void main(String[] args) throws Exception{
        TestPolymorphism tp = new TestPolymorphism();
        tp.invoke();
    }
    
    public TestPolymorphism() {
        resourceBundleBaseName = this.getClass().getPackage().getName() + ".Bundle";
    }
    
    private void invoke() throws Exception{
        String className = ResourceBundle.getBundle(resourceBundleBaseName).getString(TEST2);
        Test1 testDemo = (Test1)((ClassLoader.getSystemClassLoader().loadClass(className)).newInstance());
        testDemo.start();
        System.out.println("********************************");
        testDemo.start1();
    }
}

abstract class Test1{
    public void start(){
        // 1. some action in the current class
        System.out.println("it is executing in the Test1...");
        
        // 2. call init(), which will be implemented by sub-class
        init();
        
        // 3. some other action in the current class
        System.out.println("it reached to the end in the Test1...");
    }
    
    public void start1(){
        // 1. some action in the current class
        beforeService();
        
        // 2. call init(), which will be implemented by sub-class
        init();
        
        // 3. some other action in the current class
        afterService();
    }
    
    public void beforeService(){
        beforeAction();
    }
    
    public void afterService(){
        afterAction();
    }
    
    public abstract void beforeAction();
    
    public abstract void afterAction();
    
    public abstract void init();
}

class Test2 extends Test1{
    @Override
    public void init() {
        System.out.println("it is executing int the init() of class Test2...");
    }

    @Override
    public void beforeAction() {
        System.out.println("it is executing in the beforeAction() of class Test2...");
    }

    @Override
    public void afterAction() {
        System.out.println("it is executing in the afterAction() of class Test2...");
    }
}
@@ -441,22 +441,97 @@
 %0D%0A    %0D%0A
+    private static int i = 1;%0D%0A    private static int j = 2;;%0D%0A    %0D%0A    %0D%0A
     %0D%0A
-
     publ
@@ -647,16 +647,18 @@
         
+//
 tp.invok
@@ -659,24 +659,166 @@
 .invoke();%0D%0A
+        %0D%0A        Calculator c1 = new C1();%0D%0A        Calculator c2 = new C2();%0D%0A        tp.cal(c1);%0D%0A        tp.cal(c2);%0D%0A        %0D%0A        %0D%0A
     %7D%0D%0A    %0D
@@ -1198,32 +1198,34 @@
 ce());%0D%0A        
+//
 testDemo.start()
@@ -1288,24 +1288,24 @@
 *******%22);%0D%0A
-
         test
@@ -1327,16 +1327,91 @@
 %0A    %7D%0D%0A
+    %0D%0A    private void cal(Calculator c)%7B%0D%0A    %09c.calculate(i, j);%0D%0A    %7D%0D%0A
 %7D%0D%0A%0D%0Aabs
@@ -2818,20 +2818,338 @@
 est2...%22);%0D%0A    %7D%0D%0A%7D
+%0D%0A%0D%0A%0D%0Ainterface Calculator%7B%0D%0A%09void calculate(int i, int j);%0D%0A%7D%0D%0A%0D%0Aclass C1 implements Calculator%7B%0D%0A%0D%0A%09@Override%0D%0A%09public void calculate(int i, int j) %7B%0D%0A%09%09System.out.println(i + j);%0D%0A%09%7D%0D%0A%09%0D%0A%7D%0D%0A%0D%0Aclass C2 implements Calculator%7B%0D%0A%0D%0A%09@Override%0D%0A%09public void calculate(int i, int j) %7B%0D%0A%09%09System.out.println(i*j);%0D%0A%09%7D%0D%0A%09%0D%0A%7D

add a interface testing demo