Temporal Property

本文介绍了一种软件设计模式——时间属性模式,该模式提供了一种处理随时间变化的对象属性的方法。文章详细阐述了如何通过访问器函数及更新操作来实现属性的时间维度,并提供了Java语言的示例代码。
A property that changes over time




Usually when we see properties on a class, they represent questions we can ask of an object now. However there are times when we don't want to just ask questions about a property of an object now, we also want to ask these questions about some point in the past when things may have changed.


How it Works


The key to this pattern is providing a regular and predictable interface for dealing with those properties of an object that change over time. The most important part of this lies in the accessor functions. You will always see an accessor function that takes a Time Point as an argument: this allows you to ask "what was Mr Fowler's address on 2 Feb 1998?". In addition you'll usually see an accessor that takes no argument, this is a question about an address according to some default, usually today.


As well accessing information, you'll often need to update information. The simplest form of modification is an additive update. You can think of an additive update as one that adds another piece of information on the end of the timeline.An additive update can be handled with a single put method that takes a timepoint and a new value. So this would say "change Mr Fowler's address to 15 Damon Ave with effect from 23 August 1998". The supplied date can be in the past, indicating a retroactive change; in the present, indicating a current change; or in the future, reflecting a scheduled change. Again you may find it useful to have a put method without a timepoint that uses today (or a similar default) as the effective date for the change. This makes it easy to make current changes.


The other kind of update is an insertion update. This one of the form where you want to say "we had Mr Fowler moving in to 963 Franklin St in January 1998, but we need to change that to December 1997". The point here is that this modifying a piece of temporal information we currently have, rather than adding one on the end. For this you need a reference that access the value at the time you currently have and then adjusts it to a new range. It is not enough just to supply the value here, since the value may be valid during different ranges: I can move out and then return to the same address.


For an example let say I lived at 154 Norwood Rd from Sep 85 to Jan 87 moved to Brighton for six months and returned to stay for a while longer. If I needed to adjust the first stay's departure from January to February it's important to get the first stay and not the second. So I need both the address and something that identifies the first stay - in practice any date during the range of the first stay.


If the value you're holding is a Value Object you can actually just get away with an additive update, although an insertion update may be more easy to use.


There are two ways to implement Temporal Property. One is to have a collection of objects using Effectivity and then manipulate this collection. However once you find yourself doing this more than once you'll realize that it may be best to create a special collection class that provides this behavior: a temporal collection. Such a class is quite easy to write and can be used whenever you need a Temporal Property.


When to use it


You should use a Temporal Property when you have a class that has a few properties that display temporal behavior, and you want easy access to those temporal values.


The first point of this is the point about easy access. The easiest way to record temporal changes is to use Audit Log. The disadvantage of Audit Log is that you need extra work to process the log. So the first thing you need to know is under what circumstances people will need the history of that property. Remember that it's not difficult to refactor a regular property into a temporal one.(You can just replace the target field with a temporal collection, and easily maintain the existing interface.)


The second point is to consider how many properties are temporal. If most of the properties of the class are temporal, then you'll need to use Temporal Object.


Further Reading


I first described Temporal Property in [fowler-ap] under the name Historic Mapping. My ideas were then refined by collaboration with Andy Carlson and Sharon Estepp in preparing the temporal patterns paper in [PLoPD 4]. Francis Anderson's plop paper also describes this pattern under the name History on Association.


Example: Using a Temporal Collection (Java)


A temporal collection is a simple way to implement a Temporal Property. The basic representation and interface for a temporal collection is similar to a map: provide a get and put operation that uses a date as an index. Indeed a map makes a good backing collection for it.


class TemporalCollection...
  private Map contents = new HashMap();
  public Object get(MfDate when) {
    /** returns the value that was effective on the given date */
    Iterator it = milestones().iterator();
    while (it.hasNext()) {
      MfDate thisDate = (MfDate) it.next();
      if (thisDate.before(when) || thisDate.equals(when)) return contents.get(thisDate);
    }
    throw new IllegalArgumentException("no records that early");
  }
  public void put(MfDate at, Object item) {
    /** the item is valid from the supplied date onwards */
    contents.put(at,item);
    clearMilestoneCache();
  }
The map contains the values indexed by the start date of when they become effective. The milestones method returns these keys in reverse order. The get method then works through these milestones to find the right key. This algorithm works best when you are more likely to ask for the most recent value.


If you access the temporal collection more often than you update it, it may be worth caching the milestones collection.


