Analysis Patterns

Martin Fowler

Mentioned 19

In his long-awaited book, Martin Fowler has done for application domain patterns what the Gang of Four [Gamma et al.] have done for general purpose design patterns in their book, Design Patterns: Elements of Reusable Object-Oriented Software. This book is a must have for all analysts and designers doing object-oriented business modeling and business process re-engineering. —Donald G. Firesmith, Knowledge Systems Corporation Fowler shares with you his wealth of object modeling experience and his keen eye for identifying repeating problems and transforming them into reusable models. Analysis Patterns provides a catalogue of patterns that have emerged in a wide range of domains, including trading, measurement, accounting, and organizational relationships.

More on Amazon.com

Mentioned in questions and answers.

Are there any good resources (books, authoritative guides, etc.) for design patterns or other best practices for software that includes financial accounting features?

Specifically, where is good information about handling issues like the following:

  • Internal representations of money quantities
  • Internal representations of accounts, journals, and other records
  • Reconciling inconsistencies (either automatically or via user action)
  • Handling ends of accounting periods (daily, weekly, monthly)
  • Designing UIs and printed financial reports that make sense to businesspeople

Note: "Authoritative" or otherwise widely-accepted information is what we're looking for here. Otherwise, this will just turns into a big list of anecdotes of all the things people have tried, making the topic very subjective.

I find the Data Model Resource book to be a good source of inspiration for modeling business structures. Apache Ofbiz ERP was built around the concepts in this book.

Martin Fowler's Analysis Patterns covers some of those topics.

When writing the following today in C#

DateTime.Now.AddYears(-60)

I wondered whether there are any languages that allow a more natural syntax with units:

DateTime.Now - 60years

Does anyone know of any? Specifically, I'm interested in the presence of unit operators(?) that turn "60years" into e.g. "TimeSpan.FromYears(60)". It'd also be neat to be able to define your own unit operators, similar to how you can write conversion operators in C#

(Yes, I know TimeSpan doesn't cater for years -- it's an example.)

Java's JODA library works that way.

And there's JSR-275 that proposes a units framework.

I first heard about this issue back in 1997 from Martin Fowler. He wrote about it in his "Analysis Patterns".

I just started working for a government agency and I have come across an interesting problem: business rules depend on legislature and as such they have to respect the exact time periods that legislature has been active.

To give you an example, if a subject has applied for a subsidy on a certain date, than he has to be evaluated according to criteria that vas valid on that given date. That same subsidy, for someone that applied on some later date has different criteria. I was wondering if there is a known pattern to deal with these time-dependent rules in orderly fashion. At the moment, the code is sprinkled with expressions akin to:

if application.date >”July 17th, 2008”

What is the best way to manage this problem?

You can use a few patterns. For example using the temporal property pattern you can create on object which contains business rules which are active at a certain point in time. Also, using the specification pattern you can create business rules which are effective based on a certain date. You can associate an effective date range with each policy so that the decision of whether a given policy applies can be made. The book Analysis Patterns contains many patterns that could be applicable in your scenario.

EDIT: I meant to link to the temporal object pattern instead of temporal property. This may shed some light on implementing the database mapping.

I am looking for good resources (books/web sites) for learning object oriented design. Every resource that I find are tutoring me more on UML and RUP instead of OO design. Head first book's sheer repetition is making me not want to read any of their books. I am looking for a book similar to "Structure and interpretation of computer programs" for object oriented design that gets to the point of teaching OO. I have no preference for any specific OO laguage.

Also as replacement for the Gang of Four book.

I can recommend: The Design Patterns Smalltalk Companion

In general learning Smalltalk will help you to be a better OOP Developer on any language.

From the Amazon reviews:

Easier to understand than the original GoF, February 4, 2000 By Nicolas Weidmann
This book gives you a better understanding of the patterns than in its original version (the GoF one). I am not a SmallTalk programmer but a 9 years C++ one. At work I had to use the GoF book and never liked reading it. In contrast to this, the SmallTalk companion is easy to read and you can understand the patterns within the first few lines of their description. Take the Bridge pattern and compare their discussions in the two books. If you really like the Gof one then buy it. But according to me, it would be a big mistake buying the GoF in favour of the SmallTalk companion. Trust a C++ programmer :-)

