Tuesday, November 15, 2016

Design Pattern - Proxy


Nó là gì?
 - Mẫu Proxy (người đại diện) đại diện cho 1 chức năng của lớp khác. Bạn có thể hiểu, ta thuê 1 luật sư về đại diện cho ta, người luật sư này có thể dùng các quyền của mình một cách gián tiếp để nói cho các người khác một cách chính xác vì luật sư am hiểu về pháp luật hơn mình.

Khi nào dùng nó?

 - Khi bạn muốn bảo vệ quyền truy xuất vào các chức năng (phương thức) của thực thể.
 - Bổ sung trước khi thực hiện phương thức của thực thể.
 - Tạo đối tượng với chức năng được nâng cao theo yêu cầu.

Dùng nó như thế nào?

 Tôi sẽ xây dựng một ví dụ đơn giản như sau:
- Tôi có 1 inteface mô tả chức năng của một class :


  1. public interface ReadFile { 
  2.      public String readFile(); 
  3. }
}

Tiếp theo tạo class User triển khai interface trên:

  1. public class User implements ReadFile{
  2.       private String name; //Tên của người dùng 
  3.       public User(String name) { 
  4.            this.name = name; 
  5.       } @Override public String readFile() { //Phương thức đọc file            return name + "readed"; 
  6.       } 
  7.  }

Và một class Proxy đại diện cho lớp User:
  1. public class UserProxy implements ReadFile{ 
  2.       private ReadFile instance; 
  3.       private String name; 
  4.       public UserProxy (String name){ 
  5.            this.name = name; 
  6.       } 
  7.       @Override 
  8.        public String readFile() { 
  9.             if(name.equals("ok")){ //nêu tên người dùng là ok thì mới thực hiện phương thức của lớp User, không thì báo lỗi!                           if(instance ==null){ 
  10.                     instance = new User(name); 
  11.                   }
  12.                 return instance.readFile(); 
  13.             } 
  14.           return "You can't read file"; 
  15.         } 
  16.  }


Thử chạy chương trình test như sau :
  1. public class Main {
  2.     public static void main(String[] args) { 
  3.         UserProxy user1 = new UserProxy("ok");
  4.         System.out.println(user1.readFile());
  5.         UserProxy user2 = new UserProxy("hello"); 
  6.         System.out.println(user2.readFile()); 
  7.      } 
  8. }


Kết quả :
ok readed
You can't read file

Trên chỉ là một ví dụ nhỏ về proxy, nó còn có rất nhiều cách dùng khác. Hy vọng bạn có thể nắm bắt được các mẫu thiết kế pattern bởi nó thực sự cần thiết đối với một lập trình viên.

7 comments:

  1. Thank you for your sharing articles. I have been finding interesting posts and your articles are what I need. They are really useful and engaging. They are precious pieces of advice. I can branch out with new knowledge. Learn captivating things. Hope you will upload more me.
    e-mail with love online free
    e-mailwithlove mobile apss
    koi Nhat

    ReplyDelete
  2. I am excited to read the next posts. they are precious pieces of advice.Your posts can help me more and more in the future. Once again thank you.
    currency-converter-box-free-online currency-converter-box-online

    ReplyDelete
  3. I have been looking at a couple of your stories and i can state really well done. I will bookmark your online journal
    pop website
    sale pop master app

    ReplyDelete
  4. I am glad to read this post, it’s an impressive one. I am always searching for quality posts and articles and this is what I found here, I hope you will be adding more in future
    app autoketing
    https://apps.shopify.com/shipping-bar-master
    https://bit.ly/2BXG37u

    ReplyDelete
  5. I have a fancy on your posts.They are my cup of tea. They help me when I need to solve problems.Keep your work.They are so great.
    autoketing, https://autoketing.com/project/sale-pop-master
    https://apps.shopify.com/sales-pop-master

    ReplyDelete
  6. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
    Gái gọi Nguyễn Trãi, check hàng, video gai goi, gai goi zalo, gai goi long bien, gai goi 18

    ReplyDelete
  7. thank you a ton for sharing your knowledge! I am very Happy to find your blog.This is one wonderful blog article.I am sure that if I ask enough people, they'll agree with me, too
    Blocky Craft Police Squad Mommy Elsa Baby Caring online game best game Tomb Runner

    ReplyDelete