Little Computer People

Patterns for the people – III Strategy Pattern

leave a comment

If you read my blog for a while, you have encountered the pattern already; perhaps without knowing it.
The strategy pattern comes at hand, if you extract concrete behaviour out of your class, making the class more generic/abstract in doing things. If you extract behaviour you are following another (good) principle, called »Open-Closed-Principle«. We see later, what is meant by this.

I’m referring to my somehow favourite expample: Logging in application; because i think, that this is a task, with whom every developer has to deal with. So look at the example above.

Instead of implementing three time a logging mechanism within my application, I encapsulated the task in an Interface called ILogging. So every time I am in need for logging i have to wire up my application with something, that is able to provide Logging. Whether it is a real full implemented Logger or just a mockup doesn’t matter for my application. I provided a gap, which could be filled with different Objects. That makes my application flexible in respect to not depending on any special kind of Logger and likewise is my application more testable: I could provide a mockup logger and test, whether my application behaves correct with the use-case of logging.

Another example for strategy would be a tax calculator engine, where taxes were calculated for different countries. Every country has a special set of taxes; so i could build a generic application, which makes use of the different set of taxes in a different calculation strategy.

So, why is this following the »Open-Closed-Principle«?
The Open-Closed-Principle -the 2nd of the SOLID Principles, which i am dealing with in another post- says something like the following: Your Object has to be open for extension and closed for modification. What does that mean?
If we look at our tax-engine: the tax-engine is in so far closed, as the behavior of calculating different taxes in different countries is encapsulated and is fix due the interfaces, which states some kind of behavioural contract, i.e. the interface defines explicit, what actions are required from a class, implementing it. Or you could say: an interface contains a set of rules for running the game. If another class wants to provide features of calculating taxes with my engine, it has to implement the interface (obey the rules set by that interface).

On the other hand: new rules do not require to rewrite/modificate the tax-engine: Only a new behavioural class has to be implemented, to provide new features for the tax-engine. And so it is open for extension.

Written by Thomas Junk

April 6th, 2012 at 12:48 pm

Patterns for the People II – Abstract Factory

leave a comment

Question: What is an abstract factory?
Answer: A factory for factories.

It is as simple as that.
Guess, you have a family of objects. To get instances from each of the objects, you have to call the appropriate factory.
The disadvantage is obvious: you have to deal with a lot of factories.

So, at this point, the abstract factory comes in to play.
You know, you need some kind of an object, let’s say a connection to a database:
So instead, of straying around in the jungle of factories, you take only one factory, which to tell, what fatory you are looking for, and: There you are!

A simplified Version looks like this:

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");

http://msdn.microsoft.com/en-us/library/system.data.common.dbproviderfactory.aspx

Is an example of an abstract factory.
Here it is used to retrieve a factory for connections.

Written by Thomas Junk

Dezember 3rd, 2011 at 12:51 pm

Google vs. Facebook

leave a comment

Today I found a really nice Picture:

https://plus.google.com/u/0/117665613028757061169/posts

The Picture is a little bit controversial.
On the one hand you have something like massmedia-Facebook and on the other hand you have something like elitist google+.

I won’t and could not take part of this controversy, since I have no facebook account (or to paraphrase Mario Sixtus »I would like to complain about facebook, but that makes me standing with all the other sissies in the corner«).

But there is really one point. It is the point that indeed my usage of google+ is more like reading a feuilleton than chitchatting jibberjabber with my next-door-neighbour. The accounts, which i am following reflect this best. Okay, at first, I added friends and acqauints to start off with, but now I am more and more following creative minds (i would like to call them); creative people who inspire me, make me laugh and think about the world in new ways.

Written by Thomas Junk

Dezember 3rd, 2011 at 11:44 am

Posted in Tagtägliches