Analysis Martin Fowler fowler@acm acm.org http://ourworld ourworld.compuserve.com/homepages/martin_fowler Page Martin Fowler 9//99 What we will cover Introduction Observation Accounting How to use Page 2 Martin Fowler 9//99
projects fail despite the latest technology for lack of ordinary solutions. Ralph Johnson and Ward Cunningham Gang of Four : Design book Pattern Languages of Programming (PLoP) conferences A pattern is an idea that has been useful in one practical context and will probably be useful in others. [Fowler] http://www.hillside.net/patterns Page 3 Martin Fowler 9//99 Design Pattern: Mediator Mediator Colleague Concrete Colleague Concrete Mediator Concrete Colleague Define an object that encapsulates how a set of objects interact. Gamma, E., Helm, R., Johnson, R. and Vlissides, J. Design : elements of reusable object-oriented software, Addison-Wesley, Reading, MA, 995. www.industriallogic.com/papers/learning.html Page 4 Martin Fowler 9//99 2
David Hay Relational data models Not connected with patterns community, but still patterns Hay, D. Data Model : conventions of thought, Dorset House, New York, NY, 996. Martin Fowler OO conceptual models Healthcare, Accounting, Financial Trading, Planning Fowler, M. Analysis : reusable object models, Addison-Wesley, Reading MA, 997. Analysis Page 5 Martin Fowler 9//99 Notation Each customer may have many orders Each order has a single customer Order Customer placedon : Date dispatch() Priority Order Individual Customer Corporate Customer A customer may be a corporate customer or an individual customer An order may be a priority order Interface not implementation Martin Fowler with Kendall Scott, UML Distilled: Applying the Standard Object Modeling Language, Addison-Wesley 997 Page 6 Martin Fowler 9//99 3
application in 3 tier architecture application 2 domain database application 3 Visibility is from application to domain Domain carries shared services Applications do not see database used in domain tier Page 7 Martin Fowler 9//99 Observation Introduction Observation Accounting How to use Page 8 Martin Fowler 9//99 4
Attributes of Person Person height : Number weight : Number blood glucose level : Number What are the problems with this model? Page 9 Martin Fowler 9//99 Quantity Quantity Person height : Quantity weight : Quantity blood glucose level : Quantity amount: Number units: Unit +, -, *, / <, >, =, <=, >= as(unit) : Quantity tostring() : String valueof(string) : Quantity Define arithmetic operations for quantity Money is a kind of quantity Use Conversion Ratio to convert to other units What if there are tens or hundreds of attributes? Page 0 Martin Fowler 9//99 5
Measurement Martin Fowler is six feet tall Phenomenon Type Person Measurement amountof(phenomenontype) : Quantity amount: Quantity For each attribute of person, define an instance of phenomenon type To record a value for a person create a measurement When should we use this model? Page Martin Fowler 9//99 Observation Phenomenon Phenomenon Type Observation {overlapping} Category Observation Measurement amount: Quantity Person amountof(phenomenontype) : Quantity valueof(phenomenontype) : Phenomenon Martin Fowler is blood group O Martin Fowler is Male Page 2 Martin Fowler 9//99 6
Knowledge and Operational Levels Knowledge Associative Function arguments result Phenomenon Operational Person Observation Category Observation Operational regular day to day objects Knowledge Meta-Data Objects that capture domain rules Page 3 Martin Fowler 9//99 Range Range upper: Magnitude lower: Magnitude isupperinclusive: Boolean islowerinclusive: Boolean includes (Magnitude) : Boolean overlaps (Range) : Boolean abuts (Range) : Boolean Magnitude = < =< > >= Between 0 miles and 50 miles 6m < x 5m Page 4 Martin Fowler 9//99 7
Phenomenon with Range Range Phenomenon {phenomena's ranges should not overlap} Phenomenon Type Normal Heart Rate is 60 80 beats per minute Page 5 Martin Fowler 9//99 Assigning a phenomenon new a Measurement heart rate: Phenomenon Type normal heart rate: Phenomenon fast heart rate: Phenomenon phenomenon = phenomenonincluding(ameasurement) includes (ameasurement) false includes (ameasurement) true fastheartrate setphenomenon(fastheartrate) Page 6 Martin Fowler 9//99 8
Corporate Finance Problem Each month we get a large number of indicators (revenue, income, units sold ) Each indicator is given values for good, reasonable, poor, or bad If revenue is over 05 million that is good, over 95 is reasonable, over 90 is poor and under 90 is bad. Indicators can be calculated based on other indicators Revenue = income - costs Income = units sold * average price Page 7 Martin Fowler 9//99 Measurement Protocol Measurement Measurement Protocol {list} Phenomenon Type Source Measurement Protocol Calculated Measurement Protocol Source Protocols read from external systems Calculated Protocols are calculations Page 8 Martin Fowler 9//99 9
Calculated Measurement Measurement Protocol result type Phenomenon Type Formula Calculated Measurement Protocol argument types {list} Calculated measurement protocol has formula and arguments Revenue = Income - Costs Income = Number sold X Average Price Body Mass Index = weight / height 2 Page 9 Martin Fowler 9//99 Creating a Measurement a Measurement Protocol a Formula create Measurement find arguments execute return quantity new a Measurement Factory method for measurements Page 20 Martin Fowler 9//99 0
Observation Quantity Measurement Observation Range Phenomenon with Range Measurement Protocol Page 2 Martin Fowler 9//99 Accounting Introduction Observation Accounting How to use Page 22 Martin Fowler 9//99
Account Account balance: Money Entry amount: Money whencharged: Timepoint whenbooked: Timepoint {balance = sum(entries)} Entries record history of changes to an account Where might we use this model? Page 23 Martin Fowler 9//99 Transaction Account Entry amount: Money 2.. Transaction {sum(entries.amount) = 0} I withdraw $00 from my checking account The Willenhall Coal Company delivers 30 tons of coal to Bilston Gasworks Why are transactions useful? Page 24 Martin Fowler 9//99 2
Posting Rule (simple) Account trigger output Posting Rule multiplier: Number execute() 5% of state tax is deducted from gross income Page 25 Martin Fowler 9//99 Individual Instance Method Posting Rule calculation Method output trigger Account Income tax is deducted at 5% for first $3,500 28% to $80,750 3% to $30,800 Each instance of posting rule has its own method Conceptual statement Page 26 Martin Fowler 9//99 3
Account Types output Account Type trigger Knowledge Posting Rule Method Operational Employee Account Employee Apply the graduated tax algorithm to the gross pay of Doug and Dinsdale Piranha to yield their net pay. Page 27 Martin Fowler 9//99 Network of Posting Rules 3 hours (Tuesday) raw Overtime: Account Type OT hours PR straight += input if (Sunday) excess += input else excess += input * 0.5 straight OT hours : 3 hours.5 hours Account Type Excess Hours: Account Type Straight OT Payment : Posting Rule out = input * employee.rate Excess Payment: Posting Rule excess = input * employee.otrate * 2/3 premium = input * employee.otrate * /3 $50 Straight Dollars : Account Type Excess Hours: $50 $25 Account Type Premium Hours: Account Type $225 Total Overtime : Summary Account Type Page 28 Martin Fowler 9//99 4
Executing the network a list of Posting Rules a Posting Rule an Employee input Account output Account pay (employee) *[for all elements] run (employee) get account (input account type) get unprocessed entries * [for each entry] execute posting rule post entry Page 29 Martin Fowler 9//99 Procedural Approach H H Process Hours Get straight hours account Dollarize Straight Hours get unprocessed entries [for all entries] Dollarize Excess Hours multiply by rate H post to straight dollars Page 30 Martin Fowler 9//99 5
Changing the Rules H H Process Hours Get straight hours account Dollarize Straight Hours get unprocessed entries [for all entries] Dollarize Excess Hours [before April 5] [on or after April 5] H multiply by rate multiply by rate * 0.9 post to straight dollars Page 3 Martin Fowler 9//99 Adding an effectivity time output Account Type trigger Posting Rule effectivity: Date Range Method Employee Account Employee Add a date range for each posting rule instance Page 32 Martin Fowler 9//99 6
New Behavior a list of Posting Rules a Posting Rule an Employee input Account output Account pay (employee) *[for all elements] run (employee) get account (input account type) get unprocessed entries in (date range) * [for each entry] execute posting rule post entry Page 33 Martin Fowler 9//99 Implementing with singleton State Tax PR state: State {Singleton} CA Tax PR state: State {Singleton} MA Tax PR state: State {Singleton} NY Tax PR state: State What are the strengths and weaknesses? Page 34 Martin Fowler 9//99 7
State Tax PR state: State {abstract} Strategy Pattern Calculation Strategy «Method» return strategy. {Singleton} CA Tax {Singleton} MA Tax {Singleton} NY Tax What are the strengths and weaknesses? Page 35 Martin Fowler 9//99 Parameterized Method State Tax PR state: State rate: Number «Method» multiply by rate What are the strengths and weaknesses? Page 36 Martin Fowler 9//99 8
Combining Implementations State Tax PR state: State {Singleton} CA Tax PR Multiplier PR rate: Number {Singleton} NYTax PR Combination of singleton and parameterized approaches Page 37 Martin Fowler 9//99 Posting Rule Execution Eager firing Fire when entry added to account Find all posting rules looking at that account and fire them (etc, etc) Posting rule based Have a coordinator which tells posting rules when to fire Account based based Have a coordinator tell which accounts when to fire their outbound posting rules Backward Chained When you ask an account for its balance All input accounts must be asked for value (etc, etc) Which would you choose? Page 38 Martin Fowler 9//99 9
Accounting Account Transaction Posting Rule Individual Instance Method Posting Rule Execution Posting Rules for Many Accounts Accounting Practice Page 39 Martin Fowler 9//99 How to use Introduction Observation Accounting How to use Page 40 Martin Fowler 9//99 20
Using Starting point for development Inspiration to get things going Comparison for review Why are we different to this pattern? Documentation of complex frameworks Highlight the key patterns Informal standardization Mining from legacy systems Training The next step after basic training The why is at least as important as the what Page 4 Martin Fowler 9//99 Learning about Read the books Use a book group Discuss two chapters a week over lunch Take a training course Training must emphasize using patterns Instructors expertise is central Try using the patterns You cannot understand a pattern till you have tried it Trying a pattern and finding it does not fit is valuable Write some patterns http://www.hillside.net/patterns Page 42 Martin Fowler 9//99 2
Books Beck, K. Smalltalk Best Practice. Volume : Coding, Prentice Hall, Englewood Cliffs, NJ, 997. Buschmann, F., Meunier, R., Rohnert, H., Sommerlad, P. and Stal., M. Pattern-Oriented Software Architecture - A System of, John Wiley, 996. Coad, P., North, D. and Mayfield, M. Object Models: strategies, patterns and applications, Prentice Hall, Englewood Cliffs, 995. Coplien, J.O. and Schmidt, D.C. Pattern Languages of Program Design, Addison-Wesley, Reading, MA, 995. Gamma, E., Helm, R., Johnson, R. and Vlissides, J. Design : elements of reusable object-oriented software, Addison-Wesley, Reading, MA, 995. Fowler, M. Analysis, Addison-Wesley, Reading MA, 997 Hay, D. Data Model : conventions of thought, Dorset House, New York, NY, 996. Vlissides, J.M., Coplien, J.O. and Kerth, N.L. ed.pattern Languages of Program Design 2, Addison-Wesley, 996. http://www.hillside.net/patterns Page 43 Martin Fowler 9//99 Final Thoughts are relevant for all aspects of software engineering and for Business Process Reengineering Analysis patterns can be used across traditional vertical business domains A model is not right or wrong, only more or less useful are a starting point, not a final destination Still learning about teaching, using, and discovering patterns Page 44 Martin Fowler 9//99 22