• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why?  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have some bunch of basic queries... I'm not clear about these.
Please clarify
1.Why we need Factory pattern & in which situation we implement?
2.Why we need Command pattern & in which situation we implement?
3.Differentiate Interceptor Pattern/Filter Pattern/Front Controller Pattern

Thanks in adv.

Regards, V
 
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we use factory pattern to ensure objects are initialied in some way after they are created.
 
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mr. C Lamont Gilbert:
we use factory pattern to ensure objects are initialied in some way after they are created.


Factory or factory method pattern?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have some bunch of basic queries... I'm not clear about these.
Please clarify
1.Why we need Factory pattern & in which situation we implement?
2.Why we need Command pattern & in which situation we implement?
3.Differentiate Interceptor Pattern/Filter Pattern/Front Controller Pattern


1) Factory Method is used primarily when you have a a need to create different types of objects, with a common interface, based on conditional logic. You use it by extracting the "creation" step into methods on your class, then creating subclasses to contain the common threads of logic. There's an excellent example in Head-First Design Patterns.

Static Factory, and Factory Object, by contrast, are usually applied when there is more involved in creating an object than simply calling 'new'. This often occurs when you need to do post-construction calls, such as registering listeners.

2) Command pattern is used when you have a request-dispatch situation. You can recognise these by code where you have a large conditional, checking for different values of a common parameter. You can evolve the Command Pattern with a few simple steps:
  • Extract the body of each conditional step to its own method.
  • Extract the method bodies themselves out to new Method Objects
  • Give the Method Objects a common interface, with a simple method such as 'execute' on it.
  • Use the dispatching conditional to create the right method object. Invoke the 'execute' method outside of the conditional.
  • Replace the conditional with some other way of looking up the command (e.g. a Map)


  • For bonus points, configure the Map (or whatever) via an external configuration file.

    The command pattern most commonly comes into play when you have a natural request-dispatch situation. Web apps are a good example. Other times when the command pattern is useful is when you wish to give invokable behaviour to another object (e.g. the "onClick" event on a Swing button).

    When using the Command pattern, you need to be careful to avoid the "Anemic Domain" anti-pattern. This occurs when you put excessive domain logic into the commands, rather than the domain objects. As a rule, try to limit Command objects to the following steps:
    * Find (or make) an instance of a domain object.
    * Invoke a method on said domain object instance.
    * Return the domain object to a repository for persistence.

    If you find yourself writing a lot of Command objects (more than, oh, about a dozen), you've probably fallen victim to the "Anemic Domain" anti-pattern.

    3) You assume that there is a difference... avoid that assumption, and look for points of similarity. The differences will stand out by contrast.

    Also remember that using patterns isn't like a Bingo game - the object is to get the job done, not see how many patterns you can implement, or how closely to the pattern template you adhere.
    [ September 20, 2005: Message edited by: Robert Watkins ]
     
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    With all due respect, this sounds a lot like a homework question. You will learn a lot more if you first try to answer those for yourself. You can then present your answers here and certainly get a lot more valuable feedback than just being spoonfed...
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!