标签:sys res ring ace 基础 sim rri 需要 arp
在简单工厂模式的基础上对工厂进行抽象化,不同的产品对应不同的具体工厂。
克服了简单工厂模式违背开闭原则。但在添加新产品时需要增加产品以及对应的工厂·增加负复杂度,给系统带来额外开销.
抽象产品类
1 public interface Freshwaterfish { 2 void grow();//鱼在生长 3 void harvest();//收获鱼 4 }
具体产品类
1 public class Grasscrap implements Freshwaterfish { 2 3 @Override 4 public void grow() { 5 System.out.println("草鱼在生长!"); 6 } 7 8 @Override 9 public void harvest() { 10 System.out.println("收获草鱼!"); 11 } 12 13 }
1 public class Carp implements Freshwaterfish { 2 3 @Override 4 public void grow() { 5 System.out.println("鲤鱼在生长!"); 6 } 7 8 @Override 9 public void harvest() { 10 System.out.println("收获鲤鱼!"); 11 } 12 13 }
抽象工厂类
1 public interface Fishfactory { 2 public Freshwaterfish producefish(); 3 }
具体工厂类
1 public class GrasscrapFactory implements Fishfactory { 2 3 @Override 4 public Freshwaterfish producefish() { 5 return new Grasscrap(); 6 } 7 8 }
1 public class CarpFactory implements Fishfactory { 2 3 @Override 4 public Freshwaterfish producefish() { 5 return new Carp(); 6 } 7 8 }
客户测试类
1 public class SimpleFactory { 2 3 public static void main(String[] args) { 4 Fishfactory grassfactory = new GrasscrapFactory(); 5 Freshwaterfish grasscrap = grassfactory.producefish(); 6 grasscrap.grow(); 7 grasscrap.harvest(); 8 } 9 }
标签:sys res ring ace 基础 sim rri 需要 arp
原文地址:http://www.cnblogs.com/the-wang/p/7267836.html