package com.eyugame.modle; /** * 抽象工厂模式 * * @author JYC506 * */ public class MyFactory implements IAbstractFactory { @Override public IProduct1 createIProduct1() { return new Product1(); } @Override public IProduct2 createIProduct2() { return new Product2(); } } /** * 抽象工厂接口 * * @author JYC506 * */ interface IAbstractFactory { IProduct1 createIProduct1(); IProduct2 createIProduct2(); } /** * 产品1 */ interface IProduct1 { void show(); } interface IProduct2 { void show(); } class Product1 implements IProduct1 { @Override public void show() { System.out.println("我是产品1"); } } /** * 产品2 */ class Product2 implements IProduct2 { @Override public void show() { System.out.println("我是产品2"); } } class TestFactory { public static void main(String[] args) { IAbstractFactory myFactory = new MyFactory(); IProduct1 product1 = myFactory.createIProduct1(); IProduct2 product2 = myFactory.createIProduct2(); product1.show(); product2.show(); } }
原文:http://blog.csdn.net/h348592532/article/details/45251649