Designing classes in python
I am designing a class which produces information like cpu usage,disk usage,Mem usage,...... for 3 systems,lets say data centers which has many workstations and work stations has many PCs.So the CPU usage and the other parameters are required for all the 3 levels(Datacenters, Workstations, Pcs). Please suggest is the bel开发者_StackOverflowow class design correct or how it is to be designed
EDIT
class Datacenters:
def __init__(self,name,location,cpu,mem):
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
def getparam(self):
return self.name,self.location ,self.cpu,self.mem
def getname(self):
return self.name
class WS(Datacenters):
def __init__(self,name,location,cpu,mem,obj):
#datacentername = Datacenters.__init__(self) #To which data center it is associated
obj.getname() #To which data center it is associated
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
def getparam(self,obj):
return self.name,self.location ,self.cpu,self.mem,obj.getname()
def getpcname(self):
return self.name
class Pcs(WS):
def __init__(self,name,location,cpu,mem,obj):
obj.getpcname() #To which WS it is associated
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
def getparam(self,obj):
return self.name,self.location ,self.cpu,self.mem,obj.getpcname()
a = Datacenters("dc1","Bl1",20,30)
print a.getparam()
b = WS("WS1","Bl1",20,30,a)
print b.getparam(a)
c = Pcs("PC1","Bl1",20,30,b)
print c.getparam(b)
Much of your code is pointless. I removed the lines that isn't needed:
class Datacenter:
def __init__(self,name,location,cpu,mem):
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
class Workstation:
def __init__(self,name,location,cpu,mem,datacenter):
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
self.datacenter=datacenter
class Computer:
def __init__(self,name,location,cpu,mem,workstation):
# This line does nothing:
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
self.workstation=workstation
You don't need to write accessor methods in Python. Just access the attributes directly.
Also, the line obj.getname()
did nothing.
And of course, as pointed out by others here, since when does a Datacenter have a CPU? And doesn't a Computer have the same location as a Workstation? Then it doesn't need the location. Probably your code should look like this:
class Datacenter:
def __init__(self,name,location):
self.name=name
self.location=location
class Workstation:
def __init__(self,name,location,datacenter):
self.name=name
self.location=location
self.datacenter=datacenter
class Computer:
def __init__(self,name,cpu,mem,workstation):
# This line does nothing:
self.name=name
self.cpu=cpu
self.mem=mem
self.workstation=workstation
def location(self):
return self.workstation.location
def datacenter(self):
return self.workstation.datacenter
Some suggestions:
- Use 4-spaces indentation
- Use properties instead of getters, provide single properties, not a single one for all parameters (because parameters might be added later or by subclassing). In order to use properties, derive the base class from
object
(new style classes) - Use singular class names, "Datacenter" instead of "Datacenters", because a single instance doesn't represent multiple datacenters, right?
- Don't call parameters "obj"
- So, a datacenter has a CPU? And a workstation is a datacenter, and gets a datacenter as an argument? Whaaat?
These points were mostly about coding style. I can't really help you with your use case - hope you get the point that your class hierarchy is totally screwed?!
What does this even mean?
class WS(Datacentres):
name
location
cpu
mem
Your design sounds wrong anyway. If a Datacenter has workstations, why would workstation inheit from datacenter? A workstation isn't a datacenter.
http://en.wikipedia.org/wiki/Has-a
You shouldn't use inheritance here in that way, since Pcs
is not WS
and WS
is not Datacentres
.
You can make a class UnitInfo
which has all that fields (name, cpu...) and use the instances of that class in DataCentres
, WS
, Pcs
, because there is "has" relationship here (and not "is").
Or, make a class called Unit
(or Units
, I don't understand why you use plural forms), and inherit from that class. Unit
"has" "name, "cpu-usage" and so on. "Pc" is a Unit
. Maybe Unit
is not the appropriate name for that class though.
精彩评论