标签:
package com.godcin.test.factorymethod; public interface Product { public void operation(); }
ProductA.java
package com.godcin.test.factorymethod; public class ProductA implements Product { @Override public void operation() { System.out.println("产品A可以提供服务A"); } }
ProductB.java
package com.godcin.test.factorymethod; public class ProductB implements Product { @Override public void operation() { System.out.println("产品B可以提供服务B"); } }
Factory.java
package com.godcin.test.factorymethod; public interface Factory { public Product getProduct(); }
FactoryA.java
package com.godcin.test.factorymethod; public class FactoryA implements Factory { @Override public Product getProduct() { // TODO Auto-generated method stub return new ProductA(); } }
FactoryB.java
package com.godcin.test.factorymethod; public class FactoryB implements Factory { @Override public Product getProduct() { // TODO Auto-generated method stub return new ProductB(); } }
FactoryMethodTest.java
package com.godcin.test.factorymethod; public class FactoryMethodTest { public static void main(String[] args) { // TODO Auto-generated method stub Factory factoryA = new FactoryA(); Product productA = factoryA.getProduct(); productA.operation();// 输入结果|产品A可以提供服务A Factory factoryB = new FactoryB(); Product productB = factoryB.getProduct(); productB.operation();// 输入结果|产品B可以提供服务B } }
标签:
原文地址:http://www.cnblogs.com/themis/p/5396811.html