This is a trivial programming question. I am not an expert in Java. Say I use objects of custom classes Company and Employee, in a manner similar to what many RDBMS examples do:
class Employee
{
Company company;
}
class Company
{
String name;
}
I need to guarantee that different Company
objects have unique names - i.e. no two such objects may have the same name, because from my point of view it makes no sense, and also simply eats memory - if two employees work at IBM, then there is a single Company
object with that name
, period.
My thoughts right now go along of making Company
constructor private - so that the job of allocating Company objects with arbitrary names is delegated to a trusted method - which, suppose, will reject any subsequent attempt to create an object with a name that already exists or return an existing or new object (creating one if necessary).
The problem is, I am not sure how to accomplish this elegantly. One thing that would be nice is not having to do a O(n)
lookup every time an Company
object with a name is requested - so maybe a hash map or a binary tree is there for my convenience? I would also like to override the way the Company
objects are identified - which leads me to this: will I be overriding Object.equals
and/or Object.hashCode
methods?
The state of an object is a value from its data type. The identity of an object distinguishes one object from another. It is useful to think of an object's identity as the place where its value is stored in memory.
Each class has an associated instance ( Object. class for the Object class). This instance is unique per class loader. This means that class do have a unique identity inside a class loader.
Of course this includes also sharing the same memory address but in general while identity is related to the attributes of the object, equality is used to check whenever two objects are identical, but this doesn't include identity.
Java is an object-oriented programming language, meaning everything in Java is an object. Each object has a different name, and a class is unique in that they are used to create blueprints for objects. A class must have a unique name used to create individual class instances.
Take a look at the flyweight pattern.
What I would do is something like:
// incomplete, but you get the idea hopefully
CompanyFactory
{
private Map<String, Company> companies;
public getCompany(final String name)
{
Company company;
company = compaines.get(name);
if(company == null)
{
company = new Company(name);
companies.put(name, company);
}
return (company);
}
}
You could override equals
and hashCode
and store them in a HashMap
or HashSet
.
One thing that would be nice is not having to do a O(n) lookup every time an Company object with a name is requested - so maybe a hash map or a binary tree is there for my convenience?
That sounds right, yes.
I would also like to override the way the Company objects are identified - which leads me to this: will I be overriding Object.equals and/or Object.hashCode methods?
If you ensure that there are never two instances with the same key value, you actually don't have to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With