Best Accessor and Mutator Pattern implementation?

This is a discussion on Best Accessor and Mutator Pattern implementation? within the Tech Board forums, part of the Community Boards category; Passing any reference into/from accessor and mutator IS forbidden. Why? Let say we have a Rectangle class which aggregates Point ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Question Best Accessor and Mutator Pattern implementation?

    Passing any reference into/from accessor and mutator IS forbidden.

    Why?

    Let say we have a Rectangle class which aggregates Point and Dimension class.
    Code:
    class Rectangle {
    public:
      virtual Point *getLocation(void);
      /* OR
      virtual Point &getLocation(void); */
    };
    So any client who use Rectangle object will be able to modify the Point object
    WITHOUT notifying the Rectangle object, and this is violate the accessor and mutator pattern for sure.
    Code:
    Point *point = rectangle->getLocation();
    point->setX(point->getX() + 1); // BOOM!
    This is also become a common mistake of Java programmers who doesn't know the concept, since every object are passed by reference, not a copy.

    Code:
    class Aggregator {
      private Aggregated aggregated;
    
      public Aggregated getAggregated() {
        return aggregated; // << WRONG WAY!
      }
    
      /* unless you do this
      public Aggregated getAggregated() {
        return aggregated.clone();
      }*/
    }
    .
    .
    Aggregated aggregated = aggregator.getAggregated();
    aggregated.setAProperty( anotherProperty ); // BOOM!
    This is useless getter and setter, since the Aggregator know nothing whenever the Aggregated is modified, and we loss the flexibility and reusability terms.

    But there is a drawback or complex side effect when we copy every single thing to conform this problem, consider an example below.

    Code:
    class AnotherClass {
      YetAnotherClass *pYetAnotherClass;
    };
    
    class SomeClass {
      AnotheClass *pAnotheClass;
    };
    
    class SomeClassAggregator {
    public:
      virtual SomeClass getSomeClass(void) {
        //exploding brain detected!!
      }
    };
    And performance loss problem.

    Code:
    class ComplexClass {
      List<HugeClass> veryHugeHugeClassList;
    
      /**
       * @return returns a copy of very huge HugeClass list.
       */
      virtual List<HugeClass> getVeryHugeHugeClassList(void);
    };
    .
    .
    class ComplexClassContainer {
      List<ComplexClass> veryHugeComplexClassList;
    
      /**
       * @return returns a copy of very huge ComplexClass list.
       */
      virtual List<ComplexClass> getVeryHugeComplexClassList(void);
    };
    What will you do to solve this kind of design problem?

    However I'm using C++ codes here just for example.
    This post is generic enough and can be implemented in any other language.

    Thanks in advance.
    Last edited by audinue; 08-14-2009 at 02:55 AM.
    Just GET it OFF out my mind!!

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Ohh, hell yeah maybe I know something..

    Code:
    class Rectangle {
      const Point &getLocation(void) const; //Is it possible?
    };
    But how about the mutator and the huge list problem?
    Last edited by audinue; 08-14-2009 at 02:52 AM.
    Just GET it OFF out my mind!!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21