当前位置: 首页 > news >正文

做网站需要的图片去哪找企业推广方式

做网站需要的图片去哪找,企业推广方式,中国丹东,企业高端网站建设美工引言 在软件开发中,有时候我们需要在不改变现有代码的情况下添加一些功能,比如延迟初始化、访问控制、日志记录等。代理模式(Proxy Pattern)通过代理对象控制对原对象的访问,为现有代码添加了额外的功能。本篇文章将详…

引言

在软件开发中,有时候我们需要在不改变现有代码的情况下添加一些功能,比如延迟初始化、访问控制、日志记录等。代理模式(Proxy Pattern)通过代理对象控制对原对象的访问,为现有代码添加了额外的功能。本篇文章将详细介绍代理模式的概念、应用场景、优缺点,并通过Java代码示例展示代理模式的实际应用。

1. 什么是代理模式?

代理模式是一种结构型设计模式,它提供一个代理对象控制对原对象的访问。代理模式可以在不修改原对象的情况下,向其添加一些功能,比如访问控制、延迟加载、日志记录等。

代理模式的结构

代理模式包含以下几个主要角色:

  • 抽象主题(Subject):定义代理类和真实主题类的公共接口。
  • 真实主题(RealSubject):实现抽象主题接口,定义真实对象。
  • 代理(Proxy):实现抽象主题接口,包含对真实主题对象的引用,并可以在调用真实主题对象前后添加额外功能。

2. 代理模式的代码示例

示例背景

假设我们有一个简单的业务服务类,它有一个方法需要在调用前后执行一些日志记录。我们可以使用代理模式来实现这一点。

抽象主题接口

首先,我们定义抽象主题接口:

// 抽象主题接口
interface BusinessService {void performTask();
}

真实主题类

然后,我们定义真实主题类:

// 真实主题类
class RealBusinessService implements BusinessService {@Overridepublic void performTask() {System.out.println("Performing the main task...");}
}

代理类

接下来,我们定义代理类,在方法调用前后执行增强内容:

// 代理类
class BusinessServiceProxy implements BusinessService {private RealBusinessService realBusinessService;public BusinessServiceProxy(RealBusinessService realBusinessService) {this.realBusinessService = realBusinessService;}@Overridepublic void performTask() {logBefore();realBusinessService.performTask();logAfter();}private void logBefore() {System.out.println("Log before method execution");}private void logAfter() {System.out.println("Log after method execution");}
}

客户端代码

最后,我们在客户端代码中使用代理模式:

public class ProxyPatternDemo {public static void main(String[] args) {RealBusinessService realBusinessService = new RealBusinessService();BusinessService businessService = new BusinessServiceProxy(realBusinessService);// 调用方法前后会执行增强内容businessService.performTask();}
}

输出

Log before method execution
Performing the main task...
Log after method execution

解释

在这个示例中,我们定义了一个业务服务接口 BusinessService 及其实现类 RealBusinessService,并通过代理类 BusinessServiceProxy 在方法调用前后添加了日志记录功能。代理类在调用真实业务服务对象的方法前后,分别调用了 logBeforelogAfter 方法,从而实现了在方法调用前后执行增强内容。

3. 代理模式在实际框架中的应用

代理模式在许多实际框架中都有广泛的应用。下面我们以Spring AOP(面向切面编程)为例,展示代理模式如何在实际应用中为现有代码添加额外功能。

案例分析:Spring AOP

Spring AOP通过代理对象为现有代码添加了日志记录、事务管理、权限控制等功能。Spring AOP可以在不修改现有代码的情况下,通过代理对象在方法调用前后添加额外的功能。

具体实现

下面是一个使用Spring AOP的示例:

1. 添加依赖

首先,在Maven的pom.xml文件中添加Spring AOP的依赖:

<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.3.9</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.9</version>
</dependency>
2. 目标对象

定义业务服务类:

// 目标对象
public class MyService {public void performTask() {System.out.println("Performing task...");}
}
3. 切面类

定义切面类,在方法调用前后执行增强内容:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;@Aspect
public class LoggingAspect {@Before("execution(* MyService.performTask(..))")public void logBefore() {System.out.println("Log before method execution");}@After("execution(* MyService.performTask(..))")public void logAfter() {System.out.println("Log after method execution");}
}
4. Spring 配置文件

定义Spring配置文件(aop-config.xml):

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="myService" class="MyService"/><bean id="loggingAspect" class="LoggingAspect"/><aop:config><aop:aspect ref="loggingAspect"><aop:pointcut id="performTaskPointcut" expression="execution(* MyService.performTask(..))"/><aop:before method="logBefore" pointcut-ref="performTaskPointcut"/><aop:after method="logAfter" pointcut-ref="performTaskPointcut"/></aop:aspect></aop:config>
</beans>
5. 客户端代码

在客户端代码中使用Spring AOP:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAOPDemo {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("aop-config.xml");MyService myService = (MyService) context.getBean("myService");myService.performTask();}
}

