Understanding Design Patterns in better way

While our interview process we ask Questions on Design Patterns and most of the Candidates are only aware of MVC Design Pattern. I’m writing this post influenced from Book Design Patterns, Elements of Reusable Object-Oriented Software

There are lots of Definitions of Design Patterns on internet but Christopher Alexander says, “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it in the same way twice

In simplest way “design pattern is a general reusable solution to a commonly occurring problem within a given context in software design“.

In general, a pattern has four essencial elements:

  • Pattern Name
  • Problem
  • Solution
  • Consequences

The pattern name is a handle we can use to describe a design problem, its solutions, and consequences in a word or two.

  • Naming a pattern immediately increases the design vocabulary. It lets us design at a higher level of abstraction.
  • Having a vocabulary for patterns lets us talk about them.
  • It makes it easier to think about designs and to communicate them and their trade-offs to others.

The problem describes when to apply the pattern.

  • It explains the problem and its context.
  • It might describe specific design problems such as how to represent algorithms as objects.
  • It might describe class or object structures that are symptomatic of an inflexible design.
  • Sometimes the problem will include a list of conditions that must be met before it makes sense to apply the pattern.

The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations.

  • The solution doesn’t describe a particular concrete design or implementation, because a pattern is like a template that can be applied in many different situations.
  • Instead, the pattern provides an abstract description of a design problem and how a general arrangement of elements (classes and objects in our case) solves it.

The consequences are the results and trade-offs of applying the pattern.

  • The consequences for software often concern space and time trade-offs.
  • They may address language and implementation issues as well.
  • Since reuse is often a factor in object-oriented design, the consequences of a pattern include its impact on a system’s flexibility, extensibility, or portability.
  • Listing these consequences explicitly helps you understand and evaluate them

As per Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides in their Design Patterns book, you can divide Design Patterns into three types:

  • Creational patterns are ones that create objects for you, rather than having you instantiate objects directly. This gives your program more flexibility in deciding which objects need to be created for a given case.
  • Structural patterns help you compose groups of objects into larger structures, such as complex user interfaces or accounting data.
  • Behavioral patterns help you define the communication between objects in your system and how the flow is controlled in a complex program.

I studied following link for Design Pattern implementations in PHP and I assume these are must read for everyone writing code in PHP and in OOPs way.

I would love to participate in healthy discussion on these.

*Credits to Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides.

 

Autocomplete textfield in Drupal.

Dear Friends, Last week I got a chance to work on drupal and had to add autocomplete functionality to textfields. I had to fetch/search data from database and show in text field.

Example: Suppose I want to make autocomplete text field for ‘author’ table:

CREATE TABLE `author` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT ;

Steps to achieve this are:

1. Create a menu hook in your .module file as below:

$items['search/get_author'] = array(
   'title'  => 'text matching',
   'page callback'  => 'author_autocomplete',
   'access arguments' => array('view keyword autocomplete'),
   'type'  => MENU_CALLBACK,
);

Here we are using existing search functionality.

2. Create function that we call for search from database as below:

function author_autocomplete($string='') {
  $matches = array();
   if($string) {
     $result = db_query_range("SELECT * FROM {author} WHERE first_name LIKE '%s%%' OR last_name LIKE '%s%%'", array($string, $string), 0,     variable_get('keyword_autocomplete_matches', 10));
       while ($data = db_fetch_object($result)) {
         $matches[$data->first_name . ' '. $data->last_name] = check_plain($data->first_name) . ' ' .check_plain($data->last_name);
       }
  }
  drupal_json($matches);
}

3. Now create a textfiled with ‘autocomplete_path’ parameter using Drupal form API as below:

$form['Author'] = array(
  '#type' => 'textfield',
  '#title' => t('Author'),
  '#default_value' => $object['Author'],
  '#size' => 60,
  '#maxlength' => 64,
  '#autocomplete_path' => 'search/get_author',
);

Now, Let me explain the concept how it works.

When you type something to Author textfield autocomplete_path callback the ‘search/get_author’ -> Drupal menu hook look for page callback function ‘author_autocomplete’  -> Now function author_autocomplete generate the drupal_json data to autocmplete.js which is automatically included when you work under ‘search’ module.

Hope this will help new drupal developers and some experienced who did not work on this functionality.

Thanks and Regards,

Mahesh Salaria

Windows 7

I just finished testing of Windows 7 on my laptop (Acer Aspire 4520). It runs flawlessly and much better than Vista. Everything is very neat and clean and easy to access.

Lots of improvements in Task bar and other parts of Windows access. Main thing lots of people concerned about performance on Windows Media Center and its very much improved than Vista, You can feel the effects of Zune with it 😉

Windows media player is also improved and optimized to use low memory.

To know about more changes and improvements check on Microsoft Web Site

Here are some screen shots of Windows 7:

But Windows 7 crashes some of the applications and I reported them to Microsoft and I’m Sure they will fix them in Stable build and we are looking for more stable and smooth build.

Credits to Microsoft who provide us rock solid Operating System year after year. 😉

How to become a good Software Developer/Programmer

1. Learn Promramming instead of Languages.

– Everyone is behind the computer programming languages, computer courses etc. but very few of them try to understand basic concepts of programming and develop logic. In my views if you are good in logic, you know how things work in programs then you can work on any language. But you should learn programming ethics then languages.

2. Learn to debug and removing errors.

– Debugging skill in very important for every programmer, a good programmer write a code to report errors properly for easy debugging and helps removing errors. Need thinking up to end user.

3. Always stay motivated and keep learning new skills and technology.

– Always stay motivated, don’t think you are typing a code line. Think you writing a poem in your language in well structured form 🙂 . Always keen to learn new techniques, develop new skill in you.

4. Write good quality code with proper comments so that everyone can understand what you did.

– Good quality code here is reusable code, properly commented and structured so that others can understand what you did and you can work in a team in proper manners.

5. Remove impossible from dictionary, its your duty to make things happen.

– Don’t say this is not possible to develop. Everything is possible, some thinks take time, Some times you have to work hard to do impossible things, its your duty to make things happen.

Everything is fair/possible in War, Love and Programming. So have fun and love to code.