设计模式-工厂方法模式

类图:

1 public interface IFactory {
2     public Operation createOperation();
3 }
1 public class AddOperationFactory implements IFactory {
2 
3     @Override
4     public Operation createOperation() {
5         return new AddOperation();
6     }
7 }
 1 public class Client {
 2 
 3     public static void main(String[] args)
 4     {
 5         IFactory addFactory = new AddOperationFactory();
 6         Operation add = addFactory.createOperation();
 7         add.setNumberA(1);
 8         add.setNumberB(2);
 9         System.out.println(add.getResult());
10     }
11 }

 

图片引用自:《大话设计模式》-程杰

posted @ 2015-11-25 21:08  gatsbydhn  阅读(109)  评论(0编辑  收藏  举报