Object-Oriented Analysis and Design with Applications by Grady Booch is the bible for this topic. It is also very approachable though somewhat dense at points, but definitely worth reading and re-reading.

Quoting myself from another answer on the same topic:

Great resources to learn how to think in patterns and do correct OOP analysis and design are Analysis Patterns: Reusable Object Models by Martin Fowler and Applying UML and Patterns by Craig Larman. Also I need to mention here Domain-Driven Design: Tackling Complexity in the Heart of Software by Eric Evans, the most valuable book I found to think about the whole software design process.

I'm writing a game where the mouse-driven controller object clicks on a player object to have it do something.

There are 2 ways of initiating the interaction between mouse and player:

  • Controller calls player's function:
    Controller listens for mouse events. When a mouse click occurs anywhere onscreen, the controller searches all objects under the point that was clicked. If one of the objects is a player object, and it's "clickable" property is true, then call its appropriate function.
  • Player calls controller's function:
    Player listens for mouse events. When a mouse click occurs on the player, and the player's own "clickable" property is true, then call the controller's appropriate function.

My dilemma here is that the first option seems more intuitive with how I imagine the scenario happening in the real world, but the second option seems more intuitive with proper object-oriented design because it doesn't require looking into another object's property, which violates encapsulation to some extent (the controller must look into the player to read its "clickable" property). Also, the second option seems inline with the 'Controller' design pattern.

This is always a struggle for me -- do I defy proper object-oriented design (e.g. option 1) or do I use an implementation that seems counterintuitive to the real world (e.g. option 2)?

I'm hoping there's some kind of middle ground I'm missing.

This is always a struggle for me -- do I defy proper object-oriented design (e.g. option 1) or do I use an implementation that seems counterintuitive to the real world (e.g. option 2)?

Reality may be a good starting point for molding or evolving a design, but it is always a mistake to model an OO design to reality.

OO design is about interfaces, the objects that implement them, and the interaction between those objects (the messages they pass between them). Interfaces are contractual agreements between two components, modules, or software sub-systems. There are many qualities to an OO design but the most important quality to me is substitution. If I have an interface then the implementing code better adhere to it. But more importantly, if the implementation is swapped then the new implementation better adhere to it. Lastly, if the implementation is meant to be polymorphic then the various strategies and states of the polymorphic implementation better adhere to it.

Example 1

In mathematics a square is a rectangle. Sounds like a good idea to inherit class Square from class Rectangle. You do it and it leads to ruin. Why? Because the client's expectation or belief was violated. Width and height can vary idependently but Square violates that contract. I had a rectangle of dimension (10, 10) and I set the width to 20. Now I think I have a rectangle of dimension (20, 10) but the actual instance is a square instance with dimensions (20, 20) and I, the client, am in for a real big surprise. So now we have a violation of the Principle of Least Surprise.

Now you have buggy behavior, which leads to client code becoming complex as if statements are needed to work around the buggy behavior. You may also find your client code requiring RTTI to work around the buggy behavior by testing for conrete types (I have a reference to Rectange but I have to check if it is really a Square instance).

Example 2

In real life animals can be carnivores or herbivores. In real life meat and vegetables are food types. So you might think it is a good idea to have class Animal as a parent class for different animal types. You also think it is a good idea to have a FoodType parent class for class Meat and class Vegetable. Finally, you have class Animal sport a method called eat(), which accepts a FoodType as a formal argument.

Everything compiles, passes static analysis, and links. You run your program. What happens at runtime when a sub type of Animal, say a herbivore, recieves a FoodType that is an instance of the Meat class? Welcome to the world of covarience and contravarience. This is a problem for many programming languages. It's also an interesting and challenging problem for language designers.

In Conclusion...

So what do you do? You start with your problem domain, your user stories, your use cases, and your requirements. Let them drive design. Let them help you discover the entities you need to model into classes and interfaces. When you do you'll find that the end result isn't based on reality.

Check out Analysis Patterns by Martin Fowler. In there you'll see what drives his Object Oriented designs. It is mainly based on how his clients (medical people, financial people, etc.) perform their daily tasks. It has overlap with reality, but it isn't based or driven by reality.

According to Applying UML and Patterns (Craig Larman) the User Interface (your mouse events) should never interact with your application classes, that is, the user interface should never drive the business logic directly.

