java設計模型 解析工廠模式、proxy-agent模式、templete模式

一、Factory Design patternhtml

工廠設計模式的優勢

(1)工廠設計模式提供了接口而不是實現的代碼方法。java

(2)工廠模式從客戶端代碼中刪除實際實現類的實例化。工廠模式使咱們的代碼更健壯,耦合更少,易於擴展。例如,咱們能夠輕鬆更改PC類實現,由於客戶端程序不知道這一點。git

(3)工廠模式經過繼承提供實現和客戶端類之間的抽象。github

JDK中工廠設計模式實列

java.util.Calendar,ResourceBundle和NumberFormat getInstance()方法使用Factory模式。設計模式

valueOf() 包裝類中的方法,如Boolean,Integer等。緩存

代碼示例:https://github.com/journaldev/journaldev/tree/master/java-design-patterns/Factory-Design-Patternide

二、Prototype example函數

Employees.javapost

複製代碼
 1 package com.journaldev.design.prototype;  2  3 import java.util.ArrayList;  4 import java.util.List;  5  6 public class Employees implements Cloneable{  7  8 private List<String> empList;  9 10 public Employees(){ 11 empList = new ArrayList<String>(); 12  } 13 14 public Employees(List<String> list){ 15 this.empList=list; 16  } 17 public void loadData(){ 18 //read all employees from database and put into the list 19 empList.add("Pankaj"); 20 empList.add("Raj"); 21 empList.add("David"); 22 empList.add("Lisa"); 23  } 24 25 public List<String> getEmpList() { 26 return empList; 27  } 28 29  @Override 30 public Object clone() throws CloneNotSupportedException{ 31 List<String> temp = new ArrayList<String>(); 32 for(String s : this.getEmpList()){ 33  temp.add(s); 34  } 35 return new Employees(temp); 36  } 37 38 }
複製代碼

PrototypePatternTest.javaui

複製代碼
 1 package com.journaldev.design.test;  2  3 import java.util.List;  4  5 import com.journaldev.design.prototype.Employees;  6  7 public class PrototypePatternTest {  8  9 public static void main(String[] args) throws CloneNotSupportedException { 10 Employees emps = new Employees(); 11  emps.loadData(); 12 13 //Use the clone method to get the Employee object 14 Employees empsNew = (Employees) emps.clone(); 15 Employees empsNew1 = (Employees) emps.clone(); 16 List<String> list = empsNew.getEmpList(); 17 list.add("John"); 18 List<String> list1 = empsNew1.getEmpList(); 19 list1.remove("Pankaj"); 20 21 System.out.println("emps List: "+emps.getEmpList()); 22 System.out.println("empsNew List: "+list); 23 System.out.println("empsNew1 List: "+list1); 24  } 25 26 }
複製代碼

結果:

emps List: [Pankaj, Raj, David, Lisa] empsNew List: [Pankaj, Raj, David, Lisa, John] empsNew1 List: [Raj, David, Lisa]

三、Proxy模式

Proxy Design Pattern – Main Class

CommandExecutor.java

複製代碼
1 package com.journaldev.design.proxy; 2 3 public interface CommandExecutor { 4 5 public void runCommand(String cmd) throws Exception; 6 }
複製代碼

CommandExecutorImpl.java

複製代碼
 1 package com.journaldev.design.proxy;  2  3 import java.io.IOException;  4  5 public class CommandExecutorImpl implements CommandExecutor {  6  7  @Override  8 public void runCommand(String cmd) throws IOException {  9 //some heavy implementation 10  Runtime.getRuntime().exec(cmd); 11 System.out.println("'" + cmd + "' command executed."); 12  } 13 14 }
複製代碼

Proxy Design Pattern – Proxy Class

CommandExecutorProxy.java

複製代碼
 1 package com.journaldev.design.proxy;  2  3 public class CommandExecutorProxy implements CommandExecutor {  4  5 private boolean isAdmin;  6 private CommandExecutor executor;  7  8 public CommandExecutorProxy(String user, String pwd){  9 if("Pankaj".equals(user) && "J@urnalD$v".equals(pwd)) isAdmin=true; 10 executor = new CommandExecutorImpl(); 11  } 12 13  @Override 14 public void runCommand(String cmd) throws Exception { 15 if(isAdmin){ 16  executor.runCommand(cmd); 17 }else{ 18 if(cmd.trim().startsWith("rm")){ 19 throw new Exception("rm command is not allowed for non-admin users."); 20 }else{ 21  executor.runCommand(cmd); 22  } 23  } 24  } 25 26 }
複製代碼

ProxyPatternTest.java

複製代碼
 1 package com.journaldev.design.test;  2  3 import com.journaldev.design.proxy.CommandExecutor;  4 import com.journaldev.design.proxy.CommandExecutorProxy;  5  6 public class ProxyPatternTest {  7  8 public static void main(String[] args){  9 CommandExecutor executor = new CommandExecutorProxy("Pankaj", "wrong_pwd"); 10 try { 11 executor.runCommand("ls -ltr"); 12 executor.runCommand(" rm -rf abc.pdf"); 13 } catch (Exception e) { 14 System.out.println("Exception Message::"+e.getMessage()); 15  } 16 17  } 18 19 }
複製代碼

output

'ls -ltr' command executed. Exception Message::rm command is not allowed for non-admin users.

四、Singleton模式

(1)Singleton模式限制了類的實例化,並確保java虛擬機中只存在該類的一個實例。

(2)單例類必須提供一個全局訪問點來獲取類的實例。

(3)單例模式用於日誌記錄 ,驅動程序對象,緩存和線程池 。

(4)Singleton設計模式也用於其餘設計模式,如Abstract Factory , Builder , Prototype , Facade等。

Singleton設計模式也用於核心java類,例如java.lang.Runtimejava.awt.Desktop

Java Singleton模式

爲了實現Singleton模式,咱們有不一樣的方法,但它們都有如下常見概念。

(1)私有構造函數,用於限制其餘類的實例化。

(2)同一類的私有靜態變量,它是該類的惟一實例。

(3)返回類實例的公共靜態方法,這是外部世界獲取單例類實例的全局訪問點。

 

參考連接:https://www.journaldev.com/1827/java-design-patterns-example-tutorial

     https://www.cnblogs.com/yuanchao-blog/p/10779576.html

相關文章
相關標籤/搜索