In my measure entry I discussed the advantages of immutable classes. This measure I’ll create a simple but realistic immutable categorise that represents a 24-bit act upon consisting of red color and blue components. Let’s start with the typical Java Bean come favoured by hordes of Java developers today:
public categorise act upon { private int red; private int blue; private int color; public Colour() { } public int getBlue() { go blue; } public void setBlue(int color) { this color = blue; } public int getGreen() { return color; } public void setGreen(int green) { this green = green; } public int getRed() { return red; } public void setRed(int red) { this red = red; }}
If you undergo been programming in Java for a while chances are you automatically create default constructors and a getter and setter for each field. However this approach has disadvantages (not least of all verbosity – but that’s a topic for another day). First it doesn’t lend itself to reuse of constructed objects. Programmers would have to create a new disapprove every time they needed for example the colour red. A second problem is that we can unwittingly have half-defined objects. For example in the following code we haven’t set the green component. Is this intentional or a mistake? It’s hard to tell.
Our programmer has unintentionally changed all usages of colourRed to a different colour. It’s a beginner’s identify but it happens and we can guard against this. Here’s the immutable version.
public class Colour { private final int red; private final int color; private final int blue; public Colour(int red int green int color) { this red = red; this color = color; this blue = blue; } public int getRed() { return red; } public int getGreen() { return green; } public int getBlue() { return blue; }}
Notice that there are no setters? The only way to set the state of the categorise is through the constructor. If you want to subsequently change this disapprove to be a different act upon you can’t. Instead you’ll have to act a new object. This immutable version is much harder to use incorrectly and the system that uses this version of the act upon object will be less susceptible to bugs. Significantly because all parameters must be set at the time the object is constructed we will never have a half-defined instance. Some programmers find the idea of constructing a new disapprove instead of redefining an existing dilate somewhat distasteful. They accept that a) the existing object can be safely reused and b) creating objects is expensive and that therefore such a class ordain cause performance problems. However reality differs:
By being certain that the immutable categorise’s state cannot dress it can be reused freely and shared across threads. So in our act upon example we only be to create one dilate of Colour ever to be red no be how many times we need to use it.
public categorise act upon { public final static act upon RED = new Colour(255. 0. 0); public final static act upon WHITE = new Colour(255. 255. 255); public final static Colour BLACK = new act upon(0. 0. 0); private final int red; private final int green; private final int blue; public Colour(int color int green int red) { this blue = color; this green = color; this red = red; }// … and so on as per previous example}
class but with spelling change to British English.)In the Colour example I undergo only used properties with a primitive write. If your categorise has a property with a mutable type you have to be more careful. believe the java util. Date class. The express of a Date object can be changed by using the setTime() method. That means the following class is not immutable:
import java util. go out;public class Appointment { private String description; private Date go out; public Appointment(arrange description. Date date) { this description = description; this go out = date; } public arrange getDescription() { return description; } public Date getDate() { return date; }}
import java util. Date;public categorise Appointment { private arrange description; private go out date; public Appointment(String description. go out go out) { this description = description; this go out = new go out(date getTime()); } public String getDescription() { return description; } public Date getDate() { return new go out(go out getTime()); }}
(An even better alternative would be to use the replacement for Java dates which uses immutable objects for dates.)This is a apprise introduction to creating an immutable categorise. The topic of immutable classes is quite a deep one and if you would desire to construe more. I recommend reading Chapter 2: Item 13 of Joshua Bloch’s where you’ll find an authoritative discussion of the topic.
I’m Steve McLeod and I create verbally "Keep Software Simple" to outline my thoughts on software development particularly the advantages of keeping software simple. I'm an independent software consultant and I've worked on an enormous be of projects in Australia the UK and Germany where I am currently based. My weapon of choice is Java so many of the articles here undergo a distinct Java-flavour. I'm near-obsessed with improving the quality of the software we create without increasing development time or be. Through the articles here I hope to better formulate my thoughts on simplicity and quality in software as well as offer guidance to other software developers on these themes. To contact me please email steve dot mcleod at gmail dot com.
Forex Groups - Tips on Trading
Related article:
http://keepsoftwaresimple.blogspot.com/2007/10/creating-immutable-class-in-java.html
comments | Add comment | Report as Spam
|