Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Sep 26 2008

Going thru Patterns - Factory Method Pattern

Published by Prajwal Tuladhar at 3:32 am under PHP, Patterns

This is my first attempt to try to understand and implement patterns and practices. It is necessary to understand and implement these design patterns in order to make code flexible, re-usable, highly cohesive and less coupled.

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. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems. - Wikipedia

http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

As this is my first post regarding patterns and practices, I want to express something that I have learnt about Factory Method Pattern. It is a part of Gang-of-Four (GoF) design patterns.

The Factory Method pattern is a way of creating objects, but letting subclasses decide exactly which class to instantiate. Various subclasses might implement the interface; the Factory Method instantiates the appropriate subclass based on information supplied by the client or extracted from the current state. This pattern enables to make decision about which concrete class to be instantiatedin the run-time.

Lets go thru an example:

There is an interface called IPerson and two implemented class named Student and Programmer. The class called PersonFactory is created to make run-time decision about the selection of Student or Programmer thru a static method named GetWorkDone.

UML Diagram


PHP Code


Viewing 1 Comment

 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus

RSS Feed
Subscribe by email
Follow me @ Twitter