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

网站站内结构优化wordpress 加载中

网站站内结构优化,wordpress 加载中,亚马逊网站特点,网站做自己的超链接前言 设计模式在我们日常的软件开发中无处不在#xff0c;它们帮助我们编写更易扩展、更具可读性的代码。 今天结合我实际工作场景和源码实例#xff0c;跟大家一起聊聊工作中最常用的8种设计模式#xff0c;希望对你会有所帮助。 1. 单例模式 单例模式确保一个类只有一…前言 设计模式在我们日常的软件开发中无处不在它们帮助我们编写更易扩展、更具可读性的代码。 今天结合我实际工作场景和源码实例跟大家一起聊聊工作中最常用的8种设计模式希望对你会有所帮助。 1. 单例模式 单例模式确保一个类只有一个实例通常用于管理共享资源如配置、缓存、线程池等。 代码实现双重检查锁 这是单例模式的标准写法既保证线程安全又避免性能损耗。 public class Singleton {private static volatile Singleton instance;private Singleton() {}public static Singleton getInstance() {if (instance  null) {synchronized (Singleton.class) {if (instance  null) {instance  new Singleton();}}}return instance;} }JDK 中的应用 java.lang.Runtime.getRuntime() java.util.logging.Logger Spring 中的应用 Spring 的 Bean 默认是单例模式。可以通过 Scope(prototype) 将其改为多例。 2. 工厂模式 工厂模式用于封装对象的创建逻辑特别是当类实例化过程复杂时可以降低耦合度。 代码实现简单工厂 以支付系统为例不同支付方式需要不同的对象。 public class PaymentFactory {public static Payment createPayment(String type) {switch (type) {case AliPay:return new AliPay();case WeChatPay:return new WeChatPay();default:throw new IllegalArgumentException(Unknown payment type);}} }JDK 中的应用 java.util.Calendar.getInstance() javax.xml.parsers.DocumentBuilderFactory.newInstance() Spring 中的应用 BeanFactory 和 ApplicationContext 都是工厂模式的体现。 3. 策略模式 策略模式将不同算法封装为独立类并允许在运行时选择不同的策略。 代码实现促销策略 以电商促销为例支持满减、打折等多种策略。 public interface PromotionStrategy {void applyPromotion(); }public class DiscountStrategy implements PromotionStrategy {Overridepublic void applyPromotion() {System.out.println(Applying discount...);} }public class PromotionContext {private PromotionStrategy strategy;public PromotionContext(PromotionStrategy strategy) {this.strategy  strategy;}public void executePromotion() {strategy.applyPromotion();} }JDK 中的应用 java.util.Comparator 是典型的策略模式。 Spring 中的应用 事务管理TransactionManager支持编程式和声明式事务。 4. 代理模式 代理模式通过代理对象控制对目标对象的访问常用于权限控制、日志记录等场景。 代码实现静态代理 模拟对一个服务的权限控制。 public interface Service {void execute(); }public class RealService implements Service {Overridepublic void execute() {System.out.println(Executing real service...);} }public class ServiceProxy implements Service {private RealService realService;Overridepublic void execute() {System.out.println(Checking permissions...);if (realService  null) {realService  new RealService();}realService.execute();} }JDK 中的应用 动态代理 java.lang.reflect.Proxy RMI远程方法调用 Spring 中的应用 AOP面向切面编程广泛使用代理模式。 5. 观察者模式 观察者模式定义一对多的依赖当一个对象状态变化时所有依赖它的对象都会收到通知。 代码实现事件通知 模拟微博用户的粉丝通知。 public interface Observer {void update(String message); }public class User implements Observer {private String name;public User(String name) {this.name  name;}Overridepublic void update(String message) {System.out.println(name   received message:   message);} }public class Weibo {private ListObserver observers  new ArrayList();public void follow(Observer observer) {observers.add(observer);}public void post(String message) {for (Observer observer : observers) {observer.update(message);}} }JDK 中的应用 java.util.Observer 和 java.util.Observable javax.swing.event.ChangeListener Spring 中的应用 ApplicationEvent 和 ApplicationListener 是典型实现。 6. 装饰器模式 装饰器模式在不改变原始类的基础上动态扩展其功能。 代码实现咖啡加料 模拟一个咖啡订单系统可以动态加料。 public interface Coffee {String getDescription();double getCost(); }public class SimpleCoffee implements Coffee {Overridepublic String getDescription() {return Simple Coffee;}Overridepublic double getCost() {return 5.0;} }public class MilkDecorator implements Coffee {private Coffee coffee;public MilkDecorator(Coffee coffee) {this.coffee  coffee;}Overridepublic String getDescription() {return coffee.getDescription()  , Milk;}Overridepublic double getCost() {return coffee.getCost()  1.5;} }JDK 中的应用 java.io.BufferedInputStream 和 java.io.BufferedOutputStream Spring 中的应用 BeanPostProcessor 用于动态修改 Bean 的行为。 7. 模板方法模式 模板方法模式定义一个算法的骨架把具体的实现留给子类。 代码实现任务执行模板 模拟定时任务的执行流程。 public abstract class Task {public final void execute() {init();doWork();cleanup();}protected abstract void init();protected abstract void doWork();protected void cleanup() {System.out.println(Default cleanup...);} }public class DataProcessingTask extends Task {Overrideprotected void init() {System.out.println(Initializing data...);}Overrideprotected void doWork() {System.out.println(Processing data...);} }JDK 中的应用 java.util.AbstractList 和 java.util.AbstractMap Spring 中的应用 JdbcTemplate 和 RestTemplate 8. 建造者模式 建造者模式用于创建复杂对象特别是当对象有多个可选参数时。 代码实现构建 HTTP 请求 public class HttpRequest {private String method;private String url;private String body;private HttpRequest(Builder builder) {this.method  builder.method;this.url  builder.url;this.body  builder.body;}public static class Builder {private String method;private String url;private String body;public Builder method(String method) {this.method  method;return this;}public Builder url(String url) {this.url  url;return this;}public Builder body(String body) {this.body  body;return this;}public HttpRequest build() {return new HttpRequest(this);}} }JDK 中的应用 StringBuilder Stream.Builder Spring 中的应用 UriComponentsBuilder 用于构建 URI。 总结 这些设计模式不仅在日常开发中有着广泛应用更在 JDK 和 Spring 中深度体现。 了解它们的本质和应用场景能够让我们写出更优雅、更健壮的代码。
http://www.dnsts.com.cn/news/5739.html