class TemporalCollection...
  private List _milestoneCache;
  private List milestones() {
    /** a list of all the dates where the value changed, returned in order
    latest first */
    if (_milestoneCache == null)
      calculateMilestones();
    return _milestoneCache;
  }
  private void calculateMilestones() {
    _milestoneCache = new ArrayList(contents.size());
    _milestoneCache.addAll(contents.keySet());
    Collections.sort(_milestoneCache, Collections.reverseOrder());
  }
  private void clearMilestoneCache() {
    _milestoneCache = null;
  }
When you have a temporal collection written, then it is easy to make a temporal collection of addresses for a customer.


class Customer...
  private TemporalCollection addresses = new SingleTemporalCollection();
  public Address getAddress(MfDate date) {
    return (Address) addresses.get(date);
  }
  public Address getAddress() {
    return getAddress(MfDate.today());
  }
  public void putAddress(MfDate date, Address value) {
    addresses.put(date, value);
  }
One of the biggest problems using the temporal collection is if you have to persist the collection into a relational database - the mapping to tables is not exactly straightforward. Essentially the relational database needs to use Effectivity. This often means that you'll need to create an intersection table as a place for the date range. Some of this code can be generalized to the temporal collection class, but some explicit mapping code will be needed.


Example: Implementing a Bi-temporal Property (Java)


Before thinking about how a Bi-temporal property works, it's worth thinking about what it must do. Essentially the bi-temporal property allows us to store historic information over time and retain a full history across both dimensions. So we build up a history like this.


class Tester...
  private Customer martin;
  private Address franklin = new Address ("961 Franklin St");
  private Address worcester = new Address ("88 Worcester St");
  public void setUp () {
    MfDate.setToday(new MfDate(1996,1,1));
    martin = new Customer ("Martin");
    martin.putAddress(new MfDate(1994, 3, 1), worcester);
    MfDate.setToday(new MfDate(1996,8,10));
    martin.putAddress(new MfDate(1996, 7, 4), franklin);
    MfDate.setToday(new MfDate(2000,9,11));
  }
Notice the rhythm to the updates. When we are storing bi-temporal history the record date is always today. So in our tests we change the current date first and then store information in the history. As we put information into the history we supply the actual date.


The resulting history looks like this.


class Tester...
  private MfDate jul1 = new MfDate(1996, 7, 1);
  private MfDate jul15 = new MfDate(1996, 7, 15);
  private MfDate aug1 = new MfDate(1996, 8, 1);
  private MfDate aug10 = new MfDate(1996, 8, 10);
  public void testSimpleBitemporal () {
    assertEquals("jul1 as at aug 1", worcester, martin.getAddress(jul1, aug1));
    assertEquals("jul1 as at aug 10",worcester, martin.getAddress(jul1, aug10));
    assertEquals("jul1 as at now",worcester, martin.getAddress(jul1));


    assertEquals("jul15 as at aug 1", worcester, martin.getAddress(jul15, aug1));
    assertEquals("jul15 as at aug 10",franklin, martin.getAddress(jul15, aug10));
    assertEquals("jul15 as at now",franklin, martin.getAddress(jul15));
  }
Just as you can implement much of the complexity of temporal properties by using a temporal collection, similarly you can define a bi-temporal collection to handle much of the complexities of bi-temporal properties.


Essentially a bi-temporal collection is a temporal collection whose elements are temporal collections. Each temporal collection is a picture of record history.


We'll look at how you get information out of the collection first. The latest temporal collection represents the actual history whose record date is now. So a getting method that only has a actual date uses this current actual history.


class BitemporalCollection...
  private SingleTemporalCollection contents = new SingleTemporalCollection();
  public BitemporalCollection() {
    contents.put(MfDate.today(), new SingleTemporalCollection());
  }
  public Object get(MfDate when) {
    return currentValidHistory().get(when);
  }
  private SingleTemporalCollection currentValidHistory() {
    return (SingleTemporalCollection) contents.get();
  }
