Factory Method Pattern // Creational Patterns // Design Patterns

[Notethe Factory Method pattern and the Abstract Factory pattern are often confused. This post is about the first and simpler, the Factory Method pattern]

What’s the point

Usually, a constructor for a class type Foo will return an object of type Foo. With the Factory Method pattern, object construction is handled by another (virtual) method, the factory method, and may return objects that are subclasses of Foo. The conventional constructor is ignored – and often, deliberately made private so that it can’t be called by subclasses.

The Factory Method pattern deals with the creation of objects without specifying the exact class of object being created. It’s a more abstract way of creating objects.

There are a several variations that could all reasonably be described as implementations of the Factory Method pattern. Here’s one of the more thorough examples.

Suppose you’re writing an application to publish status updates to various social media platforms using their APIs – Facebook and Twitter, for example. You could imagine that both APIs have some basic methods like (keeping it simple):

  • Authenticate
  • PostStatus

So we could define a base implementation for a social media platform API:

and then define some classes that descend from it:

Next, we manage the creation of these objects in separate classes which are themselves derived from a common base class. The base class declares a method that each descendant must override, a method that returns an object implementing IPlatformAPI.

and finally we have a method in a supporter class that creates the appropriate object instance, given a type:

So what have we achieved here?

We’ve defined an abstract base class that could in principle handle common API-related manipulation without having to know about the details of the descendant classes;  We’ve removed the creation of the derived objects from those object classes directly to another hierarchy of constructor classes that implement a base classes’ virtual factory method; given a parameter – a string here, for instance – we can use a simple method to create an appropriate instance of a class. The factory method that does this creation can, in each creator class,  easily provide different functionality.

Darwin: once more with feeling

After the voyage ended this year  –  or 1836, Beagle Time –  I asked several people what I should do with the Twitter account. The response was unanimous: go round again.  I’d initially started tweeting just over a year into the voyage so there is a year not covered, and this time all the rest of the voyage will be covered by the database of tweets. So we’re going round again.

http://twitter.com/cdarwin

Goddbye Sony. Hello Kindle.

I while back I bought a Sony PRS-505 eBook reader. I’d contemplated the slowly growing eBook market for a while, I’d heard good things from the US about the Kindle, and I thought it about time I dipped a toe in the water.

After reviewing the eBook landscape I plumped for the Sony. The Kindle was still pricey and didn’t have a separate UK store; The Nook hadn’t been released; Sony used ePub, an open XML-based standard. The touch screen Sony PRS-700 had just been released but was getting somewhat mixed reviews, with some users pointing out that pushing greasy fingers over a surface you’re supposed to read was bound to cause a few problems, so I opted for the Sony PRS505 which had had very good reviews when it first came out.

Since then I’ve been disappointed with it for the following reasons:

  • Speed
  • Display
  • Lack of integration with store

Taking them in turns:

Speed

The page turn isn’t fantastically quick but it’s quick enough not to be a problem. However. My PRS-505 can be unbelievably slow to display the index of books when it first starts up after being switched off and it can also be very, very slow when loading a book. It didn’t take quite as long as the time needed to make a coffee but it was getting on for that. If you change the font size of the display then the book would be repaginated and I would experience that very long wait again.

Display

The e-ink display was supposed to be about the best around when I bought the device but it is still disappointingly grey. I don’t suppose this irritation is confined to Sony eReaders

Lack of integration with store

I buy my books from Amazon. But Amazon was competing with Sony and had the Kindle so I couldn’t buy books from Amazon to read on the PRS-505. I didn’t want to open another book-buying account with another seller; and then, Google announced the availability of thousands of freely downloadable ePub books which Sony made accessible but only through their US store, at least to begin with. That was frustrating. I reinstalled the Sony software and claimed to live in the US just to be able to access the free classics Google was making available.


And now the new Kindle is out. It’s significantly reduced in price. It has WhisperSync, which allows you to pick up reading where you left off on different devices; it has free 3G and WiFi to allow you to contact the Amazon store wherever you happen to be, whenever you want to buy a book. It’s Amazon – and that’s where I buy my books. It’s a smaller device, it looks smarter, it has better battery life, the display is better…and Amazon will allow non-DRMd eBooks to be sold in their store if that’s what the publisher wishesl Sony won’t. And it uses Amazon as a permanent store for all your books.