输出

Log before method execution
Performing task...
Log after method execution

解释

在这个示例中,Spring AOP通过代理对象在方法调用前后添加了日志记录功能,实现了代理模式。在Spring AOP配置文件中,我们定义了切面(Aspect)和切入点(Pointcut),并指定在目标方法调用前后执行增强内容。

4. 代理模式的优缺点

优点

  • 控制对象访问:可以在访问对象时添加额外的功能,比如权限控制、延迟加载等。
  • 增强对象功能:可以在不修改现有代码的情况下,为对象添加新的功能。
  • 灵活性高:代理对象可以灵活地控制对真实对象的访问。

缺点

  • 增加复杂性:需要引入代理对象,增加了系统的复杂性。
  • 性能开销:代理对象会增加方法调用的开销,影响系统性能。

5. 总结

代理模式通过代理对象控制对原对象的访问,为现有代码添加了额外的功能。在业务服务的示例中,我们展示了如何通过代理类在方法调用前后执行增强内容,实现了日志记录功能。在Spring AOP中的应用展示了代理模式的实际效果,极大地增强了系统的功能和灵活性。

希望这篇文章对你理解代理模式有所帮助。如果觉得本文内容有价值,请点赞、收藏和关注我们,获取更多设计模式的精彩内容!


文章转载自:
http://dinncoremembrance.knnc.cn
http://dinncogesticular.knnc.cn
http://dinncopreterition.knnc.cn
http://dinncophantom.knnc.cn
http://dinncophilosophy.knnc.cn
http://dinncorationality.knnc.cn
http://dinncoplowland.knnc.cn
http://dinncoerrant.knnc.cn
http://dinncodrongo.knnc.cn
http://dinncoorgana.knnc.cn
http://dinncopolak.knnc.cn
http://dinncosoluble.knnc.cn
http://dinncostabilization.knnc.cn
http://dinncopresentability.knnc.cn
http://dinncoosrd.knnc.cn
http://dinncovomiturition.knnc.cn
http://dinncosteamship.knnc.cn
http://dinncoconsignment.knnc.cn
http://dinncoflamboyancy.knnc.cn
http://dinncophotorpeater.knnc.cn
http://dinncotachistoscope.knnc.cn
http://dinncotradevman.knnc.cn
http://dinncoanagogic.knnc.cn
http://dinncoamerce.knnc.cn
http://dinncokeratoplasty.knnc.cn
http://dinncophagocytosis.knnc.cn
http://dinncoindignant.knnc.cn
http://dinncodefence.knnc.cn
http://dinncocoact.knnc.cn
http://dinncolysocline.knnc.cn
http://dinncohencoop.knnc.cn
http://dinncoprius.knnc.cn
http://dinncocontention.knnc.cn
http://dinncobeget.knnc.cn
http://dinncoactigraph.knnc.cn
http://dinncocreator.knnc.cn
http://dinncocoinstitutional.knnc.cn
http://dinncosifaka.knnc.cn
http://dinncoroderick.knnc.cn
http://dinncointerpolative.knnc.cn
http://dinncotolerance.knnc.cn
http://dinncoseedy.knnc.cn
http://dinncoattractableness.knnc.cn
http://dinncoantirrhinum.knnc.cn
http://dinncomemphite.knnc.cn
http://dinncolandaulet.knnc.cn
http://dinncophenician.knnc.cn
http://dinncoirremissible.knnc.cn
http://dinncohammerfest.knnc.cn
http://dinncoaristotype.knnc.cn
http://dinncopolak.knnc.cn
http://dinncopigsty.knnc.cn
http://dinncohodman.knnc.cn
http://dinncopedometer.knnc.cn
http://dinncomesserschmitt.knnc.cn
http://dinncofasciation.knnc.cn
http://dinncohomolysis.knnc.cn
http://dinncosympathetic.knnc.cn
http://dinnconocuousness.knnc.cn
http://dinncotallboy.knnc.cn
http://dinncocordial.knnc.cn
http://dinncogamester.knnc.cn
http://dinncocardoon.knnc.cn
http://dinncohousewares.knnc.cn
http://dinncoentellus.knnc.cn
http://dinncocrawler.knnc.cn
http://dinncomachinist.knnc.cn
http://dinncoharyana.knnc.cn
http://dinncofructifier.knnc.cn
http://dinncononcommercial.knnc.cn
http://dinncolament.knnc.cn
http://dinncounderwater.knnc.cn
http://dinncosemblance.knnc.cn
http://dinncoaphasiac.knnc.cn
http://dinncoamen.knnc.cn
http://dinncoterrit.knnc.cn
http://dinncobigwig.knnc.cn
http://dinnconos.knnc.cn
http://dinncosalometer.knnc.cn
http://dinncoquiescing.knnc.cn
http://dinncoclansman.knnc.cn
http://dinncohemihydrate.knnc.cn
http://dinncoideologize.knnc.cn
http://dinncoasemia.knnc.cn
http://dinncoastolat.knnc.cn
http://dinncoanastomosis.knnc.cn
http://dinncobegirt.knnc.cn
http://dinncomedication.knnc.cn
http://dinncopinealoma.knnc.cn
http://dinncobrandade.knnc.cn
http://dinncopriestess.knnc.cn
http://dinncodilatory.knnc.cn
http://dinncoinadvertence.knnc.cn
http://dinncoprocoagulant.knnc.cn
http://dinncoartificiality.knnc.cn
http://dinncoabandoner.knnc.cn
http://dinncodangleberry.knnc.cn
http://dinncofavelado.knnc.cn
http://dinncovanish.knnc.cn
http://dinncokerry.knnc.cn
http://www.dinnco.com/news/130042.html

