4-State: changing object behavior

An object that appears to change its class.

Indications: conditional code in most or all methods.

The State pattern switches from one implementation to another during the lifetime of the surrogate, in order to produce different behavior from the same method call(s). It’s a way to improve the implementation of your code when you seem to be doing a lot of testing inside each of your methods before deciding what to do for that method. For example, the fairy tale of the frog-prince contains an object (the creature) that behaves differently depending on what state it’s in. You could implement this using a boolean that you test:

//: state:KissingPrincess.java

package state;

import junit.framework.*;

 

class Creature {

  private boolean isFrog = true;

  public void greet() {

    if(isFrog)

      System.out.println("Ribbet!");

    else

      System.out.println("Darling!");

  }

  public void kiss() { isFrog = false; }

}

 

public class KissingPrincess extends TestCase  {

  Creature creature = new Creature();

  public void test() {

    creature.greet();

    creature.kiss();

    creature.greet();

  }

  public static void main(String args[]) {

    junit.textui.TestRunner.run(KissingPrincess.class);

  }

} ///:~

 

However, the greet() method, and any other methods that must test isFrog before they perform their operations, ends up with awkward code. By delegating the operations to a State object that can be changed, this code is simplified.

//: state:KissingPrincess2.java

package state;

import junit.framework.*;

 

class Creature {

  private interface State {

    String response();

  }

  private class Frog implements State {

    public String response() { return "Ribbet!"; }

  }

  private class Prince implements State {

    public String response() { return "Darling!"; }

  }

  private State state = new Frog();

  public void greet() {

    System.out.println(state.response());

  }

  public void kiss() { state = new Prince(); }

}

 

public class KissingPrincess2 extends TestCase  {

  Creature creature = new Creature();

  public void test() {

    creature.greet();

    creature.kiss();

    creature.greet();

  }

  public static void main(String args[]) {

    junit.textui.TestRunner.run(KissingPrincess2.class);

  }

} ///:~

 

In addition, changes to the State are automatically propagated throughout, rather than requiring an edit across the class methods in order to effect changes.

Here’s the basic structure of State:

 

//: state:StateDemo.java

// Simple demonstration of the State pattern.

package state;

import junit.framework.*;

 

interface State {

  void operation1();

  void operation2();

  void operation3();

}

 

class ServiceProvider {

  private State state;

  public ServiceProvider(State state) {

    this.state = state;

  }

  public void changeState(State newState) {

    state = newState;

  }

  // Pass method calls to the implementation:

  public void service1() {

    // ...

    state.operation1();

    // ...

    state.operation3();

  }

  public void service2() {

    // ...

    state.operation1();

    // ...

    state.operation2();

  }

  public void service3() {

    // ...

    state.operation3();

    // ...

    state.operation2();

  }

}

 

class Implementation1 implements State {

  public void operation1() {

    System.out.println("Implementation1.operation1()");

  }

  public void operation2() {

    System.out.println("Implementation1.operation2()");

  }

  public void operation3() {

    System.out.println("Implementation1.operation3()");

  }

}

 

class Implementation2 implements State {

  public void operation1() {

    System.out.println("Implementation2.operation1()");

  }

  public void operation2() {

    System.out.println("Implementation2.operation2()");

  }

  public void operation3() {

    System.out.println("Implementation2.operation3()");

  }

}

 

public class StateDemo extends TestCase  {

  static void run(ServiceProvider sp) {

    sp.service1();

    sp.service2();

    sp.service3();

  }

  ServiceProvider sp =

    new ServiceProvider(new Implementation1());

  public void test() {

    run(sp);

    sp.changeState(new Implementation2());

    run(sp);

  }

  public static void main(String args[]) {

    junit.textui.TestRunner.run(StateDemo.class);

  }

} ///:~

 

Proxy 和 State 总结:

Object decoupling

Both Proxy and State provide a surrogate class that you use in your code; the real class that does the work is hidden behind this surrogate class. When you call a method in the surrogate, it simply turns around and calls the method in the implementing class. These two patterns are so similar that the Proxy is simply a special case of State . One is tempted to just lump the two together into a pattern called Surrogate , but the term “proxy” has a long-standing and specialized meaning, which probably explains the reason for the two different patterns.

The basic idea is simple: from a base class, the surrogate is derived along with the class or classes that provide the actual implementation:


                     

When a surrogate object is created, it is given an implementation to which to send all of the method calls.

