Design Patterns #10

designp

Command pattern

The Command pattern encapsulates actions as objects. Command objects allow for loosely coupled systems by separating the objects that issue a request from the objects that actually process the request. These requests are called events and the code that processes the requests are called event handlers.

Suppose you are building an application that supports the Cut, Copy, and Paste clipboard actions. These actions can be triggered in different ways throughout the app: by a menu system, a context menu (e.g. by right clicking on a textbox), or by a keyboard shortcut.

Command dofactory – link
Command dotnettutorials – link
Class code: Command – azure example
Class code: Command – javascript example

Fluent pattern

A fluent interface is normally implemented by using method chaining to implement method cascading, concretely by having each method return this (self). Stated more abstractly, a fluent interface relays the instruction context of a subsequent call in method chaining, where generally the context is

  • Defined through the return value of a called method
  • Self-referential, where the new context is equivalent to the last context
  • Terminated through the return of a void context

Note that a “fluent interface” means more than just method cascading via chaining…

Class code: fluent – LINQ
Class code: fluent Customer

Flyweight pattern

The Flyweight pattern conserves memory by sharing large numbers of fine-grained objects efficiently. Shared flyweight objects are immutable, that is, they cannot be changed as they represent the characteristics that are shared with other objects.

Essentially Flyweight is an ‘object normalization technique’ in which common properties are factored out into shared flyweight objects. (Note: the idea is similar to data model normalization, a process in which the modeler attempts to minimize redundancy). An example of the Flyweight Pattern could be a is a list of immutable strings that are shared across the application.

Other examples include characters and line-styles in a word processor, or ‘digit receivers’ in a public switched telephone network application. You will find flyweights mostly in utility type applications such as word processors, graphics programs, and network apps; they are less often used in data-driven business type applications.

Flyweight dofactory – link
Flyweight dotnettutorials – link
Class code: Flyweight – big numbers
Class code: Flyweight – java GUI example

Weak reference

Objects that are referenced must be kept in memory until they are unreachable. use the WeakReference type, which leaves objects accessible in memory until they are collected by the Garbage Collector (GC).

If you have cache layer implemented with C# it’s much better too put your data in cache as weak references, it could help to improve your cache layer performance. Think that approach also could be applied to session implementation. Because session is long living object most of the time, it could be some case when you have no memory for new user. In that case it will be much better to delete some else user session object then throwing OutOfMemoryException.

Also, if you have a large object in your application (some big lookup table, etc), that should be used quite seldom and recreating of such an object isn’t a very expensive procedure. Then better have it like a week reference to have a way to free your memory when you really need that.

Class code: weak reference

Weak reference regenerate

More topics covered:

  • GC.Collect( )
  • Gather commands in different invokers
  • Add a button which invokes a command
  • WPF command

Links:

Leave a comment