Announcement

Showing posts with label design pattern. Show all posts
Showing posts with label design pattern. Show all posts

Sunday, December 29, 2013

Iterator : powerful and under utilized pattern

Iterator is one of my favorite design pattern. I have used in almost all projects and in various ways. I have been thinking of writing a blog on 'Iterator design pattern'. Recently (on 4th Dec 2013) I did webinar on the Iterator pattern.  The links are given below

  1. Iterator – a powerful but under-appreciated pattern - Webinar on Techgig.
    You can watch the video on youtube (video)


  2. I have published the slides on Slideshare. You get the presentation here.

Saturday, February 12, 2011

Insights from ‘The Design of Design’ - Part I

I have good fortunate of meeting/interacting with some great software designers while working in Geometric Ltd and during last two years as independent consultant. I am always intrigued by how an expert software designer thinks and how he learns. As Sir Ken Robinson says the key skill in today's world is ‘knowing how to learn new things’. I think of myself as ‘Thinking Craftsman’ (i.e. someone who is thinking about his trade/craft and strives to continuously improve his/her skills). Hence ‘how an expert designer learns and becomes an expert’ is a key question for me in the quest of improving my own skills.

The Design of Design’ is a new book from Fred Brooks (Author of Mythical Manmonth). As expected, it has some great/some obvious insights but most importantly it has great explanations of these insights. In this article, I am going to discuss about insights related to how ‘expert designers become experts’.

Insight One: Exemplars in Design
This is what Fred Books says about 'exemplars'

Exemplars provide safe models for new designs, implicit checklists of design tasks, warnings of potential mistakes and launching pads for radical new designs. Hence great designers have invested great efforts in studying their precedents. Bach took a six month unpaid leave to study the work and ideas of Buxtehude. Bach proved to be much greater composer but his surpassing excellence came from comprehending and using the techniques of his predecessors and not ignoring them

I argue that great technical designers need to do likewise but that the hurried pace of modern design has discouraged this practice. ... Technical design disciplines eager to produce great designs need to develop accessible bodies of exemplars and knowledgeable critiques of them (page 154-155)
Certainly lazy or slack designer can minimize his work by picking an exemplar and just modifying it to fit. By and large, those who just copy do not draw on ancient or remote exemplars but only on those that are most recent and fashionable’ (page 162).
There are two things that came to my mind after reading this article. First is ‘Design Patterns’. “In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.”.

One purpose of documenting the ‘design patterns in software’ is to 'make experts insights available to a novice’. In this sense, ‘Design patterns’ fit perfectly into what Fred Brooks calls ‘accessible bodies of exemplars’. Personally my thinking about the software design changed after I studied patterns (especially advantages and limitations of each pattern). Even today I read and reread GoF Design pattern book, articles by Robert Martin, articles and books of Martin Fowler, books like Effective C++ and More Effective C++. Every time I gain some small new insights which enrich my own ‘body of knowledge’. Another recent good book of exemplars is ‘Beautiful Code: Leading Programmers Explain How They Think’. There are positive and negative reviews on this book. For me, this book is invaluable for its insights into how various developers think about a problem and how they come up with a solution.
Second thought was ‘about Googling’. Recently (past few years) I see many developers just google about a problem, find something and copy that code. Many times they just pick up some pattern and copy the sample code for that pattern found on the internet. But since they don’t have any real understanding of the pattern, they end with more problems. While conducting programs on ‘design patterns’ I see participants eager to get sample code rather than eager to understand the pattern, participants eager to get the ‘power point slides’ rather than reading the books and articles. So far I have not found any solution to this ‘lazy or slack designer syndrome’. Bigger problem is many of these lazy/slack designers are considered ‘good/great designers’ in their company because they can rattle of latest technology and design buzz words.

For many years I regularly read blogs, various book and studied how other good designers think out of habit.But I could not clearly explain/articulate a new comer why I am doing this. Now I know how this habit has helped me and how it can help a new comer to study his craft.

Wednesday, October 13, 2010

Dear C# Programmers, please use 'using' and 'IDisposable'

In C++ world, RAII (Resource Acquisition Is Initialization) is a widely accepted idiom. It used by std::auto_ptr<>, CComPtr<>, CWaitCurstor and all smart pointer implementations, streams, etc etc manage the lifetime of resources (e.g. memory, files, cursors, database connections etc). RAII ties the life time of a resource to a life time of a 'local object' (i.e. a object on stack). C++ guarantees that destructors of the objects on stack get call in face of exceptions, multiple returns from functions etc. However, the technique 'as it is' is not directly useful in C# since in C# all objects are created on heap.

Since it is a very useful technique, Microsoft added an interface IDisposable and a keyword 'using' to .Net. It gives all the advantages of RAII.

