Functions: Objects or many parameters - OOP - oop

I have a question to OOP in general. I'd like to demonstrate a snippet of code:
public class Test
{
private int someInt = 0;
public Test()
{
this.method1(this);
this.method2(this.getSomeInt());
}
public void method1(Test test)
{
System.out.print(test.getSomeInt());
}
public void method2(int someInt)
{
System.out.print(someInt);
}
public int getSomeInt()
{
return this.someInt;
}
}
My question here: Which of those two function calls is more efficient? Does it matter if I use the getter as a parameter or should I give the object as the parameter from the beginning? Could you explain which one I should prefer because I don't really know what to stick to. The object seems to be too big as a parameter, the parameters look too cascaded. I apologize in advance if any related question has been asked already, I didn't know what to look for. ,_,

Related

SOLID - Violated Open-Closed principle

I have the following code snippet
class Vehicle{
public String brand;
public double price;
public int productionYear;
public String toString(String formatType) {
switch(formatType) {
case "JSON": // JSON formatting here
return jsonFormattedString;
break;
case "XML": // XML formatting here
return xmlFormattedString;
break;
default: // default formatting
return defaultFormattedString;
}
}
I believe that the problem with this approach is the need of changing
the source code if the behaviour changes (another formatting type);
and maybe other SOLID violations that I missed.
How can this be better implemented?
What I would do is introduce another class for "Exporting" your vehicle.
Something like this.
public class VehicleExporter
{
public string ExportAsJson(Vehicle vehicle)
{
// Do the JSON thing
}
public string ExportAsXML(Vehicle vehicle)
{
// Do the XML thing
}
}
The big thing with your design is not as much breaking the open closed principle, but the responsibility of the vehicle class.
When your class is doing the toString(), it is essentially trying to do something outside of it's responsibility.
Please let me know if I can clarify further.

Naming convention when the method signature is the same?

Hi when you have a method with same signature let's say.
void getErrorMessage(int errorCode){
}
void getErrorMessage(int domain){
}
I know I have to change the name or differentiate the parameter but what would be the best way to approach?
---------------------------Edited.
How about for constructor?
For example
public ErrorMessage(int errorCode){
}
public ErrorMessage(int domain){
}
You could make the method name explicit:
getErrorByErrorCode(int errorCode)
And
getErrorByDomain(int domain)
You can add a "ByFoo" to the end of the method, like: getErrorMessageByCode or getErrorMessageByDomain
The least dangerous and easiest-to-understand way to do this is:
public class ErrorCode {
private int intCode;
ErrorCode(int intCode) {
this.intCode = intCode;
}
int getIntegerCode() {
return intCode;
}
}
public class Domain {
private int domain;
Domain(int domain) {
this.domain = domain;
}
int getIntegerCode() {
return domain;
}
}
Message getErrorMessage(ErrorCode errorCode)
Message getErrorMessage(Domain domain)
Note the classes are immutable. You should probably also override equals. Use these classes everywhere you would have used the integer values.
Now it is impossible to mistake an error code for a domain anywhere in your code. If you mistake one for the other you will get a compiler error, and the compiler will choose the correct implementation of getErrorMessage. You extract the integer value from the object only when you need to perform integer operations on it.

Is there an OOP convention about private method signatures - directly using fields or getting arguments?

I'd like to know which of the following is a better OOP practice when having private methods. Should the fields be used directly, or given to the method like an argument?
class MyClass {
private int myNumber;
public MyClass(int number) {
this.setMyNumber(number);
}
public void setMyNumber(int number) {
this.myNumber = number;
}
public int getMyNumber() {
return this.myNumber;
}
public int getTransformedNumber() {
return transformNumberInSomeWay1();
// OR
return transformNumberInSomeWay2(this.getMyNumber());
}
private int transformNumberInSomeWay1() {
int number = this.getMyNumber();
<... transformation of the number ...>
return number;
}
private int transformNumberInSomeWay2(int number) {
<... transformation of the number ...>
return number;
}
}
If I'm using the transformation number method for other things than the field myNumber, the second way is better. What about the case when I'm not actually using it for anything else, yet I don't know how will the development of the class go? Should I always do it the second way or let the people who need it, change it later on?

Accesing arraylist property from another class using constructor

So i have a class that makes an array list for me and i need to access it in another class through a constructor but i don't know what to put into the constructor because all my methods in that class are just for manipulating that list. im either getting a null pointer exception or a out of bounds exception. ive tried just leaving the constructor empty but that dosent seem to help. thanks in advance. i would show you code but my professor is very strict on academic dishonesty so i cant sorry if that makes it hard.
You are confusing the main question, with a potential solution.
Main Question:
I have a class ArrayListOwnerClass with an enclosed arraylist property or field.
How should another class ArrayListFriendClass access that property.
Potential Solution:
Should I pass the arraylist from ArrayListOwnerClass to ArrayListFriendClass,
in the ArrayListFriendClass constructor ?
It depends on what the second class does with the arraylist.
Instead of passing the list thru the constructor, you may add functions to read or change, as public, the elements of the hidden internal arraylist.
Note: You did not specify a programming language. I'll use C#, altought Java, C++, or similar O.O.P. could be used, instead.
public class ArrayListOwnerClass
{
protected int F_Length;
protected ArrayList F_List;
public ArrayListOwnerClass(int ALength)
{
this.F_Length = ALength;
this.F_List = new ArrayList(ALength);
// ...
} // ArrayListOwnerClass(...)
public int Length()
{
return this.F_Length;
} // int Length(...)
public object getAt(int AIndex)
{
return this.F_List[AIndex];
} // object getAt(...)
public void setAt(int AIndex, object AValue)
{
this.F_List[AIndex] = AValue;
} // void setAt(...)
public void DoOtherStuff()
{
// ...
} // void DoOtherStuff(...)
// ...
} // class ArrayListOwnerClass
public class ArrayListFriendClass
{
public void UseArrayList(ArrayListOwnerClass AListOwner)
{
bool CanContinue =
(AListOwner != null) && (AListOwner.Length() > 0);
if (CanContinue)
{
int AItem = AListOwner.getAt(5);
DoSomethingWith(Item);
} // if (CanContinue)
} // void UseArrayList(...)
public void AlsoDoesOtherStuff()
{
// ...
} // void AlsoDoesOtherStuff(...)
// ...
} // class ArrayListFriendClass
Note, that I could use an indexed property.

EasyMock Testing Void With Runnable

I'm trying to test the following class (I've left out the implementation)
public class UTRI implements UTR {
public void runAsUser(String userId, Runnable r);
}
This is the way I would use it:
UTRI.runAsUser("User1", new Runnable () {
private void run() {
//do whatever needs to be done here.
}
});
The problem is, I don't know how to use EasyMock to test functions that return void. That and I'm also not too familiar with testing in general (right out of school!). Can someone help explain to me what I need to do to approach this? I was thinking about making the UTRI a mock and doing expectlastcall after that, but realistically, not sure.
public class UTRITest {
UTRI utri = new UTRI();
#Test
public void testRunAsUser() {
// Create Mocks
Runnable mockRunnable = EasyMock.createMock(Runnable.class);
// Set Expectations
**mockRunnable.run();
EasyMock.expectLastCall().once();**
EasyMock.replay(mockRunnable);
// Call the method under test
utri.runAsUser("RAMBO", **mockRunnable**);
// Verify if run was called on Runnable!!
EasyMock.verify(mockRunnable);
}
}