Instead one or several controllers should be defined to act as a middle layer to the user interface, so option 1 does in fact follow a good object oriented approach.

If you think about it, it does make sense to couple as fewer classes as possible to the UI in order to make business logic as independent of the UI as possible.

I'm working my way through Fowler's Analysis Patterns and programming examples for myself in Clojure as way of developing a better understanding of both.

Putting persistence/durability issues to the side for the moment1, it seems that Clojure refs with their synchronization would be the obviously best approach.

On the other hand, given Posting Rules that are triggered by entries into the account, producing more transactions in yet more accounts, perhaps agents, and their asynchronous updates would be better. At this point it looks like I might have to try a bit of both. Anybody have any suggestions on this particular design decision?

1 I'm assuming if I can get the functionality and concepts working nicely, I'll be able to map everything into a sensible DB schema later.

All of the mutable objects in Clojure are designed to give you a consistent view of how things change over time. If you have multiple objects changing in relation to each other then you need to create a timeline that includes all of them and refs are designed for just that purpose.

you may also be interested in the concept of watchers (add-watch reference key fn) that you can add to refs to enforce transaction invariants.

I am working on a simple invoicing and accounting applications, but I am totally lost on how to represent accounting-transactions(journal-entries) in a database. Using a separate debit and credit column in the database seems like one way, but most of the Open Source Accounting I have seen use a single amount column. This I assume simplifies the mathematics a lot. But then how do I convert debits, and credits to numerical values. Wikipedia Helped a little, but when i tried to cross check with an accounting system, it doesn't looks how it's done.

Here's the export from that accounting system:

Take a look at journal 326. While the sum of amount in this case = 0, the sum of credits isn't equal to sum of debits (Debiting 29 from Consulting & Accounting(E), Debiting 31.39 from AP(L), and crediting 2.39 to Sales Tax(L)).

However if I look at it as Debiting -31.39 from AP, It does. However I am not sure if we could credit/debit negative values.

Can someone explain how databases and accounting principles go together?

Martin Fowler's "Analysis Patterns" has a nice chapter on modeling accounting systems. Maybe it can help you.

I think you'll be better off thinking about the problem in terms of objects rather than trying to map it onto a relational database. Databases are declarative and set-based; objects encapsulate data with operations in components. I think the latter will lend itself better to modeling accounting, especially if you marry it with aspect-oriented programming. Let the database just be the way you persist things and keep the logic in the middle tier.

I'm looking for a resource that shows common design patterns or best practices for data models. For instance, a [very simplified] course model might have:

Student has many Enrollments

Course has many Sections

Sections has many Enrollments

There seem to be many approaches to setting up data models and relationships, and some are more obvious than others. Often, I find myself thinking about it one way and finding out much later that there is a quirk or complication I missed.

For more complex systems, like ERP, Reservations, eCommerce, etc., there must be some standards and best practices but I can't find them anywhere. Any resource suggestions would be welcome. I've googled around quite a bit, but I didn't see any that made sense.

This is a copy of my answer on DBA.

David Hay's

Len Silverston's

Michael Blaha's Patterns of Data Modeling. This one has some interesting temporal, graph, and tree models.

Martin Fowler's Analysis Patterns. This one skims some of the other patterns, but gives accounting a solid treatment.

They are all well-rated, and I have read all but one, and they are all very good. Several of them are available on safaribooksonline.

Also, OASIS's Universal Business Language, schemas

This is in continuation with this question I asked a yesterday. After going through various resources and consulting people, I was not able to find any JPA annotations supporting API, for mapping units of measurement. So, I decided to go with creating it myself.

Based on various patterns on Observations and Measurements described by Martin Fowler in his book - Analysis Patterns: Reusable Object Models, I tried to create a basic implementation to meet my needs. I've created two entities, Unit and Quantity, as below: -

Unit entity: -

@Entity
@Table(name = "unit")
public class Unit {

    @Id
    @Column(name = "symbol")
    private String symbol;

    @Column(name = "name")
    private String name;

