12 Replies - 2724 Views - Last Post: 17 February 2012 - 04:02 PM Rate Topic: -----

#1 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Enum or many subclasses?

Posted 10 February 2012 - 10:45 PM

Hi, guys! Question here. So I am wondering what are your thoughts on using an enum versus creating a base class and subclassing it for each value you would have put in an enum. I would normally go with the enum, but that requires you recompile that file every time you add a new value. If it is something like ice cream flavors, you can make a enum with all the known ice cream flavors, but every time you discover a new one, you have to recompile. Then again, I'm not sure I like the idea of 100+ classes extending Flavor... , but it would allow anyone using my code to subclass Flavor , without messing with an enum file... I don't know how to feel about this. Maybe I am overlooking some obvious solution?

In my situation, I want to store an enum of a bunch of known materials, like wood, stone, marble, etc. Each one may have a few properties assigned to it, but really they'll mostly be static constants.

Thanks in advance,
Sheph

Is This A Good Question/Topic? 0
  • +

Replies To: Enum or many subclasses?

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1148
  • View blog
  • Posts: 2,521
  • Joined: 05-May 05

Re: Enum or many subclasses?

Posted 11 February 2012 - 12:56 AM

If you've got a bunch of related types that will share the same properties and you don't expect those properties to change, I'd go with an enum. The Java Class library has some pretty large enums. IMHO, I don't think having to add a new constant is a significant maintenance problem. If you foresee that your types will require additional properties on top those define in your base class, I'd opt for a class hierarchy.

I'm interested in hearing others' opinions on this subject as well.
Was This Post Helpful? 2
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 11,068
  • Joined: 20-September 08

Re: Enum or many subclasses?

Posted 11 February 2012 - 03:20 AM

*
POPULAR

Quote

but it would allow anyone using my code to subclass Flavor , without messing with an enum file... I don't know how to feel about this. Maybe I am overlooking some obvious solution?


On the contrary, you're showing foresight in your analysis and design ;) I think your concerns are well-founded.

Consider a real user of your code. Are they really going to be happy to have to contact you for a new version every time they want to add (or subtract) a flavor? I certainly wouldn't be.

What you should do is design the code so that a simple declarative approach is all that is required to extend the flavor range. A simple Properties (file) + reflection mechansim should do it, for example.
Was This Post Helpful? 5
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1148
  • View blog
  • Posts: 2,521
  • Joined: 05-May 05

Re: Enum or many subclasses?

Posted 11 February 2012 - 03:49 AM

Quote

Consider a real user of your code. Are they really going to be happy to have to contact you for a new version every time they want to add (or subtract) a flavor? I certainly wouldn't be.


I hadn't considered a client of the API having the ability to define a new flavor. In that case, a class hierarchy would be best.

Quote

What you should do is design the code so that a simple declarative approach is all that is required to extend the flavor range. A simple Properties (file) + reflection mechansim should do it, for example.


I was originally going to say you could use an XML file and factory method pattern, so that you add new types (exposed through an interface) externally, but that rubbed me the wrong way. Generally, you see framework configuration files where you can select a specific implementation for some interface, like a data source or security provider. Usually that's modified by an administration system. Or you'll see XML or properties files that define global configuration data. You rarely ever see them used to create a bunch of classes. If you need to create a new component in the system, IMHO, you should be altering the code.

This post has been edited by blackcompe: 11 February 2012 - 03:50 AM

Was This Post Helpful? 2
  • +
  • -

#5 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Enum or many subclasses?

Posted 11 February 2012 - 10:42 AM

