Skip to main content

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.

Comments

Popular posts from this blog

Internet Information Services(IIS) reveals its real or internal IP Address

In the ever changing world of global data communications, inexpensive Internet connections, and fast-paced software development, security is becoming more and more of an issue. Security is now a basic requirement because global computing is inherently insecure.

Keeping that in mind, we recently ran our flagship product through a security audit. It was such a helpful exercise in tying-off any remaining lose ends in our application in terms of application security. 
Based on the security audit report, there was a relatively minor issue that appeared when accessing the /images directory of our application. Turns out that the Location response header of the 301 request returns an Internal IP address. The issue is detailed below.

Issue reportedInternet Information Services (IIS) may reveal its real or internal IP address in the Location header via a request to the /images directory. The value returned whilst pen testing is https://10.0.0.10/images.

The riskInformation regarding internal IP add…

IIS Request Filtering to block HTTP Verbs (For example Trace)

The issueRequest Filtering is a built-in security feature that was introduced in Internet Information Services (IIS) 7.0. This can be used to block specific verbs like "Trace".

When request filtering blocks an HTTP request, IIS 7 will return an HTTP 404 error to the client and log the HTTP status with a unique substatus that identifies the reason that the request was denied. Verb Denied.

HTTP SubstatusDescription404.5URL Sequence Denied404.6Verb Denied404.7File Extension Denied404.8Hidden Namespace404.1Request Header Too Long404.11URL Double Escaped404.12URL Has High Bit Chars404.13Content Length Too Large

Unit Testing HttpContext.Current.Session in MVC3 .NET

We recently changed some functionality where during the "CREATE" process, we go through a wizard to save application data. This data is saved only to the session in the final step when the user clicks the final submit.

This was easy enough to implement but when I started writing unit tests for my static methods that Add, Update, Delete or Modify the contents of our application data in the session, I got the following error:
System.NullReferenceException: Object reference not set to an instance of an object.

Turns out I had forgotten to setup the HttpContext.
The following "TestInitialise" method fixed my problem :)

[TestInitialize]
public void TestSetup()
{
// We need to setup the Current HTTP Context as follows:

// Step 1: Setup the HTTP Request
var httpRequest = new HttpRequest("", "http://localhost/", "");

// Step 2: Setup the HTTP Response
var httpResponce = new HttpResponse(new StringWriter());

// Step 3: Se…