V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
llnhhycom
V2EX  ›  Python

面向对象中类应该如何设计?

  •  
  •   llnhhycom · 2017-02-02 21:19:10 +08:00 · 1812 次点击
    这是一个创建于 2651 天前的主题,其中的信息可能已经有所发展或是发生改变。

    楼主最近在做一个项目,技术采用了 Tornado+Redis , 其中在设计用户账号类的时候为了保证代码不会太分散,就把所有与用户账号有关的操作放到 Account class 中,也包括了 login(),register(), logout()...

    这样我在 Account 中使用 python 的 @classmethod 装饰器来装饰与具体实例无关的操作,如 login(),register()等,通过 Account.Login(email, password)来进行一些登陆的操作

    我想了解的是这样设计可以吗,会不会导致 Account 类越来越庞大,以后难以维护呢?(或者还有什么其他的缺点?)

    Account 类在此:( https://github.com/cufrancis/savemylink/blob/master/lib/Account.py )

    demo 在此:( http://savemylink.lllnhhy.com )

    lzjamao
        1
    lzjamao  
       2017-02-02 22:05:38 +08:00
    不好回答。没有具体的要求,何必劳动众。设计本身就是为了解决实际问题。但我觉得代码写多,会有面向对象的设计感觉 。
    bombless
        2
    bombless  
       2017-02-02 22:20:11 +08:00 via Android
    怕膨胀的话完全可以一个类一个方法,习惯就好,哈哈
    814084764
        3
    814084764  
       2017-02-03 09:46:18 +08:00
    难道不应该这么归类么?你的账号的操作能有多少个?
    llnhhycom
        4
    llnhhycom  
    OP
       2017-02-03 10:54:34 +08:00
    @814084764 你可以看下我的 Account 类,所有和账号有关的都在 Account 类中:
    user = Account(uid=1)

    user.nickname // 用户昵称
    user.email // 用户邮箱
    ...
    ...
    user.links() // 用户发布的所有链接
    user.link()[0].title // 用户发布的链接的标题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5694 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:21 · PVG 17:21 · LAX 02:21 · JFK 05:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.