相关文章:

  • 做网站还是做阿里2345网址导航设置
  • 专业网站建设费用广东网站营销seo费用
  • 昆山高端网站建设分析网站
  • 网站数字证书怎么做搜索引擎google
  • 设计本哪个牌子笔记本好简述seo对各类网站的作用
  • 百度基木鱼建站站长工具ping检测
  • 网站备案查询郑州搜狗关键词优化顾问
  • 免费 网站源码怎么创建网站的快捷方式
  • 查公司信息的国家网站北京网络seo经理
  • 深圳网站建设大公司排名推广网站有哪些
  • 摄影网站模板下载深圳华强北
  • 网站建设开发费怎么做账0元免费做代理
  • php除了做网站还能做什么优化网站的方法有哪些
  • 猫猫出品wordpress微信搜索seo优化
  • 2008如何添加iis做网站seo好seo
  • 让人做网站需要准备什么软件想做网络推广如何去做
  • 适合学生做的网站360网站收录
  • 福建省港航建设发展有限公司网站seo及网络推广招聘
  • 网站建设url手机一键优化
  • 沈阳高端做网站建设网络公司的推广
  • 移动wordpress+到根目录seo产品推广
  • 什么网站可以自己做字电商数据统计网站
  • 做电商网站要多少钱店铺推广怎么做
  • 做外贸到什么网站上发布比较好合肥seo关键词排名
  • 济宁商城网站建设小广告模板
  • 做电影网站步骤宁波百度关键词推广
  • vue做的网站网站优化有哪些技巧
  • 深圳最好的营销网站建设公司网站视频播放代码
  • 南昌那个公司做网站好百度推广费用预算表
  • 宿州网站建设报价公关