Skip to main content

Posts

Showing posts with the label Design Pattern

Example - Chat Application

1. Create a WCF Service library project Open Visual Studio > File  >  New  >  Project Select  WCF Service Library  template under  Visual C# > WCF  templates Change the name to  ChatServiceLibrary  and click  OK Visual studio will add reference to System.ServiceModel and System.Runtime.Serialization and will add some files with sample code.  Delete IService1.cs and Service1.cs and add a new class called ChatService 1.1 Create the Data Contract  Open ChatService.cs file that you created. Add  using System.ServiceModel; Add  using System.Runtime.Serialization; Add the following code above ChatService class    [DataContract]     public class ChatMsg     {         public ChatMsg() { }         public ChatMsg(string n, string m){ this.User = n; this.Msg = m;}            ...

Composite Pattern

Composite pattern is useful when Group of items and an individual item need to be handled uniformly. For example, operations cut, copy and paste should work uniformly for both an individual file or a directory. Leaf Branch Contact Contact Group File Directory Address Address group

Adapter Pattern

An Adapter as the name says is used to provide a mechanism that adapts an existing interface to the required interface. This helps the types that are not designed to work together, work together. Adapter pattern may become handy when working with legacy system. Here adapter allows the use of interfaces that are incompatible with new interface requirement. For example, an object needs to compress a file. You find that there is another class that provide the compression service, but has a different method signature than the one required by the object. An adapter patter can come to rescue in such situations. We can create an adapter class that allows the existing service to be used through a new interface. There are two kinds of adapters: Class Adapter - This type of adapters adapters through sub-classing  Object Adapter - This type of adapter use delegation to adapt instead of subclassing Class Adapter  Let say you want to use this existing method zipFile() i...

Facade Pattern

A facade is "the front of a building OR any side of a building facing a public way or space and finished accordingly." ( Dictionary.com , 2014). A facade pattern hides all the complexities behind a simple easy to use interface. It provides a simplified access to subsystem. Related pattern: Trusted Facade Pattern Bibliography Bishop, J. (2016, 12 30). Structural Patterns: Adapter and Façade . Retrieved from MSDN: https://msdn.microsoft.com/en-us/library/orm-9780596527730-01-04.aspx MSDN. (2016, 12 30). The Trusted Façade Pattern . Retrieved from MSDN: https://msdn.microsoft.com/en-us/library/ff649496.aspx MSDN. (2016, 12 30). Trusted Facade Service . Retrieved from MSDN: https://msdn.microsoft.com/en-us/library/aa355058(v=vs.110).aspx

Singleton Pattern

Singleton Pattern is used to control the instantiation of a class. A Singleton class can have only one instance and the class provides a global access point to that instance. The singleton class has the following responsibility: ensures that only one instance of the class is created. provides access to that instance of the object. ideally the singleton instance should be created only when it is needed. There are situation when we need a single unique instance of a class. To do this we can make the constructor of the class private so that only the class itself is capable of creating an instance of itself. We can hold the instance of the singleton class in a global variable and provide access to this instance through a global method. However, a global variable would exist for the duration of the program taking up resources. This could become a problem when the instance is resource-intensive. Therefore, you may want to create such an object only when it is needed. Si...

Design Patterns Intro.

What are Design Patterns? A Design Pattern is a recurring solution to a recurring problem in a specific context. Patterns are not a specific to Software engineering. They are applied in many disciplines including but not limited to Construction, Mechanical Engineering and psychology. The first book on patterns was written by Christopher Alexander in 1977 on building architecture. “A pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” - Christopher Alexander Design Patterns is the first book on software design patterns written by Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) or GoF. This book describes 23 common design patterns (Gamma et al, 1995). All of these 23 design patterns are explained in your text book. Design patterns have 4 essential element...