Abstract Factory - II Abstract Factory vs Factory

by ipsit 3. August 2009 17:32
The other day we had a discussion related to the abstract factory and let me tell you, flames flew and it was high on melodrama.
Anyways coming back to the main point, the root of whole discussion was,
A: Why should I use the abstract factory, just for a parallel set of classes, what am I getting out of it ?
B: You are abstracting your product, that's why you are creating it. Apart from that, its a you have a mechanism for creating related set of products, which is important here, you see you should not forget that, its for creating a set of related family of products.
C: But say if you are just creating is two set of components, a UI Components for rich mode and the simple mode, could you not do with a static Factory implementation instead of so much of fun fare? And also if you look closely, what you are doing is essentially now binding your classes to the implementation of the Abstract Factories some where, because to get the code running you have to write the Implementation class some where, so would not it be better if we had just a static factory class which gives us what we need.
B: hmmmm...
A: Apart from that so much of code to manage .... That's a good one C:)
C: Thanks! Man
B: Factory Method defines an interface for creating an object, but lets subclasses decide which of those to instantiate. A factory method lets classes defer instantiation to subclasses.By contrast, an Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
C: We have a family of related objects but, ... I don't want to take a purists view or go by book view.. I look at convinience and unless you tell me why, I don't see any reason why I should not go for a Factory than a Abstract Factory here.
A: And if you ask me, I guess you are over complicating, is not this kind of an YAGNI.  
B: Well, let me examine the definitions a bit more:
Factory Method defines an interface for creating an object, but lets subclasses decide which of those to instantiate. A factory method lets classes defer instantiation to subclasses.
Which essentially means, in this case the decision is taken by sub class -> Inheritance is in action.
Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes. Now in this case the job of creating the instance of the products are delegated -> Composition in Action.
And as you guys already know Composition allows you to delay the creation of back-end objects until (and unless) they are needed, as well as changing the back-end objects dynamically throughout the lifetime of the front-end object. With inheritance, you get the image of the superclass in your subclass object image as soon as the subclass is created, and it remains part of the subclass object throughout the lifetime of the subclass.
So I would say, if you need it dynamically vary, take the composition root and enjoy the benefits of Abstarct Factory else, Factory should be just fine for you.
C: hmmm..
A: ok
Hope you like the dialogue....:)

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Design Pattern | General | MAPI | Refactoring

A brief Introduction to WCF

by ipsit 4. May 2009 17:34

Just published a brief intro to WCF. Its a birds view of the awesome wcf framework. Do comment on it and let me know how to further improve it.

http://coderslog.info/page/WCF-Introduction-2.aspx

:) 

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General | WCF

A Wrapper Around Messaging Queue

by sanket 27. April 2009 12:30
Came across a need to write a utility using Messaging Queue. It is kind of a Publisher Subscriber, but it not durable messaging at all. Just a basic beginning for any body who wants to use MSMQ, can use the following code.
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Messaging;
   4:   
   5:  namespace Coderslog.Utilities.MsmqUtilities
   6:  {
   7:      public class Queue
   8:      {
   9:          private MessageQueue                _internalQueue;
  10:          private readonly List<GetMessage>   _messageSubScribers = new List<GetMessage>();
  11:   
  12:          public static Queue CreateQueue(string queueUri)
  13:          {
  14:              var queueInst = new Queue
  15:                                  {
  16:                                      _internalQueue =
  17:                                          !MessageQueue.Exists(queueUri)
  18:                                              ? MessageQueue.Create(queueUri)
  19:                                              : new MessageQueue(queueUri)
  20:                                  };
  21:              return queueInst;
  22:          }
  23:   
  24:          private void _internalQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
  25:          {
  26:              if (e.Message == null || !(e.Message.Body is QueueMessage)) return;
  27:              foreach (var subscriber in _messageSubScribers)
  28:              {
  29:                  if (subscriber != null && subscriber.Target != null)
  30:                      subscriber((QueueMessage) e.Message.Body);
  31:              }
  32:          }
  33:   
  34:          /*To Force Users to create a queue using the above method*/
  35:          private Queue()
  36:          {
  37:              _internalQueue.ReceiveCompleted += _internalQueue_ReceiveCompleted;
  38:          }
  39:   
  40:          public void PutMessage(QueueMessage message)
  41:          {
  42:              _internalQueue.Send(message);
  43:          }
  44:   
  45:          public void Subscribe(GetMessage messageReciver)
  46:          {
  47:              _messageSubScribers.Add(messageReciver);
  48:          }
  49:   
  50:          public void InitRecieving()
  51:          {
  52:              _internalQueue.BeginReceive();
  53:          }
  54:      }
  55:   
  56:   
  57:      public delegate void GetMessage(QueueMessage message);
  58:   
  59:      [Serializable]
  60:      public class QueueMessage
  61:      {
  62:          public string Header { get; set; }
  63:          public string Body { get; set; }
  64:      }
  65:  }

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Timed Operation

by ipsit 12. March 2009 10:49

I was looking for a Timed Method Implementation, which could notify me if the method is taking more than the stipulated time. I found an excellent  article by Stephen Toub at http://msdn.microsoft.com/en-us/magazine/cc163768.aspxJust modified it a bit. Please find it below. Do go to the link and read it, it’s a good read.    

public delegate void CompensatingDelegate();    

public class TimedOperaton    {       

public static object Invoke(               

TimeSpan             time,               

Delegate             method,

CompensatingDelegate compensationMethod,               

params object[]      parameters           

){           

if (method == null) throw new ArgumentNullException("method");           

if (time.TotalSeconds <= 0) throw new ArgumentOutOfRangeException("time");            

var timeBoxedOperation          = new Operation();           

timeBoxedOperation.Method       = method;           

timeBoxedOperation.Parameters   = parameters;            

var thread = new Thread(new ThreadStart(timeBoxedOperation.Run));           

thread.IsBackground = true;           

thread.Start();            

if (!thread.Join(time)){               

if (compensationMethod != null)                   

compensationMethod();               

if (timeBoxedOperation.Exception != null)                   

throw timeBoxedOperation.Exception;               

return null;           

}           

if (timeBoxedOperation.Exception != null)throw timeBoxedOperation.Exception;           

return timeBoxedOperation.Result;       

}        

private class Operation{           

public volatile Delegate    Method;

public volatile object[]    Parameters;

public volatile Exception   Exception;

public volatile object      Result;

public void Run(){

try{                   

this.Result = Method.DynamicInvoke(Parameters);               

}catch (ThreadAbortException ex){

this.Exception = ex;               

}catch (Exception exc){

this.Exception = exc;

}           

}       

}

}

Till then Happy Coding.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General


Inida

About Coderslog

One fine day we thought, we need to be contributing to our community.

So good sense prevailed, and that's how coderslog got started. That's about us.

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

RecentPosts

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 CodersLog