1 / 108

Slides before 1st Section Divider

数量 / Quantity 模式. Slides before 1st Section Divider. Unused Section Space 1. 随时间变化事物 ( Things That Change with Time) 模式. 扩展. 观察和测量模式. Unused Section Space 2. Unused Section Space 3. Unused Section Space 4. 观察和测量模式. 徐迎晓 复旦大学软件学院 xuyingxiao@126.com. 数量 /Quantity. class Person{

Download Presentation

Slides before 1st Section Divider

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 数量/ Quantity模式 Slides before 1st Section Divider Unused Section Space 1 随时间变化事物(Things That Change with Time)模式 扩展 观察和测量模式 Unused Section Space 2 Unused Section Space 3 Unused Section Space 4

  2. 观察和测量模式 徐迎晓 复旦大学软件学院 xuyingxiao@126.com

  3. 数量/Quantity • class Person{ • int height; • int weight; • … • }

  4. Java中已有的Number • Class Number

  5. 数量

  6. Quantity

  7. arithmetic.

  8. Java中已有的Unit • Currency

  9. Money • Money是数量的一种特殊实例

  10. getter • public class Money implements Comparable { • private BigInteger amount; • private Currency currency; • public double amount() { • return amount.doubleValue() / 100; • } • public Currency currency() { • return currency; • }

  11. constructor • public Money(double amount, Currency currency) { • this.amount = BigInteger.valueOf(Math.round(amount * 100)); • this.currency = currency; • } • public Money(long amount, Currency currency) { • this.amount = BigInteger.valueOf(amount * 100); • this.currency = currency; • } • public static Money dollars(double amount) { • return new Money(amount, Currency.getInstance("USD")); • }

  12. +- • public Money add(Money arg) { • assertSameCurrencyAs(arg); • return new Money(amount.add(arg.amount), currency, true); • } • public Money subtract(Money arg) { • return this.add(arg.negate()); • } • void assertSameCurrencyAs(Money arg) { • //Assert.equals("money math mismatch", currency, arg.currency); • }

  13. private Money(BigInteger amountInPennies, Currency currency, boolean privacyMarker) { • // Assert.notNull(amountInPennies); • // Assert.notNull(currency); • this.amount = amountInPennies; • this.currency = currency; • } • public Money negate() { • return new Money(amount.negate(), currency, true); • }

  14. */ • public Money multiply(double arg) { • return new Money(amount() * arg, currency); • } • public Money[] divide(int denominator) { • BigInteger bigDenominator = BigInteger.valueOf(denominator); //分母 • Money[] result = new Money[denominator]; • BigInteger simpleResult = amount.divide(bigDenominator); • for (int i = 0; i < denominator; i++) { • result[i] = new Money(simpleResult, currency, true); • } • int remainder = amount.subtract(simpleResult.multiply(bigDenominator)) • .intValue(); • for (int i = 0; i < remainder; i++) { • result[i] = result[i].add(new Money(BigInteger.valueOf(1), • currency, true)); • } • return result; • }

  15. > < • public int compareTo(Object arg) { • Money moneyArg = (Money) arg; • assertSameCurrencyAs(moneyArg); • return amount.compareTo(moneyArg.amount); • } • public boolean greaterThan(Money arg) { • return (this.compareTo(arg) == 1); • } • public boolean lessThan(Money arg) { • return (this.compareTo(arg) == -1); • } • public boolean equals(Object arg) { • if (!(arg instanceof Money)) • return false; • Money other = (Money) arg; • return (currency.equals(other.currency) && (amount.equals(other.amount))); • }

  16. 单位的转换率

  17. Class ConversionRatio{ • Unit from, to; • Number number; • }

  18. 复合单位

  19. Class CompoundUnit implement Unit{ • Unit inverse[ ], direct[ ]; //分母,分子 • }

  20. 数量模式为解决单位问题提供了有效的办法。 • 建立分析模型时不应过早地考虑设计和实现环境,而且要从概念上抓住各种细节,这样建立的分析模型才能更精确地描述需求。 • Others • 范围(Range)模式 • 随时间变化事物(Things That Change with Time)模式

  21. Patterns for things that change with time

  22. Audit Log

  23. temporal patterns • Not just do we want to know the state of the world, we want to know the state of the world six months ago • what two months ago we thought the state of the world six months ago was • Effectivity pattern • Temporal Property pattern

  24. Effectivity

  25. Wellington. On Dec 1 1999 he begins employment with India Inc

  26. Wellington. On Dec 1 1999 he begins employment with India Inc • As time continues he starts a new employment with Peninsula Inc on April 1 and ends his employment with India Inc on May 1.

  27. Temporal Property • remove the obvious effectivity date • instead you use what looks like a regular property,but with an accessor that takes a date as an argument

  28. 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); • }

  29. providing a regular and predictable interface——accessor functions —— for dealing with those properties of an object that change over time

  30. update information • additive update • single put method that takes a timepoint and a new value • change XXX's address to YYY with effect from 23 August 2007 • insertion update • we had XXX moving in to address AAA in date YYY, but we need to change that to ZZZ

  31. two ways to implement Temporal Property • collection of objects using Effectivity and then manipulate this collection • create a special collection class that provides this behavior: a temporal collection

  32. 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(); • }

  33. Temporal Property and Effectivity are not mutually exclusive patterns • You might use address usage objects (using Effectivity) to implement a Temporal Property interface • Temporal Property introduces the notion of referring to things with a time-based index, but takes it only so far as a single property. • If you have many temporal properties on an object ? • Snapshot and Temporal Object

  34. Snapshot • gives you an object that refers to the state of the real object as at a point in time • A Snapshot is simply a view of an object with all the temporal aspects removed

  35. class CustomerSnapshot... • private Customer base; • private MfDate validDate; • public CustomerSnapshot (Customer base, MfDate validDate) { • this.base = base; • this.validDate = validDate; • } • public Address getAddress() { • return base.getAddress(validDate); • }

  36. Temporal Object • two roles: one continuity and several versions • several versions • Any time the value of any property of the object changes, you get a new version • you can imagine the versions as a list of objects with an Effectivity to handle the date range. • one continuity

  37. class CustomerVersion... • private String address; • private Money creditLimit; • private String phone; • String address() {return address;} • Money creditLimit() {return creditLimit;} • String phone() {return phone;} • void setName(String arg) {_name = arg;} • void setAddress(String arg) {address = arg;} • void setCreditLimit(Money arg) {creditLimit = arg;}

  38. class Customer... • private TemporalCollection history = new SingleTemporalCollection(); • public String name() {return current().name();} • public String address() {return current().address();} • public Money creditLimit() {return current().creditLimit();} • public String phone() {return current().phone();} • private CustomerVersion current() { • return (CustomerVersion)history.get(); • }

  39. In practice Effectivity used widely • Patterns like Temporal Property and Temporal Object work well because they hide much of the mechanics of temporal behavior • However Effectivity still is important: it is the right choice when people want an explicit object that is only valid for a particular time period.

  40. Dimensions of Time • a payroll system • an employee has a rate of $ 100/day starting on January 1 • On February 25 we run the payroll with this rate • On March 15 we learn that, effective on February 15, the employee's rate changed to $ 211/day. • what the rate was for February 25? • $211 • But often we cannot ignore that on Feb 25 we thought the rate was $100,

  41. record date | actual date| Dinsdale's rate • Jan 1 Jan 1 $100/day • Feb 15 Feb 15 $100/day • Feb 25 Feb 15 $100/day • Feb 25 Feb 25 $100/day • Mar 14 Feb 15 $100/day • Mar 15 Jan 1 $100/day • Mar 15 Feb 15 $211/day • Mar 15 Feb 25 $211/day

  42. we make the corresponding adjustments in a payroll run on March 26. • On April 4 we are told that the employee's our previous information was wrong and that the rate was actually changed to $255 on February 15 • Now how do we answer the question "what was the employee's rate on February 25?". • Mar 26 Feb 25 $211/day • Apr 4 Feb 14 $100/day • Apr 4 Feb 15 $255/day • Apr 4 Feb 25 $255/day

More Related