Tuesday 19 April 2016

DEV MONTHLY #1: Coding Design Patterns

Coding Design Patterns



One of the most important aspects of writing maintainable code is being able to notice the recurring themes in that code and optimize them. This is an area where knowledge of design patterns can prove invaluable.

What is a Pattern you ask?


A pattern is a reusable solution that can be applied to commonly occurring problems in software design. The best part is, you might be using them without even knowing it!

Why would you use a well-known pattern?


  • Patterns are proven solutions and provide solid approaches to solving issues in software development 
  • Patterns can be easily reused as they can be adapted to suit our own needs
  • Patterns can be expressive as there’s generally a set structure and vocabulary to the solution



Patterns you must know (Language Agnostic)


Below, I am going to highlight some patterns that I love using & I think any good developer must know, but this in no way is a complete list. Be sure to share your fav patterns with others.

Note: I won’t be covering the MVC & MVVM patterns as they’re well documented at various different places.

Factory Pattern

Provides an interface for creating families of related or dependent objects without specifying their concrete classes.

For example lets discuss a scenario where the primary actors are:

  • Client
  • Factory
  • Product




The client is an object that requires an instance of another object (the product) for some purpose. Rather than creating the product instance directly, the client delegates this responsibility to the factory. Once invoked, the factory creates a new instance of the product, passing it back to the client. Put simply, the client uses the factory to create an instance of the product.

The factory completely abstracts the creation and initialization of the product from the client. This indirection enables the client to focus on its discrete role in the application without concerning itself with the details of how the product is created.

Sample code is available here: https://goo.gl/MTeCWS and https://goo.gl/YoQSW4

Builder Design Pattern

Separate the construction of a complex object from its representation so that the same construction process can create different representations. Parse a complex representation, create one of several targets.They address the problem where an application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.

Many times creational patterns (such as this one) are complementary: Builder can use the Abstract Factory to implement which components get built.

Sample code is available here: https://goo.gl/W2CmFX and https://goo.gl/NiRZ4r

Some Other Patterns worth researching
Repository Pattern: https://msdn.microsoft.com/en-us/library/ff649690.aspx
Service locator Pattern: https://msdn.microsoft.com/en-us/library/ff648968.aspx


JavasScript Specific Patterns

Module Pattern


Module pattern is used to further emulate the concept of classes in such a way that we're able to include both public/private methods and variables inside a single object, thus shielding particular parts from the global scope. What this results in is a reduction in the likelihood of our function names conflicting with other functions defined in additional scripts on the page.

The Module pattern encapsulates "privacy", state and organization using closures. It provides a way of wrapping a mix of public and private methods and variables, protecting pieces from leaking into the global scope and accidentally colliding with another developer's interface. With this pattern, only a public API is returned, keeping everything else within the closure private.

This gives us a clean solution for shielding logic doing the heavy lifting whilst only exposing an interface we wish other parts of our application to use. The pattern is quite similar to an immediately-invoked functional expression (IIFE[http://goo.gl/WF41CY] - see the section on namespacing patterns for more on this) except that an object is returned rather than a function.

Sample code: https://goo.gl/KiMXFk

The Revealing Module Pattern

The Revealing Module pattern is an extension of the module pattern requires us to define all of our functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public.
Sample code: https://goo.gl/d85I9n

Some more links ..

https://addyosmani.com/resources/essentialjsdesignpatterns/book/
https://msdn.microsoft.com/en-us/library/ms954638.aspx
http://programmers.stackexchange.com/questions/125822/how-important-are-design-patterns-in-programming
http://www.dofactory.com/net/design-patterns
https://sourcemaking.com/design_patterns/builder
https://sourcemaking.com/design_patterns/builder


http://www.dofactory.com/images/logo2.png
Data & Object Factory helps developers succeed with .NET Design Patterns through training, products, and a .NET Design Pattern and Practices community

http://cdn.sstatic.net/Sites/programmers/img/apple-touch-icon@2.png?v=8a6d048f3c78&a
programmers.stackexchange.com
I'm a university student and I've just started learning about design patterns and im struggling to understand the purpose of them. I have tried researching them but ...


msdn.microsoft.com
This paper starts with a brief review of SOA concepts and leads up to a detailed discussion of several patterns and anti-patterns that developers can leverage when ...



https://addyosmani.com/resources/essentialjsdesignpatterns/cover/cover.jpg
addyosmani.com
#JavaScript Design Patterns . In this section, we will explore JavaScript implementations of a number of both classic and modern design patterns.