Recently I saw code similar to following
Stream astream=null;
try
{
    astream = new FileStream(...)
    ....
    // do some processing on stream  
}
catch(...)
{
  
}
finally()
{
    if( astream != null)
    {
       astream.Close()
    }
}
The developer was trying to ensure that stream is closed in case of any exception is thrown (or multiple returns). However, there is much simpler way to do achieve the same result. Rememer 'Stream' objects implement IDisposable interface.  So you can achieve this same effect simply by
using (astream = new FileStream(...))
{
    ....
    // do some processing on stream   
}
It is smaller, simpler and easier to understand. This code clearly indicates, what is the expected life of 'astream'.  With Garbage collector .Net (somewhat ) automates managements of one types of resource i.e. memory. However, there is no good way to manage other types of resources (like files) in face of exceptions where a 'guaranteed release' at certain point is required. Since GC doesn't really guarantee at what object will get garbage collected, tieing resource release to object lifetime doesn't work well. IDispose and 'using' keyword address this concern.
Even though this functionality is available in C#/.Net for a long time, many C# developers don't seem to use it. Probably because they are not really aware of the benefits. Hence my dear C# programmers, please learn about IDisposable/'using' keyword and use it regularly. It will make your life easier. In fact, I will recommend that as much possible make your own classes IDisposable

Sunday, August 09, 2009

Implementing a Well behaved Singleton - 4

In part-3 of this series, I talked about implementing singleton such that construction sequence is guaranteed and the destruction is guaranteed however the sequence in which the destruction happens is not guaranteed.

Sometimes one singleton object depends on another singleton object. In such cases, the technique described in part-3 will be useful to ensure that the singleton's are constructed in the correct order. However, since it uses the 'function static objects' , the destruction sequence is compiler dependent. Suppose Singleton object Foo depends on singleton object Bar, this technique will ensure correct order of construction. However, compiler may call the destructors of Foo and Bar in the wrong order, since compiler is not aware of this dependency.

To fix the destruction sequence we will take the help of little known function called 'atexit'. You can find the details of 'atexit' here. or in the compiler help. The important property is If more than one atexit function has been specified by different calls to this function, they are all executed in reverse order as a stack, i.e. the last function specified is the first to be executed at exit. This is property we need to ensure that the singltons are destructed in the 'reverser order' of their construction.


class FooSingleton
{
public:
~FooSingleton(void);
static FooSingleton& GetSingleton(void);

private :
FooSingleton(void);
static void DestroySingleton(void);

private :
static FooSingleton* m_pFoo;
int m_var;
};

////////////////////////////////
// In the .cpp file
FooSingleton::m_pFoo = NULL;

FooSingleton::FooSingleton(void)
{
//...
// Code construct foo singleton
::atexit(FooSingleton::DestroySingleton); // THIS IS KEY }
}

FooSingleton&
FooSingleton::GetSingleton(void)
{
if( m_pFoo == NULL)
{
m_pFoo = new FooSingleton;
}
return(*m_pFoo);
}

void
FooSingleton::DestroySingleton(void)
{
delete m_pFoo;
}

Now if FooSingleton and BarSingleton objects are coded in similar way and FooSingleton depends on BarSingleton then 'FooSingleton::GetSingleton' will call BarSingleton::GetSingleton(). Hence the BarSingleton will get constructed first and in the process the BarSingleton::DestroySingleton function will get registered with 'atexit. Next FooSingleton::DestroySingleton will get registered with 'atexit'.

When program exits, the functions registered with 'atexit' will be called in the reverse order of registration. Hence the FooSingleton::DestroySingleton will get called first and then BarSingleton::DestroySingleton.

Now we have ensured that the singleton objects are destructed in the reverse order of their construction sequence. This technique is completely 'standards compliant' and hence should work on all C++ compilers.

Many developers have misconception that Singleton is the easiest design pattern to implement. As you can see implementing a 'well behaved' singleton is NOT such an easy problem to solve.

Monday, April 13, 2009

Book recommendations for Software Developers

As I mentioned in my previous post on 'C++ Book Recommendations', I have now published a list of books for software developers. These are the books helped me in developing my ideas about Software Development (irrespective of technlogy or programming languages)

Check the list at "Book Recommendations for Software Developers"

Monday, July 12, 2004

Implementing a Well behaved Singleton - 3

Last time I showed an singleton implementation where 'User has to manage the destruction of Singleton object'. (the part-2 is here). In this part, lets see how to improve on that.

class FooSingleton
{
public:
~FooSingleton(void);
static FooSingleton& GetFooObj(void);

private :
FooSingleton(void);
private :
int m_var;
};

