package com.test;
interface Bean{
void method1();
void method2();
}
interface BeanFactory{
Bean getBean();
}
class ImplBean implements Bean{
@Override
public void method1() {
System.out.println("implement method1");
}
@Override
public void method2() {
System.out.println("implement method2");
}
}
/**
*多态向上转型
*/
class ImplBeanFactory implements BeanFactory{
@Override
public Bean getBean() {
return new ImplBean();
}
}
public class Factories{
public static void BeanCousumer(BeanFactory factory){
Bean bean=factory.getBean();
bean.method1();;
bean.method2();
}
public static void main(String[] args) {
BeanCousumer(new ImplBeanFactory());
}
}
简单的工厂模式实现
于 2019-03-28 10:44:22 首次发布