Inheritance Design
Right now, I have the following design:
Item |------------------------| ImageUploader | |--------------| | | | | AvatarUploader FaviconUploader NameChanger
These classes are various items that users can purchase and use.
However, I'm adding a new class IconUploader
. Unlike the other classes, this is not an item that can be used, but an administrative panel. ImageUploader
contains certain security checks to make sure only safe files are uploaded, and IconUploader
needs these precautions as well.
However, I'm not sure what to do. Ideally, I'd make ImageUploader
an interface, but you can't have actual code in interfaces, so I can't do that. I could move ImageUploader
out of the Item
class hierarchy and make i开发者_如何学JAVAts functions static, but that doesn't feel right to me. Is it? And if it isn't, what is the best way to restructure this?
N.B.: I'm using PHP, if that affects anything.
You should probably have a GenericUploader
, and then each type of uploader can specify a list of valid files, then the GenericUploader
will ensure those requirements are met (along with anything else you might wish it do to).
You need a ImageUploaderBase
class which is abstract and have the common functionality.
You could move the security checking and common upload functionality for the uploaders into a separate Uploader class, and make ImageUploader use this Uploader class (so all derived classes like Avatar/FaviconUploader would use it as well).
Then you could make IconUploader use Uploader (without it being derived from Item).
Then you can make ImageUploader and IconUploader implement an IUploader interface, and have all calls to the interface forwarded to the Uploader class to implement.
精彩评论