编写一个接口
/** * @author wangxm * @create 2019-04-06 下午 10:14 */ public interface Api { void test1(String mgs); }
接口的实现类
/** * @author wangxm * @create 2019-04-06 下午 10:15 */ public class ApiImpl implements Api { public void test1(String mgs) { System.out.println(mgs); } }
工厂类
/** * @author wangxm * @create 2019-04-06 下午 10:16 */ public class Function { private Function() {} public static Api createApi() { //可以写一些别的生成逻辑 return new ApiImpl(); } }
客户端调用
/** * @author wangxm * @create 2019-04-06 下午 10:17 */ public class Client { public static void main(String[] args) { Api api = Function.createApi(); api.test1("hello"); } }