    @Column(name = "multiplier")
    private Number multiplier;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "symbol")
    private Unit baseUnit;

    public Unit() {

    }

    public Unit(String symbol, String name, Number multiplier, Unit baseUnit) {
        this.symbol = symbol;
        this.name = name;
        this.multiplier = multiplier;
        this.baseUnit = baseUnit;
    }

        /** Getters and Setters **/
}

Quantity Entity: -

@Entity
@Table(name = "quantity")
public class Quantity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int quantityId;

    private Number amount;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "unit")
    private Unit unit;

    public Quantity() {

    }

    public Quantity(Number amount, Unit unit) {
        this.amount = amount;
        this.unit = unit;
    }

    /** Getters and Setters **/
}

And in Item entity, I'm having a reference of Quantity entity, to represent my unitOfIssue and unitOfPurchase. And then, wherever I've to deal with some quantity, I'll have this Quantity reference.

Now, here's the problem I'm facing now. Since Quantity class here will be an entity. So, we have to map it to a DB Table. So, the caveat here is, everytime I want to add some quantity, or update the quantity, an entry will go in the Quantity table first, and then it will be updated in the Item table. I think, the problem is pretty clear. We would have huge entry in the Quantity table, which clearly shows a bad design.

Can someone give an insight on the approach and what options I've while I implement this pattern in JPA? And how can I solve the problem?

I would recommend this instead. Since units of measure are unlikely to change often, it is worth building them into the code. That way you can meaningfully use the values in the program itself.

It also makes the database more cohesive if it will ever be used anywhere else. You can also extend Quantity to be things LinearQuantity, ArealQuantity, VolumetricQuantity (etc.) to make sure someone isn't trying to buy 30 feet of oil.

@Embeddable
public class Quantity{

   public enum Unit {FEET,METERS,INCHES,MM}

   @Enumerated( value = EnumType.STRING)
   private Unit unit;

   private Number amount;

   public Quantity() {

   }
}


@Entity
Public Class PurchaseOrder
{
    @Embedded
    @AttributeOverrides({
      @AttributeOverride(name="unit", column=@Column(name="UNIT")),
      @AttributeOverride(name="amount", column=@Column(name="AMOUNT"))
    })
    private Quantity quantity;
    ....

}

I am currently trying to draw a set of UML diagrams to represent products, offers, orders, deliveries and payments. These diagrams have probably been invented by a million developers before me.

  1. Are there any efforts to standardize the modeling of such common things? Or even the modeling of specific domains (for example car-manufacturing).
  2. Do you know if there is some sort of repository containing UML diagrams (class diagrams, sequence diagrams, state diagrams...)?

There is a movement for documenting (as opposed to standardizing) models for certain domains. These are called analysis patterns and is a term Martin Fowler came up with. He actually wrote a book called Analysis patterns. Also, he has a dedicated section on his website where he presents some of these patterns accompanied by UML diagrams.

Maybe you'll find some inspiration that will help you in modeling your domain. I've stressed the word inspiration as I think different businesses have different requirements although they operate the same domain so the solutions you might read about may not be appropriate for your problem.

I am thinking through a nice pattern to be useful across domains of measurable units (ie, Length, Time) and came up with the following use case and initial classes, and of course, questions!

1) Does a Composite pattern help or complicate?
2) Should the Convert method(s) in the ComposityNode be a separate converter class?

All comments appreciated. Cheers,
Berryl

Example Use Case:

var inch = new ConvertableUnit("inch", 1)
var foot = new ConvertableUnit("foot", 12)
var imperialUnits = new CompositeConvertableUnit("imperial units", .024)
imperialUnits.AddChild(inch)
imperialUnits.AddChild(foot)

var meter = new ConvertableUnit("meter", 1)
var millimeter = new ConvertableUnit("millimeter ", .001)
var imperialUnits = new CompositeConvertableUnit("metric units", 1)
imperialUnits.AddChild(meter)
imperialUnits.AddChild(millimeter)

var oneInch = new Quantity(1, inch);
var oneFoot = new Quantity(1, foot);
oneFoot.ToBase() //  "12 inches"

var oneMeter = new Quantity(1, meter);
oneInch.ToBase() //  .024 meters

Possible Solution

ConvertableUnit : Node  
  double Rate
  string Name

Quantity
  ConvertableUnit Unit
  double Amount

