Structuring classes with similar properties

Nathan Petrelli npetrelli at klassmaster.com
Sat Mar 3 11:02:07 PST 2007


I didn't knew where to post this questions, please excuse me if this isn't the right group.

I'm having problems organizing the code of my application because I have several similar classes but some of them lacks some properties and others introduce new properties. Right now they all have lots and lots of duplicated code and it's started to hurt development:

class Invoice {
	int id;
	char[] code;
	char[] clientName;
	real total;
	real payment;
	InvoiceDetails[] details;
}

class Call {
	int id;
	char[] clientName;
	char[] phone;
	real total;
	CallDetails[] details;
}

class Quote {
	int id;
	char[] clientName;
	real total;
	real payment;
	QuoteDetails[] details;
}

// There are other four like this.

Each class also has similar operations, but they all have to cope with a class having some set of properties and different set of properties in another:

class Invoice {
	string save() {
		// ...
		row.code = generateCode();
		row.total = calculateTotal(details);
		row.save(this);
	}
	string print() {
		printer.appendCenter("%s", toString(getUTCTime()));
		printer.append("Code: %s", code);
		printer.append("Client: %s", clientName);
		foreach (InvoiceDetails det; details)
			printer.append("%s %s %s %s", det.product, det.ammount, det.price);
		printer.append("Total: %s", total);
		printer.print();
	}
	// ...
}

class Call {
	string save() {
		// ...
		row.phone = phone;
		row.total = calculateTotal(details);
		row.save(this);
	}
	string print() {
		printer.appendCenter("%s", toString(getUTCTime()));
		printer.append("Client: %s", clientName);
		printer.append("Phone: %s", phone);
		foreach (CallDetails det; details)
			printer.append("%s %s %s %s", det.product, det.ammount, det.price);
		printer.append("Total: %s", total);
		printer.print();
	}
	// ...
}

As you can see, I have a problem. A big problem. ;)

The the typical solution of using inheritance and calling the super class' methods to add something before or after doesn't work here because the changes on the operations happen in the middle of methods.

So my question is: How would you structure code like this?

Thanks



More information about the Digitalmars-d mailing list