码迷,mamicode.com
首页 > 编程语言 > 详细

java设计模型 解析工厂模式、proxy-agent模式、templete模式

时间:2019-05-04 13:31:03      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:out   tag   mat   参考   单例类   实现类   package   array   静态方法   

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) && "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.

4、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

java设计模型 解析工厂模式、proxy-agent模式、templete模式

标签:out   tag   mat   参考   单例类   实现类   package   array   静态方法   

原文地址:https://www.cnblogs.com/lijianxuan/p/10808215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!