Structurally, the difference between Proxy and State is simple: a Proxy has only one implementation, while State has more than one. The application of the patterns is considered (in Design Patterns ) to be distinct: Proxy is used to control access to its implementation, while State allows you to change the implementation dynamically. However, if you expand your notion of “controlling access to implementation” then the two fit neatly together.

 

Sparsh:用于基于视觉的触觉感知的自监督触觉表征【传感器:带标记点的 GelSight 2017、无标记点的 DIGIT】 t​⊕It−5​→x∈Rh×w×6tt−2t−4t−6∈R4×h×w×3t​∈Rh×w×3t​∈SE3). Tt​tt−1​≜ΔxΔy˙​Δθ∈SE2ˉ2∘0.5∘1.0∘to1.0∘2.0∘zt−1​zt​qt−1​qt​≜Δqt​Δqt1​...;Δq∈R7。 阅读详情

相关推荐

视觉特征-自监督-同源不同crop视图一致性【2025-08】:DINOv3【DINOv2+Gram损失=DINO损失+(iBOT损失+Gram损失)+KoLeo正则化损失】

自监督学习有望消除手动数据标注的必要性,从而使模型能够轻松处理大规模数据集以及更复杂的架构。由于无需针对特定任务或领域进行定制,这种训练方式能够利用单一算法从各种来源获取视觉表征,这些来源包括自然图像和航空图像等。本技术报告介绍了 DINOv3,它通过简单而有效的策略,为实现这一目标迈出了重要一步。首先,我们通过精心设计数据准备流程并进行优化,从而提升数据集与模型规模的扩展能力;其次,我们提出了一种名为“Gram 锚定”的新方法,该方法有效解决了在长时间训练过程中特征图质量会下降这一长期存在的难题。

u013250861的博客 353

Chain-of-Code:让大模型真正理解代码逻辑的推理链方法

程序分析是软件工程的核心能力,涉及控制流、数据流与语义契约等基础概念;而大模型代码生成长期受限于黑箱式续写,缺乏可追溯的推理过程。Chain-of-Code通过结构化思维链(CoT)将符号执行、程序切片与SMT约束求解深度耦合,使模型从‘猜代码’转向‘证代码’——每一步推理都绑定具体代码片段、状态变更与可验证依据。该方法显著提升代码审查、竞态分析与技术债务识别等高风险场景的确定性与可审计性,为AI编程辅助从工具层跃迁至工程决策层提供关键技术路径。

weixin_30615767的博客 692

视觉特征-自监督-同源不同crop视图一致性【2023-04】:DINOv2(无监督学习鲁棒视觉特征)【12 亿张互不重复的图像】【判别式自监督方法】【交叉熵损失+iBOT损失】

近期,自然语言处理领域在大量数据上进行模型 Pretraining 取得的突破,为计算机视觉中类似Foundation Model 的发展开辟了道路。这些模型能够生成通用视觉特征,即无需微调即可跨图像分布和任务发挥作用的特征,从而大幅简化图像在任何系统中的使用。本研究表明,现有的 Pretraining 方法,尤其是自监督方法,如果在来自多样化来源的充足精选数据上进行训练,便能够产生此类特征。我们重新审视了现有方法,并结合不同技术,从数据规模和模型规模两方面扩展我们的 Pretraining。

u013250861的博客 369

Godot 2D游戏敌人AI开发:从状态机到行为树的完整实现指南

在游戏开发中,有限状态机(FSM)是一种用于管理对象行为状态的经典设计模式,它通过定义一组有限的状态以及状态间的转换规则,使复杂的行为逻辑变得清晰且易于维护。其核心原理是将行为分解为离散的状态(如空闲、巡逻、追逐、攻击),并在特定条件触发时进行状态切换,从而模拟出智能体的决策过程。这种模式在游戏AI、动画控制和UI流程管理中具有重要技术价值,能够有效解决代码耦合度高、难以扩展的问题。在2D游戏开发领域,尤其是使用Godot引擎时,结合Area2D感知系统和AnimationTree动画状态机,可以构建出响应

weixin_30258901的博客 317

国科大 雁栖湖校区 研一上 课程避坑与生存指南

本文为国科大雁栖湖校区研一新生提供全面的课程避坑与生存指南,涵盖选课策略、核心课程学习技巧、校园生活攻略及心态管理。重点解析算法设计与分析、人工智能原理等硬核课程的学习方法,并分享食堂、住宿、自习室等实用生存技巧,帮助新生高效适应研究生生活。

weixin_29304985的博客 301