CompositeConvertableUnit : Node
  ISet<ConvertableUnit> _children
  ConvertableUnit BaseUnit {get{ return _children.Where(c=>c.Rate == 1).First() } } 
  Quantity ConvertTo(Quantity from, Quantity to)
  Quantity ToBase(Quantity from);

Martin Fowler has a very well thought out model for measurements and conversions and such in Analysis Patterns. Worth reviewing. I believe he recommended a Conversion Ratio object that would handle converting from one unit to another.

I'm kicking around a small database project (sqlite or mysql) for learning purposes... figured I'd work on cataloging some of my many books ;)

Thought I had most of the tables and relationships worked out, until I started going thru and populating with some sample data from a particular book series. One of the authors writes under a pen name in this series/genre, but under a completely different name for another genre, and his 'actual' name is something else entirely. Add to the fun that sometimes the reader (me) may not be aware that the authors name on the cover is a pen name or not.

Any ideas or suggestions for how to deal with this sort of thing in practice?

TIA,

Monte

The answers from @Adam and @mellamokb do not take account of some other complications:

  1. Pen names can be used by authors
  2. An author may have zero-many pen names
  3. A pen name can belong to an author
  4. A pen name can be a house (publisher) pen name, used by zero-many authors
  5. A pen name is often adopted, when an author starts collaborating with one or more other authors, for the collaboration.
  6. As you say, it is not always obvious that a name is a pen name, so you have to make the assumption that a name is real unless you know otherwise.

Obviously, dealing with all these cases is only worth while if the number of books is large - before I disposed of my book collection (approx 15,000) I was contemplating making a shift to this sort of solution, because of pen names, and because of edited anthologies and article series.

If you wish to go down this route, a good place to start is Martin Fowler's Analysis Patterns or the IEEE standards for author descriptions for its own publications.

This is a newbie type question, but I am stuck on how to do this within the context of Core Data in iOS.

I have a data type I want to use called Measurement which consists of a unit (string) and a value (double). I have another class PhysicalAttributes which has a number of attributes some of which are primitive data types and some (most) which should be of type Measurement.

In Core Data, I made an entity called Measurement and an entity called PhysicalAttributes.

If I were not using Core Data, I would have a class like:

@interface PhysicalAttributes : NSObject

@property (nonatomic) Measurement *height ;
@property (nonatomic) Measurement *weight ;

// etc.

@end

But I cannot determine if in Core Data I should use relationships called height, weight, etc. or use a transformable data type and model it that way. It doesn't feel right to call it a relationship since Measurement is really a data type.

All the examples I can find of using core data don't really give examples of something like the above.

Any help/pointers appreciated!

Do you really want Measurement? Maybe as an abstract class or a protocol, but you probably want classes for Weight and Height, otherwise, the possible types inside Measurement are going to get really ugly.

I agree with you about it making you feel dirty to use a relationship for something like this, but, the other way to look at it is these measurements are probably being captured at some point in time, so you could add a timestamp too.

Martin Fowler's book Analysis Patterns discusses this at some length and comes down on the side of doing classes (which is correct) for these things.

Although I'm coding in Objective C, this is more of a general programming question.
What is the best way to convert a constantly changing scalar value to a changing interval or frequency?

Right now every time the scalar value changes I am destroying the NSInterval
ie

   [self.myTimer invalidate];
    self.myTimer = nil;

and creating a new one, but this seems like a VERY expensive way to achieve my goal, since the changing scalar value in my case represents the horizontal velocity of a swipe.

For a rough analogy, think of the speed of a swipe being reflected in a visual metronome, the faster you swipe, the higher(shorter interval) the frequency of the metronome.

First, understand that this is a "measurement" problem. To solve this problem, isolate the minimum attributes needed to solve the problem and derive the rest.

The distance moved and the time taken to move the distance are derived attributes of each measurement, in this case the measurement is named "swipe". In a racing analogy, the measurement is called a lap. The "speed" can now be calculated. This will be the "velocity", which is simply distance/time.

The distance can be calculated given the start and end points of the swipe. To obtain the time value, create an startTime instance of NSDate in touchesBegan:withEvents: and in touchesEnded:withEvents: calculate elapsedTimeInterval using [startTime timeIntervalSinceNow];

Depending on your needs, you may need a Measurement class with properties for startPosition, endPosition, startTime and endTime so you can keep track of "fastest" speed etc.