FooSingleton&
FooSingleton::GetFooObj(void)
{
static FooSingleton fooObj;

return(fooObj);
}


If you compare the code from the Part 2 and this code, there are small difference.
1. GetFooObj function is now returning a Reference to FooSingleton and not a pointer. So chances of user, copying and deleting this pointer are less.
2. There is no global FooSingleton pointer. The GetFooObj function is now using a function static instance of FooSingleton (fooObj)


The result is
1. when fooObj is created on the first call to GetFooObj().
2. Every subsequent calls same instance is returned.
3. Since this is a static object, compiler manages the destruction of the object. User don't have to worry about destructing the singleton.
4. Singleton object creation sequence is now predictable. So if some other global/static object is dependent on existence of FooSingleton instance, GetFooObj() function will create it at appropriate time.


THIS IS GOOD. Most the problems with previous implementations are taken care off. However, the destruction sequence is still not predictable. And that is topic of next entry in the series.

Tuesday, June 22, 2004

Implementing a Well behaved Singleton - 2

In the second part of this series, Lets look at a simple singleton implementation and see how these questions affect the implementation.

class FooSingleton
{
   public :
       FooSingleton(void);
       ~FooSingleton(void);

       static FooSingleton* GetFooObj(void);

   private :
      int m_var;

};


static FooSingleton g_fooSingleton;

FooSingleton*
FooSingleton::GetFooObj(void)
{
    return(&g_fooSingleton);
}
The problems with this code
(a) There is no guarantee that User of the class cannot create another instance of the class. So technically this is not a singleton.
(b) User can derive from this class. Usually not a good idea for singletons.
(c) Single instance is implemented using file static object. The sequence of static objects is decided by the compiler. Hence if other static objects depend on Singleton instance of FooSingleton, you have a problem. It may happen that g_fooSingleton is not initialzed yet.

Not a well behaved implementation.

Lets see how to improve on this. First lets make the constructor of the FooSingleton private. This will take care of (a) and (b). Problem however is then you cannot create file static 'g_fooSingleton'. It has to be implemented differently.

class FooSingleton
{
   public:
       ~FooSingleton(void);

       static FooSingleton* GetFooObj(void);

   private :
       FooSingleton(void);

   private :
      int m_var;
      static FooSingleton* m_pFoo;

};


FooSingleton* FooSingleton::m_pFoo= NULL;


FooSingleton*
FooSingleton::GetFooObj(void)
{
    if( m_pFoo == NULL)
           m_pFoo = new FooSingleton();

    return(m_pFoo);
}
First call to GetFooObj will create the Singleton instance, subsequence calls will return the same instance. There are still problems with this implementation.
1. GetFooObj is returning a pointer. What happens if user deletes this pointer by mistake ? The FooSinglton object will NOT know that the pointer is already deleted.
2. Exactly this singleton instance of FooSingleton is going to be destroyed ? Since m_pFoo is a pointer, its destructor will not get called automatically. That could be a problem.

The User (developer) has to manange the destruction of Singleton object and he needs to be really careful in using the class (.e.g don't keep copy of the pointer to FooSingleton, don't delete the pointer to FooSingleton etc). Not a well behaved implementation.

How to improve on this is part of next installment of this series :-)

Monday, June 21, 2004

Implementing a Well behaved Singleton - 1

Singleton is one of the simplest design patterns. It also the one most abused. I have seen developers replace a global variable with a signleton object and argue that since its a design pattern, its good without really looking at the intent of the pattern and the effects.

What do i mean by a Well Behaved Singleton ? A well behaved Singleton will have following characteristics.
1. Construction is trasparent to the user(at start of program or on demand).
2. If construction of the singleton depends on some other object being initialized first, then the implementation should ensure that constructor sequence is correct under any circumstances.
3. Destruction is trasparent to the user.
4. If some other object (esp. destruction of some other object) depends on Singleton, timing and sequence of destuction of Singleton should be correct in any circumstances.
5. Single instance is ensured by the implementation. It should be impossible to create a second instance. If the user tries to create a second instance, the implementation should give an error (e.g compilation error, assertion failure etc)
6. All assumptions are made explicit.

Assuming that you understand intent of pattern and want to implement a singleton, there are many ways to achieve it, depending on your needs. First Lets look at major considerations for how to implement a Singleton.

1. Do you anticipate a need to create a derived class which is also a singleton ?
2. Does the initialization of a Singleton object depend on some other object being already initialized ?
3. Does some other object (e.g. other singleton) depend of Singleton object being initialized first ?
4. Does the destruction of a Singleton object depends on existance of some other object ?
5. How Are you ensuring destruction of Singleton ?



In this series I plan to look at some ways of implementing Singleton, look at pros/cons of them and see how well behaved they are.