Home Database Mysql Tutorial 基于Java的代理设计模式_MySQL

基于Java的代理设计模式_MySQL

Jun 01, 2016 pm 02:11 PM
theme acting use Can picture based on accomplish object model

   一、引子

  我们去科技市场为自己的机器添加点奢侈的配件,很多DIYer都喜欢去找代理商,因为在代理商那里拿到的东西不仅质量有保证,而且价格和售后服务上都会好很多。客户通过代理商得到了自己想要的东西,而且还享受到了代理商额外的服务;而生产厂商通过代理商将自己的产品推广出去,而且可以将一些销售服务的任务交给代理商来完成(当然代理商要和厂商来共同分担风险,分配利润),这样自己就可以花更多的心思在产品的设计和生产上了。

  在美国,任何企业的产品要想拿到市场上去卖就必须经过代理商这一个环节,否则就是非法的。看来代理商在商业运作中起着很关键的作用。 不小心把话题扯远了,回过头来,那么在我们的面向对象的程序设计中,会不会有代理商这样的角色呢?来看这篇文章的人肯定不会说:没有!

  那么就跟着这篇文章来看看代理模式的奇妙吧。

  二、定义和分类

  代理模式在设计模式中的定义就是:为其他对象提供一种代理以控制对这个对象的访问。说白了就是,在一些情况下客户不想或者不能直接引用一个对象,而代理对象可以在客户和目标对象之间起到中介作用,去掉客户不能看到的内容和服务或者增添客户需要的额外服务。

  那么什么时候要使用代理模式呢?在对已有的方法进行使用的时候出现需要对原有方法进行改进或者修改,这时候有两种改进选择:修改原有方法来适应现在的使用方式,或者使用一个“第三者”方法来调用原有的方法并且对方法产生的结果进行一定的控制。第一种方法是明显违背了“对扩展开放、对修改关闭”(开闭原则),而且在原来方法中作修改可能使得原来类的功能变得模糊和多元化(就像现在企业多元化一样),而使用第二种方式可以将功能划分的更加清晰,有助于后面的维护。所以在一定程度上第二种方式是一个比较好的选择!

  当然,话又说回来了,如果是一个很小的系统,功能也不是很繁杂,那么使用代理模式可能就显得臃肿,不如第一种方式来的快捷。这就像一个三口之家,家务活全由家庭主妇或者一个保姆来完成是比较合理的,根本不需要雇上好几个保姆层层代理:)

  根据《Java与模式》书中对代理模式的分类,代理模式分为8种,这里将几种常见的、重要的列举如下:

  1. 远程(Remote)代理:为一个位于不同的地址空间的对象提供一个局域代表对象。比如:你可以将一个在世界某个角落一台机器通过代理假象成你局域网中的一部分。

  2. 虚拟(Virtual)代理:根据需要将一个资源消耗很大或者比较复杂的对象延迟的真正需要时才创建。比如:如果一个很大的图片,需要花费很长时间才能显示出来,那么当这个图片包含在文档中时,使用编辑器或浏览器打开这个文档,这个大图片可能就影响了文档的阅读,这时需要做个图片Proxy来代替真正的图片。

  3. 保护(Protect or Access)代理:控制对一个对象的访问权限。比如:在论坛中,不同的身份登陆,拥有的权限是不同的,使用代理模式可以控制权限(当然,使用别的方式也可以实现)。

  4. 智能引用(Smart Reference)代理:提供比对目标对象额外的服务。比如:纪录访问的流量(这是个再简单不过的例子),提供一些友情提示等等。

  代理模式是一种比较有用的模式,从几个类的“小结构”到庞大系统的“大结构”都可以看到它的影子。

  三、结构

  代理模式中的“代理商”要想实现代理任务,就必须和被代理的“厂商”使用共同的接口(你可以想象为产品)。所以自然而然你会想到在java中使用一个抽象类或者接口(推荐)来实现这个共同的接口。于是代理模式就有三个角色组成了:

  1.抽象主题角色:声明了真实主题和代理主题的共同接口。

  2.代理主题角色:内部包含对真实主题的引用,并且提供和真实主题角色相同的接口。

  3.真实主题角色:定义真实的对象。

  使用类图来表示下三者间的关系如下:

