Design Patterns

In relation to programming languages, such as JavaScript, a design pattern refers to method of writing code that reuses specific elements while providing a solution to frequently occuring issues in software design.

The benefits of using design patterns are:

  • They have been proven to solve problems by using methods that reflect on the influences of the developer that defined them.
  • Design patterns can easily be reused, as they reflect on a distinct solution that can be manipulated to specific needs, meaning that they are relatively robust.
  • Design patterns can also be expressive, as there  is generally a structure to the pattern that can aid in conveying a significant solution to the problem quite easily.

Additional advantages to patterns also include:

  • Reusing design patterns help to prevent small issues that can cause larger problems later on in the development process.
  • Design patterns can provide non-specific solutions which are writtin in a way that means that they do not need to apply to indivisual issues.
  • Certain patterns can reduce the total file-size of code by preventing repetition.
  • Design patterns add to a developer’s vocabulary, which makes communication faster.
  • Design patterns that are regularly used can be improved over time through observing techniques used by other developers using those patterns, forwarding the design pattern community.

A design patterns may be considered effective if:

  • The design pattern resolved a certain problem, as they are not intended to capture principles or strategies, but solutions. This is one of the most important factors of an effective design patterns.
  • The solution to an issue can’t be too obvious, provding the solution to a problem indirectly. This is thought to be a nessessary means when dealing with conveluted problems in software development.
  • The design pattern is proven to function as described, otherwise they may not be taken seriously. If a design pattern seems to be very experimental, many users will not take the risk of using it.
  • A design pattern needs to describe a relationship between elements. In a few examples it can be percieved that a design pattern describes a type of module. Although a design patter be interpretated this way, the main description of the pattern must describe far more distinct system structures and mechanisms that explain its relationship to code.

The most common design patterns used in relation to software development are module design patterns. Modules are essentially “classes” in JavaScript. They are primarily self-contained and serve a specific function, meaning that they can be moved around, removed and added to a code file without disrupting the whole system. A reasonable example of a module would be a JavaScript function. A major advantage of modules is encapsulation, meaning that elements included in them cannot be accessed by other modules as it it stored in a seperate scope.

Featured below is an example of a module design pattern:

(function() {

    // declare private variables and/or functions

    return {

      // declare public variables and/or functions

    }  

})();

 

In use:

var HTMLChanger = (function() { 

   var contents = ‘contents’ 

   var changeHTML = function() {   

      var element = document.getElementById(‘attribute-to-change’);   

      element.innerHTML = contents; 

      } 

   return {   

      callChangeHTML: function() {     

         changeHTML();     

         console.log(contents);    } 

      };

   })();

HTMLChanger.callChangeHTML();       // Outputs: ‘contents’console.log(HTMLChanger.contents);  // undefined

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s