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

SQL Locks - Part 3

by sanket 16. February 2009 18:44

Having outlined an approach, even if its not a approapriate one, we are now intrested in seeing a better solution that is much cleaner than the previous one.

As we re-collect from the previous post our generalized problem was, "There is a addition to behaviour in some scenarios".

We decided to use a Decorator.

What we did was created a interface called IWebPageGenerator, which had a method Generate. Now, the original class was made to  implement from the interface IWebPageGenerator.And all the calls we initiated in the original class was from the Generate Method.

As for the scenario which required a Security check what we did was,

/*           SecurityDecorator                    */

public class SecurityDecorator:IWebPageGenerator{

      public IWebPageGenerator ChildWebPageGenerator {get; private set;}

      public SecurityManager _securityManager;

      public SecurityDecorator(IWebPageGenerator childWebPageGenerator){

             ChildWebPageGenerator = childWebPageGenerator;

             _securityManager = SecurityManager.Instance.Context(Privelege.Metadata.Update);

      }

      public string Generate(){

             _securityManager.Verify();

             return ChildWebPageGenerator.Generate();  

      }

}

/*               Usage                            */

var  webPageGenerator = new SecurityDecorator(webPageGenerator);

/*                                                */

As you can see what this approach brings in is lesser number of code changes. And a more cleaner code base. And the bonus is a better infrastructure of Web Pages Generator which is flexible and extensible.

- Till then Happy Coding :)

Be the first to rate this post

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

Tags:

Refactoring | Design Pattern

SQL Locks-Part 2

by sanket 12. February 2009 16:58

As I mentioned in the previous post, we choose the path of Refactoring the code to rectify the problem we were facing.

Let me restate the problem:

"We have a application where in there is a process called publish. What publish does is,

  • It generates/prepares the XML of the Business Entities.
  • That XML is the Model. This model is given to the Generator
  • The Generator,generates DTO's and generates API for them.
  • Then the request goes to the Render library, where in the Webpages or the UI is generated.
  • Then the SQL Scripts are generated and then they are run against the database.

All this happens inside a Transaction so that we can rollback in case of error.

All was hanky dory untill recently.  The Web Page generator store is used from couple of other places. So due those scenarios, we had to introduce a check for security before allowing it to be generated. "

Let me generalize the problem a bit more. There is a addition to behaviour in some scenarios.What are the probable ways in solving this problem?

An if condition, which takes a decision on basis of a context variable whether Verify needs to be called

For Example:

//... do something

if(!_publishUnderProgress) _securityManager.Verify();

//... do something

Problems:

We might have to add few more conditions, or few more checks if the same class is used in a different context.

Notice the number of places where our logic is scattered.

  • Establish Context From the Publisher Not to Use Verify
  • Establish Context From other locations to Use Verify.
  • Make changes in the WebPageGenerator

Bottom line is, its not a clean solution.

We will outline a better way of solving it the next time.

 

Be the first to rate this post

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

Tags:

Refactoring

Maintenance! Phew how I used to dislike it...

by ipsit 31. October 2008 05:51

Most of the interviews that we generally tend to attend, we always try to make sure, that we are going for a job role, where in the kind of work is more towards development. And I myself have done this. But untill, recently, there was a task, that had been already written, and it badly needed extension. This piece of code which was already built was doing what it was meant for, effectively.

But the changes that were required, had few contracdictions with its essential design or the core design. Now the challenge was to get the piece moving or doing which it was already doing and then on top of it, add few more extensions to it. A typical maintenance scenario... 

I just questioned my self, was it that I was afraid to tackle such a situation earlier or whether, it was like, i was ignorant of this aspect of Software development. Just to brighten up this idea, I would start up a series to take scenarios and then try to make changes to it and learn and understand the underlying principles of the same...

Happy Coding!

Currently rated 3.0 by 1 people

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

Tags:

.Net 3.0 | Refactoring


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