RSS

Tag Archives: design pattern

Observer vs Publish Subscribe (Pub Sub) Pattern

Oberserver

  • The recipient knows who’s the sender of the message.
  • Unicast, send one, receive by one.
  • Asynchronous.
  • Straight forward, from sender to recipient directly.

Pub Sub

  • Both sender and recipient don’t know who send or receive the message. They just don’t need to know.
  • Multicast approach, send one, every one receives.
  • Asynchronous, messages can be sent to multiple recipients at the same time.
  • Message queue based, there’s some kind of container to queue a message.
 
Leave a comment

Posted by on June 3, 2015 in General

 

Tags: , , ,

Composite vs Decorator Patterns

In Java world (yes, I know, there is such world ostensibly …), there’s a templating framework called Tiles. It’s basically an open source framework to help developers build View part of the MV* (* = whatever).

Tiles uses Composite pattern, much like what you see in ASP.NET MVC.

The other side of the coin is Decorator pattern. SiteMesh is an example of Decorator pattern, it is also a Java open source template framework to build View. In .Net world, SiteMesh is equivalent to classic ASP’s master page concept.

Composite vs Decorator

Both Composite and Decorator break layout to smaller components (or sections), such as Header, Menu, Content and Footer. Each of these components is built on separate page (.jsp file in the case of Java).

When the page is requested, it will take all needed components and put them together as a page. The difference between Composite and Decorator is on the way the page is constructed.

Composite allows the children components to define what’s needed. For example, when you load a Contact page, you tell the framework that the main component to load is Contact. The framework knows Contact component needs Header, Content and Footer components. It will then load all the dependencies components, stick them into Contact component. The page is complete and returned to user.

Decorator, on the other hand, relies on the parent (or master page) to define what’s needed for a page. Taken from Contact page example above, Decorator will first see which parent component the Contact is using, then call the parent component. From there, it knows the parent’s dependencies, construct the parent component along with its dependencies, then stick the Contact component into parent component where it belongs. In this case, the parent component also define where the main component should be placed.

More differences from Tiles project site.

Aspect Composite View Decorator
Reusability The different parts of the page (template and pieces) can be reused across the whole application. Each decorator can be reused, but the decoration itself can be applied to one page at a time.
Ease of configuration Each page must be defined explicitly. The decorator can be applied even to the entire application.
Runtime configuration The pages can be configured and organized at runtime Since one page is decorated at a time, this feature is not present.
Performances Low overhead for composition. The page to be decorated has to be parsed.

Decorator Problem

One problem I had before was trying to build a reusable web component, think about Calendar widget. I would be fine if I build my widget with pure HTML and JavaScript, but if there is a special case where I need to use View framework to load up my widget, then Decorator wouldn’t be much useful because I can’t have children component to use my widget without repeating code.

That’s not to say Composite doesn’t have any problem, I just haven’t come across one.

 
Leave a comment

Posted by on April 29, 2015 in General

 

Tags: , , , ,

What is Command Query Responsibility Separation (CQRS)?

From Wikipedia: every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer.

what-is-command-query-responsibility-separation-cqrs

Further read: MSDN’s CQRS Journey

 
Leave a comment

Posted by on October 30, 2014 in General

 

Tags: ,

What is Repository Pattern?

The repository pattern is a data access pattern that abstracts away data access code. With the repository pattern, the application can interact with a data source without knowing specific details of the data source. The application communicates to a repository objects and the repository objects responsible for getting, inserting, updating or deleting data. The application doesn’t know anything about the data access, the repository is in charge of that.

Why would you use Repository pattern:

  • Testability: easier unit test your application, specifically data access layer.
  • Maintainability: since all the data access logic resides in its own object, it’s easier to maintain.
  • Refactor: easier to change data access later. For example, switch from a local DB to a cloud based DB.

Further read: Martin Fowler’s Repository Pattern

 
Leave a comment

Posted by on April 25, 2013 in General

 

Tags: ,

What is Singleton Pattern?

The singleton pattern ensures a class has only one instance and provide a global point of access to it.

Example of Singleton pattern in real-world application:

  • Logging. Logger object instance needs only exist once across the application life cycle. Especially true when the log is written to a locking-mechanism store, such as a Windows file.
  • Load Balancer. A load balancer type of situation must only exist as a singleton object to direct which resource is available to a invoker. 
  • IoC Container. Underneath any IoC container, there’s singleton instance object that controls what class implementation to pass given the contract.

Further read: GOF’s Pattern: Singleton

 
Leave a comment

Posted by on April 25, 2013 in General

 

Tags: ,

Quick Take on MVC x MVP x MVVM

I wish I am more diligent to create visual diagram for this post. But, here’s the take.

MVP

A variation of MVC pattern and mostly implemented in ASP.NET web form to achieve separation of concern such in MVC. In MVP, Presentation knows about the View through View’s contract (interface). Suggested reading: Jean-Paul Boodhoo’s August 2006 Design Patterns column.

MVC

Very similar to MVP, but in MVC, the controller (Presenter in MVP) doesn’t refer back to View. A controller can be used by multiple Views. This is achieved with Action (a method in controller) in ASP.NET MVC. Suggested reading: Niraj Bhatt’s MVC vs MVP vs MVVM.

MVVM

Similar to Martin Fowler’s Presentation Model pattern, MVVM pattern is introduced by John Gossman in 2005, tailor-made to harness the core feature and power of WPF (and Silverlight) platform. In MVVM, as in Presentation Model pattern, the ViewModel keeps up to date with the View, the two always sync with each other. Suggested reading: Josh Smith’s The Evolution of Model-View-ViewModel.

MVVM in JavaScript

In recent years, MVVM has also been implemented in JavaScript as a framework, such in KnockoutJS or Kendo MVVM. Suggested reading: Addy Osmani’s Understanding MVVM – Guide for JavaScript Developers

ASP.NET MVC’s View Models

In ASP.NET MVC, there is something called View Models. This is not to be confused with MVVM pattern. View Models is objects defined in class that each View in MVC interact with. The difference between View Models and Model in MVC is that Model refers to the objects that the whole application interact. Suggested reading: Stephen Walther’s ASP.NET MVC Create View Models

Other Sources

More links for more readings about the three:

MVVM vs MVP vs MVC: The concise explanation
MVVM vs MVP vs MVC: The differences explained

 
Leave a comment

Posted by on July 13, 2012 in General

 

Tags: , , , ,

jQuery Widget Factory Properties and Methods

Properties

this.element
The element of the invoked plugin.

this.name

this.namespace

this.options
Provide options that users can set.

this.widgetBaseClass

this.widgetEventPrefix

Methods

_create
This is where you set up your widget. This method get called first time the plugin is invoked / initiated on an element.

destroy
Method to reverse everything the plugin has applied.

disable

enable

_init

option

_setOption
Method to respond to changes to options property.

_setOptions

_trigger
Fire an event that users can use.

Source: jQuery UI Wiki

 
Leave a comment

Posted by on July 7, 2012 in References

 

Tags: , , ,

 
%d bloggers like this: