4 Replies - 579 Views - Last Post: 20 June 2012 - 10:01 AM Rate Topic: -----

#1 aliasoink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-October 09

Object Oriented Design Question

Posted 19 June 2012 - 07:34 PM

I want some input on how specific and/or detailed to get when designing classes and objects. For example, I want to write code concerning the dimensions of a physical object. The physical object is the iron core of an electric motor. Should this be coded as a class/object, and all the dimensions be coded as instance attributes? Or should each dimension of the iron core be coded as a class/object?

I included some code below which is coded using each dimension as it's own class/object.

class Iron(object) :
	
	"""general parent class for iron objects"""
	
	def __init__(self,measurement) :
		self.measurement=float(measurement)
		
	def getMeasurement(self) :
		return self.measurement
		
	def setMeasurement(self,measurement) :
		self.measurement=float(measurement)
		

class IronLength(Iron) :
	
	"""create an iron length object and set it's measurement"""
	
	def __init__(self,length) :
		super(IronLength,self).__init__(length)
		
class IronBore(Iron) :

	"""create an iron bore object and set the iron bore measurement"""
	
	def __init__(self,bore) :
		super(IronBore,self).__init__(bore)
	
class IronTooth(Iron) :

	"""create and iron tooth object and set the iron tooth width measurement"""
	
	def __init__(self,tooth) :
		super(IronTooth,self).__init__(tooth)
		
class IronBackiron(Iron) :

	"""create an iron backiron object and set the iron backiron measurement"""
	
	def __init__(self,backiron) :
		super(IronBackiron,self).__init__(backiron)
		

class IronSlotDepth(Iron) :
	
	"""create an iron slot depth object and set the iron slot depth measurement"""
	
	def __init__(self,depth) :
		super(IronSlotDepth,self).__init__(depth)
		
		
class IronVent(Iron) :
	
	"""create an iron vent object and set the vent width, number of vents and total vent width of any iron vents"""
	
	def __init__(self,vent,numvents=0) :
		super(IronVent,self).__init__(vent)
		self.numvents=int(numvents)
		self.ventwidth=self.getMeasurement()
				
	def getNumVents(self) :
		return self.numvents
		
	def getVentWidth(self) :
		return self.ventwidth
		
	def getTotalVents(self) :
		self.totalvents=self.getVentWidth()*self.getNumVents()
		return self.totalvents

ironlength=IronLength(float(input("please enter iron length measurement")))
ironbore=IronBore(float(input("please enter iron bore measurement")))
irontooth=IronTooth(float(input("Please enter the tooth measurement")))
ironback=IronBackiron(float(input("please enter backiron measurement")))
ironslotdepth=IronSlotDepth(float(input("please enter slot depth measurement")))
numvents=int(input("please enter number of vents"))
ironventwidth=IronVent(float(input("please enter vent width measurement")),numvents=numvents)
ironlengthtotal=ironlength.getMeasurement()-ironventwidth.getTotalVents()

print("Iron Dimensions Entered")
print("Iron Length:  "+str(ironlength.getMeasurement()) )
print("Iron Bore:  "+str(ironbore.getMeasurement()) )
print("Iron Tooth:  "+str(irontooth.getMeasurement()) )
print("Iron Backiron:  "+str(ironback.getMeasurement()) )
print("Iron Slot Depth:  "+str(ironslotdepth.getMeasurement()) )
print("Iron Number Of Vents:  "+str(numvents))
print("Iron Vent Width:  " +str(ironventwidth.getVentWidth()) )
print("Iron Length Total:  "+str(ironlengthtotal) )	





Is This A Good Question/Topic? 0
  • +

Replies To: Object Oriented Design Question

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Object Oriented Design Question

Posted 19 June 2012 - 07:50 PM

The better question is, how will you use this class? Will you benefit from having all those dimensions?

Let's say I made a class that represented a dog, would I benefit from having a an integer that says how many hairs are on that dog? What if I had a list with the length and color of each hair? This is getting complex! What if I made a hair class that held the weight, color, and length of each hair, and then tried to make the dog even more detailed? It might be interesting, but it would be far from useful.

More data does not make a class better. You need to know how that data will be used in order to properly model that class.

This post has been edited by atraub: 19 June 2012 - 07:51 PM

Was This Post Helpful? 1
  • +
  • -

#3 aliasoink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-October 09

Re: Object Oriented Design Question

Posted 19 June 2012 - 08:18 PM

The sample code would be part of an electric motor redesign program, and the dimensions are relevant. They will be used in math formulas as part of the electric motor redesign. The electric motor redesign program would allow for changes in horsepower, rated voltage, rpm, current, etc.

I would be designing other classes and objects that pertain to the motor nameplate data and electrical data. Perhaps a better example to illustrate my question would be the motor nameplate. The nameplate would include horsepower, voltage, speed, amp rating, motor frame, model number, serial number, etc. Should all of these be created as separate objects, or as entities/attributes of a nameplate instance object? Does it matter, or is it based on the preferences of the programmer and the needs of the program,
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Object Oriented Design Question

Posted 20 June 2012 - 09:56 AM

Programming exists so that we can solve problems, thus, the real question is, what design will most efficiently allow you to solve your problem? Nothing says you have to use object orientation, it's simply a style that many programmers favor. Program to solve your problem :)
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7896
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Object Oriented Design Question

Posted 20 June 2012 - 10:01 AM

It's a truism from DS&A courses that your representation of your data largely defines your program. If you represent your data in a way that is congruent with your intentions, you can manipulate it easily. If you represent your data badly, you will find yourself fighting against your data.
When you design an object - and this is true in any OO language, I think - you are designing a data representation, and you should make it line up with your problem as well as possible.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1