告別面向過程: 從彙編到C,因爲機器的執行都是經過有順序的,咱們的編程都是面向過程,能夠極大的提升系統資源的利用率。當愈來愈多的項目須要快速實現,硬件設備愈來愈多的不是咱們考慮的重點。項目愈來愈大,開發人員愈來愈多的時候,咱們須要一種更加工程化的思想來分析項目。java
走向工程化:從項目的可擴展,低耦合、開發週期等方面來思考問題,面向對象編程就是基於這一點提出的一種高度抽象的思惟方式。將項目的各個模塊從分解、求同、抽象,造成以功能模塊爲基點來完成項目工程。封裝、基礎、多態的出現可讓咱們大大下降項目的耦合性、 從而提升穩定行和可擴展性。mysql
結構模式是功能的封裝、基礎、多態的主要使用實現。咱們平時用的最多,也是最容易忽略的一種實現。經常使用的有如下幾種實現。
sql
//舊功能已經存在,須要在原來的功能上添加新功能 //基類 public class Source { public void method1() { System.out.println("this is basic Source"); } } //想添加功能 public interface TargetTable { // 舊方法 // public void method1(); /** * @see 添加新的方法 */ public void method2(); } //適配器 public class Adapter extends Source implements TargetTable { // Source實現了方法1,全部只須要實現方法2 @Override public void method2() { System.out.println("this is other method"); } public static void main(String[] args) { // 這中方法在集合中大量使用,例如ArrayList和LinkList,若是使用List作引用,則有一些方法無法實現 // TargetTable tt = new Adapter(); 若是是接口引用,則須要在接口裏面實現一下舊接口 Adapter tt = new Adapter(); //若是直接用adapter類,直接調用就行 tt.method1(); tt.method2(); } }
//共能還沒實現,須要將功能用在不一樣的地方 //功能 public interface SourceInterf { public void method(); } //實現類 public class Source implements SourceInterf { @Override public void method() { System.out.println("the original method!"); } } // 裝飾器 public class Decorator implements SourceInterf { /** * @see 接口:須要實現的功能 * @see 基類:實現功能 * @see 裝飾類:接受接口實現類,強化 */ private SourceInterf sourceInterf; public Decorator(SourceInterf sourceInterf) { this.sourceInterf = sourceInterf; } @Override public void method() { System.out.println("before decorator!"); sourceInterf.method(); System.out.println("before decorator!"); } public static void main(String[] args) { SourceInterf source = new Source(); SourceInterf obj = new Decorator(source); obj.method(); } }
//須要在舊的功能上實現新功能 //功能 public interface SourceInterf { public void method(); } //實現類 public class Source implements SourceInterf { @Override public void method() { System.out.println("the original method!"); } } //代理類 public class Proxy implements SourceInterf { /** * @see 接口:須要實現的功能 * @see 基類:實現功能 * @see 裝飾類:實例化接口實現類,強化 */ private SourceInterf sourceInterf; // 直接實例化 public Proxy() { this.sourceInterf = new Source(); } @Override public void method() { System.out.println("before decorator!"); sourceInterf.method(); System.out.println("before decorator!"); } public static void main(String[] args) { SourceInterf obj = new Proxy(); obj.method(); //一、修改原有的方法來適應。這樣違反了「對擴展開放,對修改關閉」的原則。 //二、就是採用一個代理類調用原有的方法,且對產生的結果進行控制。這種方法就是代理模式。 } }
public class ConnectionP2 { /** * @see 享元模式:相似於池技術 */ private String url = "jdbc:mysql//localhost:33016/test"; private String username = "root"; private String password = "root"; private String driverClassName = "com.mysql.jdbc.Driver"; // 空閒池:存放空閒鏈接 private Vector<Connection> freePool; // 空閒個數 private int freeNumber = 100; private int poolSize = 100; private static ConnectionP2 instance = null; Connection conn = null; private ConnectionP2() { freePool = new Vector<Connection>(poolSize); for (int i = 0; i < poolSize; i++) { try { Class.forName(driverClassName); conn = DriverManager.getConnection(url, username, password); freePool.add(conn); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // 空閒數+1 public void release() { synchronized (ConnectionP2.class) { freeNumber++; } } // 返回一個、空閒數-1 public Connection getConnection() { synchronized (ConnectionP2.class) { if (!freePool.isEmpty()) { conn = freePool.get(freeNumber); freeNumber--; } return conn; } } }