(The class SingleTemporalCollection is just the vanilla temporal collection I discussed above, I'll explain the relationship between the two later.)


To to get a true bi-temporal value, we use a getter with both actual and record dates.


class BitemporalCollection...
  public Object get(MfDate validDate, MfDate transactionDate) {
    return validHistoryAt(transactionDate).get(validDate);
  }
  private TemporalCollection validHistoryAt(MfDate transactionDate) {
    return (TemporalCollection) contents.get(transactionDate);
  }
We can then use the bi-temporal collection in a domain class.


class Customer...
  BitemporalCollection addresses = new BitemporalCollection();
  public Address getAddress(MfDate actualDate) {
    return (Address) addresses.get(actualDate);
  }
  public Address getAddress(MfDate actualDate, MfDate recordDate) {
    return (Address) addresses.get(actualDate, recordDate);
  }
  public Address getAddress() {
    return (Address) addresses.get();
  }
Each time we update the collection, we need to keep the old copy of the actual history.


class BitemporalCollection...
  public void put(MfDate validDate, Object item) {
    contents.put(MfDate.today(), currentValidHistory().copy());
    currentValidHistory().put(validDate,item);
  }
  public void put(Object item) {
    put(MfDate.today(),item);
  }
The bi-temporal collection supports a very similar interface to a one dimensional temporal collection. So we can make an interface for temporal collection and provide separate implementations for the one dimensional and two dimensional collections. This allows bi-temporal collections to be substitutable for one dimensional temporal collections, which makes it easy to refactor a single dimensional temporal property into a bi-temporal property.


In this case I'm using Time Point with a date granularity for both the actual and record dates. Although this makes the example simpler to write, it may be better to use a finer granularity for the record time. The issue here is that if you modify a record at 2pm, run a billing process at 3pm and then modify it again at 4pm. With the current implementation the appropriate value, while not completely lost, is not easy to get to. Another approach, often used in business, is only to do things like billing at the end of a business day and not to allow any further updates on that day after billing is done. Post billing updates are then given a record date of the day after billing is done. Again this kind of functionality can easily be added to the appropriate temporal collection classes.
下载前可以先看下教程 https://pan.quark.cn/s/a426667488ae 标题“仿淘宝jquery图片左右切换带数字”揭示了这是一个关于运用jQuery技术完成的图片轮播机制,其特色在于具备淘宝在线平台普遍存在的图片切换表现,并且在整个切换环节中会展示当前图片的序列号。 此类功能一般应用于电子商务平台的产品呈现环节,使用户可以便捷地查看多张商品的照片。 说明中的“NULL”表示未提供进一步的信息,但我们可以借助标题来揣摩若干核心的技术要点。 在构建此类功能时,开发者通常会借助以下技术手段:1. **jQuery库**:jQuery是一个应用广泛的JavaScript框架,它简化了HTML文档的遍历、事件管理、动画效果以及Ajax通信。 在此项目中,jQuery将负责处理用户的点击动作(实现左右切换),并且制造流畅的过渡效果。 2. **图片轮播扩展工具**:开发者或许会采用现成的jQuery扩展,例如Slick、Bootstrap Carousel或个性化的轮播函数,以达成图片切换的功能。 这些扩展能够辅助迅速构建功能完善的轮播模块。 3. **即时数字呈现**:展示当前图片的序列号,这需要通过JavaScript或jQuery来追踪并调整。 每当图片切换时,相应的数字也会同步更新。 4. **CSS美化**:为了达成淘宝图片切换的视觉效果,可能需要设计特定的CSS样式,涵盖图片的排列方式、过渡效果、点状指示器等。 CSS3的动画和过渡特性(如`transition`和`animation`)在此过程中扮演关键角色。 5. **事件监测**:运用jQuery的`.on()`方法来监测用户的操作,比如点击左右控制按钮或自动按时间间隔切换。 根据用户的交互,触发相应的函数来执行...
垃圾实例分割数据集 一、基础信息 • 数据集名称:垃圾实例分割数据集 • 图片数量: 训练集:7,000张图片 验证集:426张图片 测试集:644张图片 • 训练集:7,000张图片 • 验证集:426张图片 • 测试集:644张图片 • 分类类别: 垃圾(Sampah) • 垃圾(Sampah) • 标注格式:YOLO格式,包含实例分割的多边形点坐标,适用于实例分割任务。 • 数据格式:图片文件 二、适用场景 • 智能垃圾检测系统开发:数据集支持实例分割任务,帮助构建能够自动识别和分割图像中垃圾区域的AI模型,适用于智能清洁机器人、自动垃圾桶等应用。 • 环境监控与管理:集成到监控系统中,用于实时检测公共区域的垃圾堆积,辅助环境清洁和治理决策。 • 计算机视觉研究:支持实例分割算法的研究和优化,特别是在垃圾识别领域,促进AI在环保方面的创新。 • 教育与实践:可用于高校或培训机构的AI课程,作为实例分割技术的实践数据集,帮助学生理解计算机视觉应用。 三、数据集优势 • 精确的实例分割标注:每个垃圾实例都使用详细的多边形点进行标注,确保分割边界准确,提升模型训练效果。 • 数据多样性:包含多种垃圾物品实例,覆盖不同场景,增强模型的泛化能力和鲁棒性。 • 格式兼容性强:YOLO标注格式易于与主流深度学习框架集成,如YOLO系列、PyTorch等,方便研究人员和开发者使用。 • 实际应用价值:直接针对现实世界的垃圾管理需求,为自动化环保解决方案提供可靠数据支持,具有重要的社会意义。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值