相关文章:

  • 揭阳企业网站建设开发wordpress免费的模板下载
  • 广州市城乡和建设局网站首页本溪做网站
  • nas怎么做自己的网站企业网站系统建设
  • 做网站支付系统难度前端开发就业前景
  • 临沧市住房和城乡建设网站佛山优化网站方法
  • 营销型网站 案例山东省城乡与住房建设厅网站
  • 上海企业网站制作报价医疗网站建设讯息
  • 常州网站建设乛薇网站网页模板
  • 成功的网站必须具备的要素wordpress附件分离
  • 怎样做一个网站平台网站的盈利点
  • 赣州市铁路建设办公室网站北京工商注册网上核名
  • 网站建设宽度一般都是多少钱免费网络电话免费版试用
  • 网站内页的设计重庆校园网站开发
  • 信息查询类网站是怎么做的盐城做网站的公司地址
  • 如何设计和建立一个公司的网站wordpress登录名
  • 如何使用手机看建设网站网站建设推广怎么做
  • 高中网站建设课程摄影网站的设计与实现开题报告
  • 做网站计入什么科目哪些软件属于网页制作工具
  • 山东青岛网站设计网站备案成功后怎么
  • 天津做网站的大公司网站开发技术学习
  • 萧山网站制作公司宿州网站建设工作室
  • 网站管理手册基本的网站建设知识
  • 免费咨询中心sem和seo是什么
  • 宝安附近公司做网站建设哪家效益快郎溪县建设局网站
  • 附近的网站设计制作价格义乌专业做网站
  • 做企业网站哪家好做好公众号 网站建设
  • 建站公司服务短租房网站哪家做最好
  • 如何在百度提交网站广东黄页企业名录
  • 广州品牌网站设计价格万链网站做的怎么样?
  • 岳阳博物馆网站网站一般字体