Java设计模式(一):工厂方法模式
/*** 工厂接口**/public interface IFactory {IProduct createProduct();}/*** 工厂实现类**/public class Factory implements IFactory {public IProduct createProduct() {return new Product();}}/***产品接口**/public interface IProduct {void showProduct();}/***产品实现类**/public class Product implements IProduct {public void showProduct() {System.out.println("这是产品");}}/*** 调用*/public class Client {public static void main(String[] args) {IFactory factory = new Factory();IProduct product = factory.createProduct();product.showProduct();}}
?4、优点:代码结构清晰,把变化封装起来