none
Customization of Design pattern- Visitor Design pattern. RRS feed

  • Question

  • Can we make customization in the design patterns and use.

    For example , If I would like to use the visitor design pattern for my application , the mechanisam suits the visitor design pattern partially.

    But the visitor design pattern can be used with the collection of objects with different types( but with the same base type).

    Now I want to use this design pattern on a single object instead of a collection of objects as traditionally its used.

    Can we do like that..and still call it as Visitor Design pattern..!!

    Friday, November 18, 2011 8:46 AM

Answers

  • One of the key aspects of the visitor pattern is working with collection (hierarchies) of objects of one base type where the collection (hierarchy) decides whether a visitor is accepted or not. And also hiding necessary casts in the visitor class. So you can do it, but imho it is then a degenerated adapter pattern. E.g.

     

    interface IElement {}
    class Subject: IElement {}
    
    class DegeneratedVisitorOrAdapter
    {
      void VisitAdapt(IElement element)
      {
        Subject subject = element as Subject;
        if (subject != null) { // your code... }    
      }
    }
    
    // with
    static void Main()
    {
      Subject mySubject = new Subject();
      DegeneratedVisitorOrAdapter myDvoa = new DegeneratedVisitorOrAdapter();
      myDvoa.VisitAdapt(mySubject);
    }
    


     


    • Edited by Stefan Hoffmann Friday, November 18, 2011 10:04 AM typo
    • Marked as answer by Surendran08 Friday, November 18, 2011 10:26 AM
    Friday, November 18, 2011 10:04 AM
  • Can we make customization in the design patterns and use.

    ...and still call it as Visitor Design pattern..!!

    Hi,

    Of course you can customise the design pattern. It's a pattern not a law. If the solution works use it.

    I don't quite agree with Stefans statement that the objects need to be of a single base type; however again it's a pattern not a law so it's not that I disagree in any negative sense.  It's all good. This code...

      void VisitAdapt(IElement element)
      {
        Subject subject = element as Subject;
        if (subject != null) { // your code... }   
      }

    Could have been written....

      void Visit(Subject subject)
      {
            // your code...
      }

    Then there would be no need for any casting, method overloading would handle the details. This has some implications like for example how would this code work if at all....

      void Visit(IElement element)
      {
             // your code...
             // if Subject implemented IElement would this be called?
      } 

      void Visit(Subject subject)
      {
              // your code...
      }

    So it might be all types of IElements would needs to handled.

     

     

     


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    • Marked as answer by Surendran08 Monday, November 28, 2011 11:44 AM
    Friday, November 18, 2011 11:41 AM

All replies

  • One of the key aspects of the visitor pattern is working with collection (hierarchies) of objects of one base type where the collection (hierarchy) decides whether a visitor is accepted or not. And also hiding necessary casts in the visitor class. So you can do it, but imho it is then a degenerated adapter pattern. E.g.

     

    interface IElement {}
    class Subject: IElement {}
    
    class DegeneratedVisitorOrAdapter
    {
      void VisitAdapt(IElement element)
      {
        Subject subject = element as Subject;
        if (subject != null) { // your code... }    
      }
    }
    
    // with
    static void Main()
    {
      Subject mySubject = new Subject();
      DegeneratedVisitorOrAdapter myDvoa = new DegeneratedVisitorOrAdapter();
      myDvoa.VisitAdapt(mySubject);
    }
    


     


    • Edited by Stefan Hoffmann Friday, November 18, 2011 10:04 AM typo
    • Marked as answer by Surendran08 Friday, November 18, 2011 10:26 AM
    Friday, November 18, 2011 10:04 AM
  • Thanks a lot.

     


    Surendran Anthony.
    Friday, November 18, 2011 10:26 AM
  • Can we make customization in the design patterns and use.

    ...and still call it as Visitor Design pattern..!!

    Hi,

    Of course you can customise the design pattern. It's a pattern not a law. If the solution works use it.

    I don't quite agree with Stefans statement that the objects need to be of a single base type; however again it's a pattern not a law so it's not that I disagree in any negative sense.  It's all good. This code...

      void VisitAdapt(IElement element)
      {
        Subject subject = element as Subject;
        if (subject != null) { // your code... }   
      }

    Could have been written....

      void Visit(Subject subject)
      {
            // your code...
      }

    Then there would be no need for any casting, method overloading would handle the details. This has some implications like for example how would this code work if at all....

      void Visit(IElement element)
      {
             // your code...
             // if Subject implemented IElement would this be called?
      } 

      void Visit(Subject subject)
      {
              // your code...
      }

    So it might be all types of IElements would needs to handled.

     

     

     


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    • Marked as answer by Surendran08 Monday, November 28, 2011 11:44 AM
    Friday, November 18, 2011 11:41 AM
  • Thanks Derek.
    Surendran Anthony.
    Monday, November 28, 2011 11:45 AM