So it’s the new Kindle for me. I might be just as disappointed by Christmas with the Kindle as I was with the Sony; but I think this iteration of the device might be the the one that tips me over into buying the bulk of my books as eBooks in future. I hope so.

Consultants, training and Mehrabian

I recently attended a day-long Project Management course. Building a lego tower made an enjoyable change to my daily routine but  our trainer made one or two claims about psychology and personality theory that I doubted. Living with a Psychologist sometimes isn’t easy.

For example, I was surprised at the uncritical claims made by our trainer for the work of Jung. This is the Jung who, as a follower of Freud, had no problem with the idea of an unconscious mind (rather than unconscious mental processes), who contended that alchemical symbols had a direct relationship to the ‘psychoanalytic process’, whatever that may mean, who believed in a Collective Unconscious shared by everything with a nervous system and who suggested that flying saucers were  an expression of an archetype.

Given all this, I tend to be sceptical of claims of truth rooted in Jungian theory. I become doubly sceptical when the person making the claim, faced with perfectly natural incredulity about some counter-intuitive assertions, simply appeals to authority. And when that authority is Jung, well, then I need to go and check the facts.

In this case the claims were for the efficacy and accuracy of a theory of personality types based on a 2-factor model:  personalities could be assigned a position in a 2-dimensional space using the supposedly independent axes of Assertiveness and Responsiveness. Our trainer incorrectly stated  – and incorrectly restated   –  that these axes and the personality inventory we had all completed before the day began were designed by Jung. They weren’t. This Social Styles model derives from the work of David Merrill.

the 2 dimensions of Merrill’s social styles model

This model can hardly be seen to derive from even a simplification of  Jung’s basic attitudenal axis of intraversion-extraversion, combined with his four functional types  – Thinking, Feeling, Sensation and Intuition  – and to describe it as directly and specifically the work of Jung is clearly incorrect.

And what is it worth? It’s extraordinarily popular on the web but is there much to a categorisation of people in this 2-dimensional space? What evidence is there that these two factors are orthogonal? That they are fundamental?

It seems unlikely that the salient features of everyone’s  ‘social style’ could be adequately described in such a simple model and the ready availability of other models using different factors and different numbers of factors suggest that this particular model might not have much purchase in reality. Goldberg has a 5-dimensional model; Eysenck has three; Holland has six; Allport names three levels of traits; Cattell, two tiers of sixteen and five factors respectively.

These models all suffer from the well-known criticisms of all trait-thories: of being essentially descriptive rather than explanatory; of being over-simplifications; of being superficial; of ignoring situational dependencies; and of being statistical generalisations that do not correspond to individual behaviour. I certainly wouldn’t base my decisions on them or alter my behaviour because of them.

The next contentious assertion made by our trainer was about Mehrabian‘s studies that appeared  – to our trainer – to suggest that only 7% of communication was in the content of what was spoken, with – according to her –  38% provided by ‘accent’ (presumably Mehrabian’s ‘tone’) and the remaining 55% by bearing or posture (Mehrabian’s ‘facial expression’).

This was simply a misunderstanding, or misrepresentation of Mehrabian’s studies and conclusions, surprising for such an experienced trainer who had some edcuation in Psychology. It’s a very common misrepresentation amongst management consultants, sales trainers, and suchlike.

Mehrabian’s studies asked participants to judge the feelings of a speaker by listening to a recording of a single word spoken in different tones of voice. That was it. Mehrabian himself has become sufficiently exasperated to publish a warning on his website :

Please note that this and other equations regarding relative importance of verbal and nonverbal messages were derived from experiments dealing with communications of feelings and attitudes (i.e., like-dislike). Unless a communicator is talking about their feelings or attitudes, these equations are not applicable.

and he goes on to say:

I am obviously uncomfortable about misquotes of my work. From the very beginning I have tried to give people the correct limitations of my findings. Unfortunately the field of self-styled ‘corporate image consultants’ or ‘leadership consultants’ has numerous practitioners with very little psychological expertise

None of Mehrabian’s concerns, or the critiques made by other researchers, were mentioned by our trainer.

Still, the lego was fun.