Abstract Factories

I was designing an application’s Data Access Layer when I realized that the Objects that needed to be persisted can rapidly grow. This meant that I can’t use Binary or XML Serialization due to memory limits, but I can’t use a full relational database either since some of the objects needed to be stored in the memory for quick access. So my only options are memcached key-value database, a combination of relational (for long term data) and Binary/XML serialization (for short term data) or use Db4o plus a clever design pattern called “Abstract Factories”. Of course I went with the simplest path (for me, at least).

Problem: You need to persist (save to storage) a number of related objects.

Solution: You can use Abstract Factory pattern in conjunction with Db4o.

AbstractFactoryPatternExample

Design Pattern: Abstract Factory pattern
Pattern Type: Creational
First Stated in: http://en.wikipedia.org/wiki/Design_Patterns_(book)
Description: the abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme (eg. related to each other)

First, I encapsulated all of the Db4o stuff in the Abstract Classes. The abstract server factory act as the databases while the abstract client products are basically tables, or the enumerations of the abstract data. Now, why are all of them abstractions? So that when there’s a new database, you only need to inherit the ServerFactory and it automagically works~! Same goes for the other abstractions. In fact, you can add properties to the concrete data objects (eg. add quantity to OrangeData and/or price to GrapeData) and all of it would be persisted!

As you can see in the attached C# Console solution below, the actual business logic are totally oblivious to the Db40 stuff going on in the background, while all of the Server/Client/Data objects just inherited the Data Access Layer classes in the AbstractFactory+Db4o library. Tinker with the app, add properties to the Data, add more servers/clients and see how powerful this combination of technologies really are.AbstractFactoryPatternExample_Source2

Download Source Here: http://ow.ly/1zHeI
Download Release Here: http://ow.ly/1zHgv

0 comments:

Post a Comment