通過依賴注入來從不同的部門獲取數據
[java]
package com
public interface DataManagement {
//使用該方法來獲取數據
public void getData();
}
[java]
package com
public class DepartmentAData implements DataManagement {
@Override
public void getData() {
//使用該方法來從部門A獲取數據
System
}
}
[java]
package com
public class DepartmentBData implements DataManagement {
@Override
public void getData() {
//使用該方法來從部門B獲取數據
System
}
}
[java]
package com
public class DepartmentCData implements DataManagement {
@Override
public void getData() {
//使用該方法來從部門C獲取數據
System
}
}
[java]
package com
public class Business {
private DataManagement dm;
public void setDm(DataManagement dm) {
this
}
//根據注入的獲取數據
public void getData() {
dm
}
}
[java]
package com
public class Test {
public static void main(String[] args) {
Business business = new Business();
// business
// business
business
business
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25886.html