473,287 Members | 3,228 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

OOP: Swap Method

9
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(objectB) [returns new B and modifies A]

-or-

b.) objectHelper.Swap(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).
Nov 21 '07 #1
9 1468
r035198x
13,262 8TB
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(objectB) [returns new B and modifies A]

-or-

b.) objectHelper.Swap(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.
Nov 21 '07 #2
fsx
9
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 ;-)
Nov 21 '07 #3
r035198x
13,262 8TB
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.
Nov 21 '07 #4
fsx
9
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.
Nov 21 '07 #5
r035198x
13,262 8TB
So is it common to have a lot utility classes? ...
That's not what I meant at all.
Nov 21 '07 #6
fsx
9
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.SwapDrivers(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!!!
Nov 21 '07 #7
r035198x
13,262 8TB
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.SwapDrivers(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.
Nov 21 '07 #8
fsx
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.
Nov 22 '07 #9
r035198x
13,262 8TB
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.
Nov 22 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: Xah Lee | last post by:
in computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that } in advanced languages such as LISP family, it is not uncommon...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
16
by: Manuel | last post by:
Please, excuse me for starting this discussion. I'm not a troll, and I love the OOP architecture. However I'm a C++ newbie, and sometimes it's hard for me to fully understand the improvement of...
10
by: Nemisis | last post by:
HI everyone, I think i am on the right path but if someone could confirm it, that would be great. In my business Layer i have my business object, for this example, called User. User has...
22
by: Pramod | last post by:
Hello to all of you, basically i want a program how to swap value...i know 2 method 1st --- temp=a; a=b; b=temp; 2nd---- a=a+b; b=a-b; a=a-b;
7
by: sachsase | last post by:
//swap of 2 variables void main() { int a=27,b=46; a=1?a-(b=1?((a=1?a+b:0)-b):0):0; printf("a=%db=%d",a,b); } I got output for this problem as a=46;b=27 Same but using scanf to read...
14
by: mesut | last post by:
hi colleagues, I don't know if this is the right group for but it's in C# so I try. I have a #3 procedural function called GetInfo.. and those are 3 overloaded methods. I would like to use the...
0
by: christian2.schmidt | last post by:
Hi, I tried to implement the std::swap method on properties (and indexers) without success. The problem is that list calls the getter and returns a copy and the assignment in the swap method...
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.