@FunctionalInterface
public interface App {
void say();
}
public interface ClsApp extends App {
@Override
void say();
}
public interface UwpApp extends App {
@Override
void say();
}
public interface WebApp extends App {
}
public class AppImpl implements App {
@Override
public void say() {
System.out.println("应用产品");
}
}
public class ClsAppImpl extends AppImpl implements App,ClsApp {
@Override
public void say() {
System.out.println("控制台应用");
}
}
public class UwpAppImpl extends AppImpl implements App,UwpApp{
@Override
public void say() {
System.out.println("uwp应用");
}
}
public class WebAppImpl extends AppImpl implements App,WebApp {
@Override
public void say() {
System.out.println("网站应用");
}
}
public abstract class AppFactory {
protected abstract <T extends App> T createApp(Class<T> c);
}
public class ClsAppFactory extends AppFactory{
@Override
public <T extends App> T createApp(Class<T> c) {
ClsApp app = null;
try{
app = (ClsApp)Class.forName(c.getName()).newInstance();
} catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
e.printStackTrace();
}
return (T) app;
}
}
public class UwpAppFactory extends AppFactory {
@Override
public <T extends App> T createApp(Class<T> c) {
UwpApp app = null;
try {
app = (UwpApp)Class.forName(c.getName()).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
return (T)app;
}
}
public class WebAppFactory extends AppFactory {
@Override
public <T extends App> T createApp(Class<T> c) {
WebApp app = null;
try {
app = (WebApp)Class.forName(c.getName()).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
return (T)app;
}
}
public class Main {
public static void main(String[] args) {
ClsAppFactory clsAppFactory = new ClsAppFactory();
ClsApp clsApp = clsAppFactory.createApp(ClsAppImpl.class);
clsApp.say();
WebAppFactory webAppFactory = new WebAppFactory();
WebApp webApp = webAppFactory.createApp(WebAppImpl.class);
webApp.say();
UwpAppFactory uwpAppFactory = new UwpAppFactory();
UwpApp uwpApp = uwpAppFactory.createApp(UwpAppImpl.class);
uwpApp.say();
}
}