OOP: Swap Method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fsx
    New Member
    • Nov 2007
    • 9

    OOP: Swap Method

    Which would be the best way to implement a swap method in OOP? Basically the method just swaps (some) values of the same object type

    a.) objectB = objectA.Swap(ob jectB) [returns new B and modifies A]

    -or-

    b.) objectHelper.Sw ap(objectA, objectB) [passing each object by ref]

    -or-

    c.) ????

    Don't worry about what's actually getting swapped, I'm really interested in the best OOP method (low coupling, etc).
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by fsx
    Which would be the best way to implement a swap method in OOP? Basically the method just swaps (some) values of the same object type

    a.) objectB = objectA.Swap(ob jectB) [returns new B and modifies A]

    -or-

    b.) objectHelper.Sw ap(objectA, objectB) [passing each object by ref]

    -or-

    c.) ????

    Don't worry about what's actually getting swapped, I'm really interested in the best OOP method (low coupling, etc).
    The OOP way to do swap? That's a strange question.
    Well what you should be using here is cloning, copy constructor or both.

    Comment

    • fsx
      New Member
      • Nov 2007
      • 9

      #3
      Method B is the how I would code it if I was using a non-OOP language.

      Method A looks like OOP to me, but then I'm not an expert - which is why I'm asking ;-)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by fsx
        Method B is the how I would code it if I was using a non-OOP language.

        Method A looks like OOP to me, but then I'm not an expert - which is why I'm asking ;-)
        There's a bit more to OOP than just how a method call looks like.
        Swap doesn't really sound right as an action that can an object can perform. However if you have a class called ObjectUtils then Swap is a nice place for it.
        Having said that, remember that by default object references are passed by value. To Swap objects using a Swap method you'll need to pass by reference. Do consider having a look at cloning (Copying) and or creating objects using the copy constructor.

        Comment

        • fsx
          New Member
          • Nov 2007
          • 9

          #5
          So is it common to have a lot utility classes? I was trying to avoid creating utility classes, but maybe I was being too strict and putting too much in the non-utility classes.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by fsx
            So is it common to have a lot utility classes? ...
            That's not what I meant at all.

            Comment

            • fsx
              New Member
              • Nov 2007
              • 9

              #7
              Sorry, I'm not trying to be annoying!

              I have a system that has Car objects and Driver objects. The Drivers "swap" Cars, so I wasn't sure if I should use:

              carA.SwapDriver (carB) ["Stateful" method in the Car Class]
              - or -
              CarUtility.Swap Drivers(carA, carB) [Shared method in the CarUtility Class]
              - or -
              carA.SwapDriver (carA, carB) [Shared method in the Car Class]

              As it's a new project I didn't want to make a bad design decision at the start!

              Thanks!!!

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by fsx
                Sorry, I'm not trying to be annoying!

                I have a system that has Car objects and Driver objects. The Drivers "swap" Cars, so I wasn't sure if I should use:

                carA.SwapDriver (carB) ["Stateful" method in the Car Class]
                - or -
                CarUtility.Swap Drivers(carA, carB) [Shared method in the CarUtility Class]
                - or -
                carA.SwapDriver (carA, carB) [Shared method in the Car Class]

                As it's a new project I didn't want to make a bad design decision at the start!

                Thanks!!!
                The Car is not the one that does the Swap action is it? It's the Driver who swaps cars. So driver.SwapCar( Car newCar) is the way to go here. That SwapCar method really belongs to the driver not to the car.

                Comment

                • fsx
                  New Member
                  • Nov 2007
                  • 9

                  #9
                  Thanks - that makes sense.

                  So is it common to have a stateful class (i.e. Driver) that also has some unstateful (shared) methods? It looks like an obvious place to store them rather than break them out into a seperate class full of shared methods.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by fsx
                    Thanks - that makes sense.

                    So is it common to have a stateful class (i.e. Driver) that also has some unstateful (shared) methods? It looks like an obvious place to store them rather than break them out into a seperate class full of shared methods.
                    Actually form your scenario, the SwapCar mathod should not be shared at all. The only objects that should be allowed to SwapCars is an object of type Driver. The method SwapCar should thus be in the Driver class and not be made static. No other objects should have access to the SwapCar action unless those objects are Drivers.

                    Comment

                    Working...