7

I would like to create a base class that all classes in my program will extend. One thing I wanted to do was find a uniform way to store all instance variables inside the object.

What I have come up with is to use a HashMap to store the key/value pairs for the object and then expose those values through a get and set method.

The code that I have for this so far is as follows:

package ocaff;

import java.util.HashMap;

public class OcaffObject {

    private HashMap<String, Object> data;

    public OcaffObject() {
        this.data = new HashMap<String, Object>();
    }

    public Object get(String value) {
        return this.data.get(value);
    }

    public void set(String key, Object value) {
        this.data.put(key, value);
    }

}

While functionally this works, I am curious if there are any real issues with this implementation or if there is a better way to do this?

In my day to day work I am a PHP programmer and my goal was to mimic functionality that I used in PHP in Java.

1
  • 3
    You could do that, but why? If you want to pass around a hash map, just pass around a hash map. IMO you lose the benefits if a lot of what OOP is if you just throw everything into a map, though. Commented Oct 21, 2011 at 23:36

2 Answers 2

14

I don't think this is a good way to deal with what you mean. Programming in java is quite different than programming in php, from my point of view.

You need to keep things clean and strongly typed, using the real paradigm of clean object oriented programming.

Some problems with this technique comes to my mind, here are some, not in importance order.

  1. First problem you have with this is performance and memory footprint: this will consume a lot of memory and will perform very badly.

  2. Second problem is concurrency, HashMap is not thread safe.

  3. Third problem is type safety: you don't have type safety anymore, you can write to a field whatever you want and no one is checking it, a real anti-pattern.

  4. Fourth problem is debugging... it will be hard to debug your code.

  5. Fifth problem is: everyone can write and read any field knowing his name.

  6. Sixth problem: when you change the name of a field in the hash set you don't get any kind of compile time error, you only get strange run-time behavior. Refactoring will become impossible.

Typed fields are much more useful and clean.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 I like your listing of problems. I particularly think 3, 4, 5 and 6 are more important than 1 and 2, but even those 2 are important to be aware of.
Yes agree, the last points are more important :) but performance can be an issue when this is a base class for class User for example and you have 1000000 users online :) lol :)
This is exactly the type of information I was looking for. I am glad I asked before I got too far along in my project. Thank you.
To me ... arguing about which of those points is more important is like arguing whether it is more important for a car to have tread on the tyres or gas in the tank. It is a matter of perspective.
1

If you're taking the time to make a class for this, I would simply add what you need as members of the class. This will give you compile time checking of your class members, greatly reducing your subtle bugs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.