Thank you for your responses. I am glad to get an insight into your thoughts on the subject. The reason I would suggest subclassing is for the user of my API to write their own code if need be, otherwise they could just change properties of a new Flavor object. (And I think I just answered my own question about using an enum. That's out. So if I want them to be able to add their own code, I kinda am forced into having many subclasses.) If they can subclass it, they can add their own functionality to all the methods. (such as eat(); What happens when you eat this flavor?). I am not sure this can be done (easily) with a properties file, but maybe I'm wrong. I don't have much experience with reflection.

I've been thinking that the hypothetical example might be making this too vague, so let's say I give you some of my classes that I'm drafting. I'll go with the subclass approach:
public abstract class Material {
	// some methods i might implement... again still drafting it
	public abstract int getHardness()
	public abstract boolean isBurnable();
	public abstract void onBurn();
}

public class Wood extends Material {	
	public int getHardness() {
		return 10;
	}

	public boolean isBurnable() {
		return true;
	}

	public void onBurn() {
		// fake methods
		sendSmokePuff();
		provideHeat();
	}
}

But then what do I do? Make Wood a singleton just to get some properties and some user defined code? It's so baffling to me. I suppose with an enum I could have them pass an interface to the constructor to allow for user defined code. I'm at a loss...
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1148
  • View blog
  • Posts: 2,521
  • Joined: 05-May 05

Re: Enum or many subclasses?

Posted 11 February 2012 - 12:18 PM

Quote

But then what do I do? Make Wood a singleton just to get some properties and some user defined code? It's so baffling to me. I suppose with an enum I could have them pass an interface to the constructor to allow for user defined code. I'm at a loss...


So are you saying that you feel it's a waste to have to subclass and override just to return a single property? If so, I see what you mean.

Your requirement for concrete implementations to define some behavior in onBurn is the only thing holding you back from using an external file to create implementations. Are your sure can't restrict these types to strictly having properties? What exactly will provideHeat do? Does it have some real functionality, like calling a method, or does it just define some other property? I'm asking because it could be the case that onBurn behaves different for different implementations, but if sendSmokePuff and provideHeat just end up returning strings, onBurn can be externalized too. E.g.

<materials>
	<material>
		<name>Wood</name>
		<hardness>10</hardness>
		<burnable>true</burnable>
		<onburn>
			<actionlist>
				<action>smoke</action>
				<action>heat</action>
			</actionlist>
		</onburn>
	</material>
	<material>
		<name>Iron</name>
		<hardness>100</hardness>
		<burnable>true</burnable>
		<onburn>
			<actionlist>
				<action>glow</action>
			</actionlist>
		</onburn>
	</material>
</materials>



That XML allows Iron to glow instead of smoke and heat up. Like I said this method of doing this will only work for property-based classes. You could use this method in a materials database system or something.
Was This Post Helpful? 2
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Enum or many subclasses?

Posted 11 February 2012 - 02:05 PM

I could do the thing you suggest, but then I will have to write all the ways to interact with the model of my world. I would need to write a glow() method, and such. I was hoping to keep it open ended, so I can write it once and it can be modified easily by the user. Then the user can write a glow method. Doing that your way.. well I might as well just parse a scripting language and I have like 0 experience with that one.

Mostly the issue arises from me thinking way to far ahead, and while my concerns might be "well-founded" if the project never gets finished they won't matter much anyway. I'm too focused on theory and less focused on production, even though it's my own project with no deadlines. But if I do produce it I don't want to have to rewrite the way a core functionality behaves.

The point I'm trying to make is that provideHeat() can do anything. It can access my model if onBurn() was passed a reference to it, which is probably what I would do to spread fire to neighboring objects. sendSmokePuff() could signal an animation to be played, but keep in mind I am only drafting my classes at this point and I'm not trying to ask for help on these methods, just the architecture of my design.

Thanks for staying tuned, blackcompe. I really appreciate your input.
Was This Post Helpful? 0
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Enum or many subclasses?

Posted 11 February 2012 - 02:37 PM

Quote

I'm asking because it could be the case that onBurn behaves different for different implementations
This too. Wood burns differently Stone obviously. Or if you wanted to add iron to my game, you could write an Iron class that extended Material and write your own onBurn implementation. Instead of hacking a giant list of Enum to add IRON(40, new OnBurnImpl()); at the beginning of the file

Do you see where I'm going? This has been hard to verbalize but I think this is the key point. How would you design your program to deal with this issue? The requirement being that it be easily altered by my users through an API that allowed them to do so (if they know java).
Was This Post Helpful? 1
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5749
  • View blog
  • Posts: 12,556
  • Joined: 16-October 07

Re: Enum or many subclasses?

Posted 11 February 2012 - 03:31 PM

Neat. I'd make Material an interface.
e.g.
public interface Material {
	String getName();
	int getHardness();
	boolean isBurnable();
	void onBurn();
}

public abstract class MaterialImpl implements Material {
	private String name;
	private int hardness;
	private boolean burnable;
	public MaterialImpl(String name, int hardness, boolean burnable) {
		init(name, hardness, burnable);
	}
	public MaterialImpl() { init(); }
	protected void init(String name, int hardness, boolean burnable) {
		this.name = name;
		this.hardness = hardness;
		this.burnable = burnable;
	}
	protected void init() { }
	public String getName() { return name; }
	public int getHardness() { return hardness; }
	public boolean isBurnable() { return burnable; }
	public abstract void onBurn();
}


public class Wood extends MaterialImpl {
	public Wood() { super("Wood", 10, true); }
	public void onBurn() {
		// fake methods
		sendSmokePuff();
		provideHeat();
	}
}




You could set up a singleton where all you materials can live:
public class Materials extends ArrayList<Material> {
	private static Materials instance = new Materials();
	public static Materials getInstance() { return instance; }
	private Materials() { }
}



You can add full classes or, kind fun and potentially useful, anonymous ones.
Materials.getInstance().add(new Wood());

Materials.getInstance().add(new MaterialImpl() {
	protected void init() { init("Paper", 1, true); }
	public void onBurn() { }
});


Was This Post Helpful? 1
  • +
  • -

#10 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Enum or many subclasses?

Posted 11 February 2012 - 04:19 PM

Nice one, baavgai. You wrote more code than I did for my own project already. Haha. (But I still have a lot of the design drafted) That looks like it would be a good solution. The anonymous classes would cut down on the number of class files I would have clogging my project directory. That was my initial qualm with the subclassing route. At first I was against it, but reading over my posts, I seem to have been unknowingly defending it.

So you still think this is a good idea though, considering that in a situation where an anonymous class would be discouraged (can't think of one) one would have to make a new class file for a Material constant?
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5749
  • View blog
  • Posts: 12,556
  • Joined: 16-October 07

Re: Enum or many subclasses?

Posted 11 February 2012 - 04:43 PM

A class is just a blueprint for an object. An anonymous one doesn't really have a disadvantage when your goal is to simply implement functionality for an interface or base class.

I tend to prefer anonymous classes to a ton of little classes that offer only minor variance. Any issue you have for or against them would is probably just a function of style. Your running code is more concerned with the objects themselves than how they got instantiated.
Was This Post Helpful? 1
  • +
  • -

#12 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Enum or many subclasses?

Posted 11 February 2012 - 05:03 PM

Yeah but then, the number of class files in your directory has to be personal preference as well right? It doesn't effect the code in any way, just how fast you can find a class file you want to update and how big your scroll bar is.

Either way, I think this conversation has just sold me onto the anonymous class/subclass route. But I am very thankful for everyone who put in their 2 cents. It is always nice to hear / see another person's outlook on taking on a problem. I learned many things from this :D
Was This Post Helpful? 1
  • +
  • -

#13 bsee123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-April 11

Re: Enum or many subclasses?

Posted 17 February 2012 - 04:02 PM

Maybe I'm oversimplifying this, but if you're talking about modifying source code to add possibilities of a class, maybe you should just consider making it a database application. When you add instances to a database then you don't need to worry about changing your code at all. Just create a table called tblFLAVOR or something appropriate and describe each flavor with proper field/attributes. The logic could be very generic in your program and you could leave almost all of the maintenance up to the user rather than the programmer.

Plus this makes searching and doing subsets of instances a snap as well. If you're worried about creating methods/subroutines to perform on a flavor, try an object oriented database.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1