Fragment is a modular section of an Activity that has it's own lifecycle, receives its own input events, which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities)
Apart from the obvious advantage of using fragments, UI optimization across different screens, it lets you manage activity's background processing without a visible user interface component.
Now...
====> Why would I complicate my life, coding more...??
Albeit recommended, you don't need to unless you plan to control the lifecycle of individual elements and/or reusing the stack-state or history of previous views.
I would discourage you to ever use null
since it can lead to a further NPE, which are hard to debug (and cost a lot if they occur in production code).
Solution 1 (overload method)
If no deviceName
is provided, you can provide a default one instead. The biggest disadvantage from this approach is the danger in genericDeviceMap.put(deviceName, device)
because it can silently override the entry whose key is the default name (therefore, losing track of the previous Device
).
public void attachDevice(Device device)
{
attachDevice(device, "DefaultName");
}
public void attachDevice(Device device, String deviceName)
{
..
device.setName(deviceName);
genericDeviceMap.put(deviceName, device);
..
}
Solution 2 (extract method)
Maybe that with your current architecture it doesn't make sense to add an entry to genericDeviceMap
when attachDevice
is called without a name. If so, a good approach is to only extract the common behaviour between the two attachDevice
into private methods. I personnally don't like this approach for 2 reasons:
- The behaviour between the two
attachDevice
is not the same, one has a side-effect (device.setName(deviceName)
) and the other not
- The side-effect in itself who often lead to subtle bugs because you alter an object who's coming from an outside scope
Code:
public void attachDevice(Device device)
{
preAttachDevice();
postAttachDevice();
}
public void attachDevice(Device device, String deviceName)
{
preAttachDevice();
device.setName(deviceName);
genericDeviceMap.put(deviceName, device);
postAttachDevice();
}
private void preAttachDevice()
{
...
}
private void postAttachDevice()
{
...
}
Solution 3 (remove method)
My favorite, but the hardest. Ask yourself if you really need these two methods ? Does it make really sense to be able to call attachDevice
either with a name or not ? Shouldn't you be able to say that attachDevice
must be called with a name ?
In this case the code is simplified to only one method
public void attachDevice(Device device, String deviceName)
{
..
device.setName(deviceName);
genericDeviceMap.put(deviceName, device);
..
}
Or on the other hand, do you really need to maintain a Map
of devices and devices names and set the device's name ? If not, you can get rid of the second method and only keep the first one.
public void attachDevice(Device device)
{
...
...
}
Best Answer
If you need to pass only the URL, then pass only the URL. No need to over think it.
There are several disadvantages of passing the whole object:
If you only need to pass one field of many, then most certainly it's best to pass just the one field.
If you need to pass a few fields of many, then it's probably still better to pass only the needed fields, for the above reasons.
If you need to pass two product fields out of three, then it might be acceptable to pass the entire product.