使用这个算法我可以实现英雄联盟里英雄的走位|Java 开发实战

A算法,A(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法,也是解决许多搜索问题的有效算法。算法中的距离估算值与实际值越接近,最终搜索速度越快。 基本概念 首先在大学我们遇到最多的算法Dijkstra、Floyd、广度搜索、深度搜索。关于这些算法我们以后再慢慢的研究,今天的重点在A算法上。A算法是一种启发式算法。与上述几种算法不同的是A*算法在考虑起始节点的同时还会考虑到目标节点的代价。 在A*算法中我们给每个节点都定义一些属性。最基本的就是下文提到的三基数-这里的三基数是我自己定义.

Java进阶营 312

LPDDR5 JESD209-5B 第4章:Initialization and Training 详解

LPDDR5 初始化和training章节

xinsan5989的博客 555

地平线旭日X3派(RDK X3)从开箱到联网:新手避坑指南

本文为地平线旭日X3派(RDK X3)新手提供从开箱到联网的完整避坑指南。详细讲解了硬件识别、系统镜像烧录、串口调试(波特率921600)、有线/无线网络配置以及SSH/VNC远程访问等关键步骤,帮助开发者快速搭建稳定的开发环境,为后续AI应用部署扫清障碍。

weixin_29174385的博客 939

CANN/mat-chem-sim-pred PID整定算子基准

This document records the measured CPU/NPU behavior of `PidFopdtBatchRolloutScore`. ## Environment - NPU host: `node202` - Device: `Ascend910B3`, device id `0` - CANN: `/usr/local/Ascend/ascend-tool

gitblog_01157的博客 1036

实现高精度卷积混响--MATLAB仿真篇

一、前言 之前在STM32F407上现实了直播声卡,实现的经典Schroeder混响模型和Moorer混响模型,得到不错效果。后来得知卷积混响最为真实地表现空间的特性,所以也就尝试在MCU上实现一个高精度的卷积混响(卷积点数为32768),目前MCU出来的混响效果算得上一流水准,本人也在计划在ADI的21489或者SC584上更高精度的卷积混响。 二、卷积方式 1、conv1是使用MATLAB自带卷积函数conv 2、conv2和conv3是改进后卷积过程,具有减少运算量特点,方便嵌入式移植 clear;

Six44的博客 5079

比较器和运算放大器-2

今天继续了解输入阻抗问题,观察下图:首先考虑在我们没有接运放时,A点输出电压为6V,当接入如图所示的阻抗时电压还是6V吗?共模电阻和红框内对地电阻并联,阻值变小,相当于5k,通过计算可以求得A点电压变为4V,这样A的值发生了改变,试想一下这样的信号接入到运放的两端是不是相当于比较的两端信号发生了改变,那结果一定会发生偏离。信号源一般内阻是不确定的,一般也不是很小,如果不想改变输入阻抗,那唯一的办法就是改变运放的内阻了,比如内阻改为100K,1M等,越大越好,是不是A点电压就会无限接近6V。

weixin_42855218的博客 1312

CANN/cannbot-skills:CUDA到Ascend C SIMT迁移工具

name: cuda2ascend-simt description: "将 CUDA 算子/Kernel 迁移到 Ascend C SIMT。触发:CUDA 转 Ascend、CUDA 迁移昇腾、cuda kernel porting to Ascend、CUDA to Ascend SIMT、CUDA 代码移植 NPU、GPU 算子转 NPU、.cu 转 .asc、CUDA 转 NPU。Use

gitblog_00009的博客 487

MATLAB机械臂轨迹规划:在关节空间中生成多项式轨迹以及 jtraj函数(内容超详细讲解)

matlab机器人仿真,关节空间多项式轨迹生成

qq_40969179的博客 1万+

昇腾GE图拆分模块约束文档

Graph execution module should try to avoid dynamic memory allocation (may cause random performance degradation). Graph compilation module memory reuse processing phase forbids graph modification (mult

gitblog_00328的博客 758

全面指南:PyTorch与ROCm版本兼容性及多版本环境管理

本文详细介绍了在AMD GPU上配置PyTorch开发环境的完整流程,重点解决版本兼容性和多版本ROCm环境管理问题。主要内容包括:1) PyTorch与ROCm版本对应关系查询;2) 四种查看ROCm版本的方法;3) 使用update-alternatives管理多版本ROCm;4) 环境变量配置与冲突解决方案;5) 完整的验证流程和性能测试方法。文章提供了最佳实践建议和故障排除清单,帮助开发者快速搭建稳定高效的深度学习开发环境,充分发挥AMD GPU硬件性能。通过系统化的验证流程确保各组件协同工作,解决

