1、Factory Design pattern
工厂设计模式的优点
(1)工厂设计模式提供了接口而不是实现的代码方法。
(2)工厂模式从客户端代码中删除实际实现类的实例化。工厂模式使我们的代码更健壮,耦合更少,易于扩展。例如,我们可以轻松更改PC类实现,因为客户端程序不知道这一点。
(3)工厂模式通过继承提供实现和客户端类之间的抽象。
JDK中工厂设计模式实列
java.util.Calendar,ResourceBundle和NumberFormat getInstance()
方法使用Factory模式。
valueOf()
包装类中的方法,如Boolean,Integer等。
代码示例:https://github.com/journaldev/journaldev/tree/master/java-design-patterns/Factory-Design-Pattern
2、Prototype example
Employees.java
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.java
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]
3、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) && "[email protected]$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.
4、Singleton模式
(1)Singleton模式限制了类的实例化,并确保java虚拟机中只存在该类的一个实例。
(2)单例类必须提供一个全局访问点来获取类的实例。
(3)单例模式用于日志记录 ,驱动程序对象,缓存和线程池 。
(4)Singleton设计模式也用于其他设计模式,如Abstract Factory , Builder , Prototype , Facade等。
Singleton设计模式也用于核心java类,例如java.lang.Runtime
, java.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