标签:优势 rod factory 实例 interface factor 耦合 专注 工厂
设计原则:SOLID 可以说,五个设计原则各司其职。
单一职责原则(S):用于类的设计
开闭原则(O):总的指导思想
里氏替换原则(L):用于指导类继承的设计
接口隔离原则(I):用于指导接口的设计
依赖翻转原则(D):用于指导如何抽象
类之间的关系:
继承=实现>组合>聚合>关联>依赖
public interface product { public void createProduct(); } public class conCreteProduct implements product { @Override public void createProduct() { // TODO Auto-generated method stub System.out.println("生产产品"); } } public class user { public static void main(String[] args) { // TODO Auto-generated method stub product prod1 = new conCreteProduct(); prod1.createProduct(); } }
public interface product { public void createProduct(); } public class conCreteProduct implements product { @Override public void createProduct() { // TODO Auto-generated method stub System.out.println("生产产品"); } } public class factory { public void createProduct() { product prod1 = new conCreteProduct(); prod1.createProduct(); } } public class user { public static void main(String[] args) { // TODO Auto-generated method stub factory fact1 = new factory(); fact1.createProduct(); } }
public interface product { public void makeProduct(); } public class concreteProduct implements product { @Override public void makeProduct() { // TODO Auto-generated method stub System.out.println("中国制造"); } } public class factory { public static product makeProduct() { Properties pro = new Properties(); InputStream in = null; try { in = factory.class.getResourceAsStream("factory.properties"); pro.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } product prod = null; try { prod = (product) Class.forName(pro.getProperty("product1")).newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return prod; } } public class user { public static void main(String[] args) { // TODO Auto-generated method stub product prod = factory.makeProduct(); prod.makeProduct(); } } 配置文件factory.properties product1=No0.concreteProduct
标签:优势 rod factory 实例 interface factor 耦合 专注 工厂
原文地址:http://www.cnblogs.com/shal9999/p/6920980.html