数字人生 606

【系统分析师】2025年下半年真题:综合知识-答案及详解(回忆版)

本文摘要包含系统分析师考试相关题目解析,主要涵盖软件工程、数据库、网络协议和项目管理等知识点。内容包括:软件再工程与逆向工程的区别(D);GNU GPL开源协议特性(A);TCP/IP协议分层(A、B);数据库范式判定(A、B);前导图进度管理工具(C);系统规划中的数据类识别方法(B、D);关系代数除运算(C);CMM优化级特征(A);回归测试重要性(C)。这些解析帮助理解核心概念和考试重点。

数据知道的博客 6717

Android 12 S WindowManager解读之从dumpsys window windows看window

();;();;();执行adb shell dumpsys window windows 如下:rootTaskId=1(根Taskid) mSession=Session{e98843e 2512:u0a10120} mClient=android.os.BinderProxy@64c3da9appop=NONEmcc?mcc?

weixin_41028555的博客 4860

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

微积分导读:从圆周率到导数与积分

……

u013669912的博客 1400

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

SH9面向碳硅认知对齐的轻量化度量:TCFT‑Lite 中的拓扑偏差角与递归收敛机制(世毫九实验室原创研究)

AbstractR_η: M → MR_η: M → Mbreak。

shardylab.blog.youkuaiyun.com 542

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

转:How to fix your entire life in 1 day 如何在一天内重启人生

行动朝目标推进

Rolei的博客 265

机器人-模仿学习-双臂【2023-04】:ACT【80M参数;CVAE-Transformer架构;ResNet18编码四路视觉;4层编码器融合视觉状态;7层解码器并行预测100步动作;时序集成平滑】

Meta。

u013250861的博客 429

大模型/agent便于理解的技术交接报告skill

我发现“技术报告难读,往往不是因为术语太多,而是因为知识出现顺序错了”。然后我把这个认识压缩成了一个极简 Codex Skill。

weixin_44151034的博客 42

Evo‑1:保持语义对齐的轻量级视觉‑语言‑动作模型

视觉-语言-动作 (VLA) 模型已成为一个强大的框架它统一了感知、语言和控制,使机器人能够通过多模态理解执行多样化的任务。然而,当前的VLA模型通常包含海量参数,并严重依赖大规模机器人数据预训练,导致训练过程中的高计算成本,以及实时推理的部署能力受限。此外,大多数训练范式往往会降低视觉-语言主干网络的感知表征,导致过拟合和对下游任务的泛化能力差。在这项工作中,我们提出了Evo-1,一个轻量级VLA模型,它在不进行机器人数据预训练的情况下,降低了计算量并提高了部署效率,同时保持了强大的性能。

u013250861的博客 175

多维聚合前的数据变形:语义对齐、结构解耦与上下文注入

多维聚合不是简单GROUP BY,而是建立在精准数据变形基础上的高维分析工程。其核心原理在于:先通过语义对齐将原始字段映射为业务可理解的维度标签,再经结构解耦处理嵌套、数组、JSON等非规范化结构,最后注入用户画像、活动日历、地理层级等外部上下文,构建语义完备的宽表。这一过程直接决定聚合结果的准确性、性能上限与业务可解释性。尤其在实时风控、电商归因、IoT设备监控等场景中,缺失时间维度标准化、分类字段规范化或状态字段快照化等关键变形动作,将导致环比计算失真、用户行为链路断裂、指标重复计数等典型问题。本文聚焦

记录技术点点之路 464

Tbase 源码 (一)

Tbase 源码 分布式MPP数据库

driftingman的专栏 1483

InternVLA‑A1.5:统一理解、潜在预见与动作,实现组合泛化

前面已经知道,一段视频经过 时空块嵌入(Tubelet Embedding) 之后,会从原始的视频帧转换成一个三维 时空 Token 网格(Spatio-temporal Token Grid)。例如 V-JEPA 中典型的输入:[16,224,224,3][16,224,224,3][16,224,224,3]经过大小为:2×16×162\times16\times162×16×16的三维 Patch Embedding 后,会得到:[8,14,14,de][8,14,14,d_e][8,14,14,de

u013250861的博客 177
上一篇: 2-Singleton
下一篇: 5-Iterators: decoupling algorithms from containers
ccfangle
博客等级 码龄20年 1粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值