基于Java的代理设计模式_MySQL

  当然,图上所示的是代理模式中的一个具体情况。而代理模式可以非常灵活的使用其他方式来实现,这样就与图上所示有很大的区别。

  也许,现在你已经对代理模式已经有了一个宏观的认识了,下面我们来看看怎么实际的使用代理模式。

  四、举例

  以论坛中已注册用户和游客的权限不同来作为第一个例子:已注册的用户拥有发帖,修改自己的注册信息,修改自己的帖子等功能;而游客只能看到别人发的帖子,没有其他权限。为了简化代码,更好的显示出代理模式的骨架,我们这里只实现发帖权限的控制。 首先我们先实现一个抽象主题角色MyForum,里面定义了真实主题和代理主题的共同接口――发帖功能。

  代码如下:

  public interface MyForum

  {

  public void AddFile();

  }

  这样,真实主题角色和代理主题角色都要实现这个接口。其中真实的主题角色基本就是将这个接口的方法内容填充进来。所以在这里就不再赘述它的实现。我们把主要的精力放到关键的代理主题角色上。代理主题角色代码大体如下:

  public class MyForumProxy implements MyForum
  {
  private RealMyForum forum ;
  private int permission ; //权限值

  public MyForumProxy(int permission)
  {
  forum = new RealMyForum()
  this.permission = permission ;
  }

  //实现的接口

  public void AddFile()
  {
  //满足权限设置的时候才能够执行操作
  //Constants是一个常量类
  if(Constants.ASSOCIATOR == permission)
  {
   forum.AddFile();
  }
  else
   System.out.println("You are not a associator of MyForum ,please registe!");
  }
  }

  这样就实现了代理模式的功能。当然你也可以在这个代理类上添加自己的方法来实现额外的服务,比如统计帖子的浏览次数,记录用户的登录情况等等。

  还有一个很常见的代理模式的使用例子就是对大幅图片浏览的控制。在我们常见的网站上面浏览图文的信息时,不知道你有没有注意到,图片位置放置的是经过缩小的,当有人要仔细的查看这个图片时,可以通过点击图片来激活一个链接,在一个新的网页打开要看的图片 。这样对于提高浏览速度是很有好处的,因为不是每个人都要去看仔细图上的信息。这种情况就可以使用代理模式来全面实现。这里我将思路表述出来,至于实现由于工作原因,就不表述了,至于这种方式在B/S模式下的真实可行性,我没有确认过,只是凭空的想象。如果不是可行的方式,那这个例子可以放到一个C/S下来实现,这个是绝对没有问题的,而且在很多介绍设计模式的书和文章中使用。两种方式的实现有兴趣的可以来尝试一下。

  我们在浏览器中访问网页时是调用的不是真实的装载图片的方法,而是在代理对象中的方法,在这个对象中,先使用一个线程向浏览器装载了一个缩小版的图片,而在后台使用另一个线程来调用真实的装载大图片的方法将图片加载到本地,当你要浏览这个图片的时候,将其在新的网页中显示出来。当然如果在你想浏览的时候图片尚未加载成功,可以再启动一个线程来显示提示信息,直到加载成功。

  这样代理模式的功能就在上面体现的淋漓尽致――通过代理来将真实图片的加载放到后台来操作,使其不影响前台的浏览。

  五、总结

  代理模式能够协调调用者和被调用者,能够在一定程度上降低系统的耦合度。不过一定要记住前面讲的使用代理模式的条件,不然的话使用了代理模式不但不会有好的效果,说不定还会出问题的
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? Mar 21, 2024 pm 09:12 PM

With the popularity of Douyin short videos, user interactions in the comment area have become more colorful. Some users wish to share images in comments to better express their opinions or emotions. So, how to post pictures in TikTok comments? This article will answer this question in detail and provide you with some related tips and precautions. 1. How to post pictures in Douyin comments? 1. Open Douyin: First, you need to open Douyin APP and log in to your account. 2. Find the comment area: When browsing or posting a short video, find the place where you want to comment and click the "Comment" button. 3. Enter your comment content: Enter your comment content in the comment area. 4. Choose to send a picture: In the interface for entering comment content, you will see a "picture" button or a "+" button, click

How to make ppt pictures appear one by one How to make ppt pictures appear one by one Mar 25, 2024 pm 04:00 PM

In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to arrange two pictures side by side in wps document How to arrange two pictures side by side in wps document Mar 20, 2024 pm 04:00 PM

When using WPS office software, we found that not only one form is used, tables and pictures can be added to the text, pictures can also be added to the table, etc. These are all used together to make the content of the entire document look richer. , if you need to insert two pictures into the document and they need to be arranged side by side. Our next course can solve this problem: how to place two pictures side by side in a wps document. 1. First, you need to open the WPS software and find the picture you want to adjust. Left-click the picture and a menu bar will pop up, select "Page Layout". 2. Select "Tight wrapping" in text wrapping. 3. After all the pictures you need are confirmed to be set to "Tight text wrapping", you can drag the pictures to the appropriate position and click on the first picture.

See all articles