[Tutor] Design - Granularity of classes
Alexandre Ratti
alex@gabuzomeu.net
Thu, 16 May 2002 11:21:12 +0200
Hello,
How granular do you make your classes? I.e. do you usually wrap fairly
simple data in classes? Here is an example.
Let's assume we upload files into a Web app. Let's call UploadFile the
class that is used to handle these files. UploadFile will hold properties
such as size and type.
class UploadFile:
"""Uploaded file."""
def __init__(self, name, data, size, type):
self.name = name
self.data = data
self.size = size
self.type = type
# etc
For instance, would you create a FileType class to hold the mime type and a
FileSize class to hold the type?
class FileType:
"""File type"""
def __init__(self, type):
# Can test mime type here
self.type = type
def __repr__(self):
return str(self.type)
class FileSize:
"""File size"""
def __init__(self, size):
# Can test data type here
self.size = size
def __repr__(self):
return str(self.size)
Now UploadFile could be rewritten as:
class UploadFile2:
"""Uploaded file."""
def __init__(self, name, data, size, type):
self.name = name
self.data = data
self.size = FileSize(size)
self.type = FileType(type)
# etc
=> Does it make sense? I think this strategy would add overhead but it
might help testing for correct data type.
Thanks.
Alexandre