Take a look at Analysis Patterns by Martin Fowler. I find it very useful when trying to map domain problems to software solutions.

So I'm about to start a big project that involves clients pulling data from a webservice. The data is controlled by an application that will determine when the data becomes live or more specifically, data is manipulated directly by the application and then the user will determine when that data becomes accessible to the webservice (either immediately or at a specific date). This 'pull' technique is favored in this case as opposed to the clients determining what data to use and when.

Now I've got a few ideas on how to tackle this problem that generally involves somekind of 'updateNumber' that is applied to every object on the system and a 'scheduling' subsystem that sets the 'publish' date to a specific 'updateNumber', e.g. 05/11/2011 = 78911, so retreive every object that has an updateNumber of '78911' and below (and hasn't been deleted).

However, I'm guessing that there's somekind of design pattern that handles this pull deployment / scheduling scenario (and thus already resolved this problem) and that my Google Skills modifier is running low. Is there such as design pattern or has someone came across a similar problem is willing to provide some advice?

Thanks in advance

From what I understand after our exchange, I would model (at the DB level) this as follows:

Every Item record has a "valid-from" field expressed as a date. Value may be null (this would indicate that the specific Item has not been validated yet by a user and therefore is "pending").

In any given point T in time the "snapshot" is identified by querying the items with max(valid-from)<= current-date.

If you want to mantain the concept of "release" (maybe the actual release date is meaningful for your specific scenario, or you just want to reduce the possibility of wrong inputs) you could create a releases table which has the following structure:

Release Name | Release date
-------------|-------------
 Labor Day   | 20110501
 Summer Camp | 20110607
 2012 preview| 20111210

So the user will be able to refer to a given "release" in terms of some event or codename (while still being able to see the actual date, of course) and the corresponding date will be copied to the "valid-from" field of the validated item.

Personally I'd prefer this approach to having release names or codes at the item level, but of course my solution may be a problem in case you have two distinct "releases" on the same date, and later decide to change one of the two in the future (e.g. postpone it, maybe).

You asked for a pattern, but I am not aware of a pattern for this kind of situations. Maybe you can find something in Fowler's works (I don't remember anything for this in his book, though - check the site, too)

Rechecked his site after writing this - maybe this chapter could be of some interest.

What are the most important patterns you should know, when developing testable n-tier line of business web applications?

  • Repository pattern
  • Model View Controller
  • Dependency Injection Pattern
  • Factory Pattern
  • Singleton

What else?

"most" important?

The problem domain's patterns -- whatever they are. Accounts Payable, General Ledger, Accounts Receivable, Organizational Hierarchy.

http://www.businessanalysisbooks.com/0471295515.html

http://www.amazon.com/Analysis-Patterns-Reusable-Addison-Wesley-Technology/dp/0201895420

Technical patterns (MVC, Repository, etc.) are a dim second place.

I want to ask for your help, as I am stuck in a project I am working on for monitoring patients from their homes.

I am a PHP/MySQL developer, and if you could guide me in a right direction, it would be great. Here is what I want:

I have 3 type of users: Admin, Patients, Doctors

  1. Each user must login to use the system. So we have login information.
  2. A doctor can have under his/her responsibility many patients. Thus, he is connected to many patients. (Will check their daily measurements)
  3. A patient can be under responsibility of only one doctor.
  4. I also want to hold personal data of patients and doctors, apart from their login details. (So I can make profiles for them, but they will have different information)
  5. Will also have an internal messaging system so that logged in users can communicate.
  6. Specific(about 3-4) measurements will be stored for each patient every day. (Patients will enter those using a form)
  7. The last thing is that i want a mailing list for the patients, in order to send them if they want daily reminder emails to upload their measurements.

Please, what kind of database design do I need. How can I arrange tables and ids and stuff. If you could help me, that would be great!

Thanks in advance. Regards, Andrew.

You've already identified a number of tables and their cardinalities.

You might have a User table and different roles: Doctor, Patient, Admin, etc.

Users have credentials; that's 1:1.

Profile will have Address, ContactInfo, etc.

The measurements piece is getting tricky. I'd recommend having a look at Martin Fowler's Analysis Patterns. He has a nice chapter modeling the situation you're talking about here. It's subtle.