开发者

Static or constant or what are they?

I just want to know how I can call certian classes in design pattern here, like which type are they classified in OO design

(1) I use a class that has just named constants , this class is used directly other classes to get values of constants in it.I dont instantiate the class.

(2) I use a class with full of static methods, this class is basically used by other classes as a holder of methods that are used by them. So again i dont instantiate the class开发者_开发百科.

What are these kinda classes classified under OOdesign? Can I do it in a more elegant way?


What are these kinda classes classified under OOdesign? Can I do it in a more elegant way?

You do have better alternatives, IMHO.

I use a class that has just named constants , this class is used directly other classes to get values of constants in it.I dont instantiate the class.

For e.g. in this case you don't necessarily need a class. You can have a settings module that defines the various "constants". I put constants in quotes because there are no constants in Python the way there are in say, Java.

(2) I use a class with full of static methods, this class is basically used by other classes as a holder of methods that are used by them. So again i dont instantiate the class.

Again, no need for a class. You can have one or more modules that contain these methods or rather, functions. They can be logically grouped as you see fit.

I'd like to add a note that you don't have to stick to (Java style?) "classes only" approach (for lack of a better phrase). Rather try to write code that doesn't go against the grain of the language. In Python's case I'd argue that such classes as you described above are best avoided. They seem to me like a carry over from Java.


  1. Many (but not all) languages that focus strongly on OOP (C++, C#, Java) have an enum type for constants rather than putting them in a class. However, in other languages, such as Smalltalk and Python, there is no special construct for constants and it can make sense to put them in a class. To the best of my knowledge, there's no special name for that kind of class.

  2. In other languages, a static class is a class that cannot be instantiated and which defines only constants and static methods. Even though Python has no language-level support for enforcing those rules, I would still refer to a class designed that way as a static class.

In Python 2.6 or greater, you can use a class decorator to enforce the rules:

def staticclass(cls):
    """Decorator to ensure that there are no unbound methods and no instances are
       created"""
    for name in cls.__dict__.keys():
        ob = getattr(cls, name)
        if isinstance(ob, types.MethodType) and ob.im_self is None:
            raise RuntimeError, "unbound method in class declared no_instances"
    def illegal(self):
        raise RuntimeError,"Attempt to instantiate class declared no_instances"
    cls.__init__ = illegal
    return cls

@staticclass
class MyStaticClass(object):
    pass

As Manoj points out, many times you can do away with the class and put the constants or functions at the module level. However, there are cases where it really is useful to have a class. For example, if the class has significant state, putting the functions at the module level requires littering the code with global statements. Although rare, it is also sometimes useful to have a class hierarchy of static classes.

Another alternative to a static class is the singleton pattern, where you ensure that only one instance of the class is every created (and typically provide a static method that returns that instance).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