OO设计 [英] OO design

查看:66
本文介绍了OO设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用python编写了一段时间的脚本。基本上写一些

函数并在ipython shell中运行。这非常有用。但是我更多地这样做,我越是看到我或多或少地做了同样的事情

一遍又一遍。所以我觉得我需要进入课程编程

及其所有附带的好处。然而,我最大的问题是概念性的

。我无法理解定义合适的课程,他们如何获得数据并相互沟通。我希望你们中的一些人能够分享你们关于这个主题的一些智慧。


我基本上做的是下面的很多内容::


1.获取任意数值数据(通常是大型数据集,以列式

格式,甚至是来自其他软件包的COM。我一般要处理

一组或多组X,Y数据)

2.操纵数据(缩放,最小二乘拟合,均值,峰值,
从另一个中添加/减去一个XY集等)

3.绘图数据(原始集,操作结果,散点图,直方图

等 - 我使用matplotlib)

4.导出数据(print,csv,shelve)


我可以编写一些功能代码来完成上述任何操作。

但对于我的生活,我无法看到如何在一个基于OO

的框架中完全挂钩它我可以构建和扩展(使用更多的数据格式,

操纵,GUI等等。


当我想到我应该做什么时,我最终得到一个XY类,它有一个

方法,用于我想要做的一切,例如。


类XY:

def read_file

def scale_data

def plot_data

def shelve_data


但不知何故感觉不对,特别是当我预计

方法的数量会增长和增长时让课堂变得非常笨拙。


即使这是一个合法的选择,我也不明白如何我会如何b / b
同一图表上的不同XY对象或逐点添加

。两个不同的XY对象如何交流

以及如何处理它们必须具有的共同点(例如,情节

屏幕)。


显然我在这里有一些概念化问题。希望有人能够对这个问题有所了解


bwaha。

解决方案
>>>>> "克里斯" == chris< cf ***** @ hotmail.com>写道:


chris>编写一些功能代码来做任何事都没有问题

chris>上面提到的。但对于我的生活,我看不出我怎么能

chris>在一个基于OO的框架中将它们完全挂钩,我可以使用

chris>构建和扩展(使用更多数据格式,操作,

chris> GUI等)。


Chris,

我回应你的情绪。

我的小宠物项目最近发展到包括一个对象

层次结构。我一直觉得Java-esque byzantine血统

对于最后一堂课来说都是一件小事。

我用来触发因子分解的是我是否根据数据类型需要

分支:


如果input_data.type ==" this":

do_this()

否则:

do_that()


意味着我应该将我的代码考虑在内:


class input_data_base()

pass


class input_data_this(input_data_base)

pass


class input_data_that(input_data_base)

pass

有了这个,我的项目有一定数量的合理继承

层次结构(两个)并且代码不会从这样的保留中受益

它的程序性。

OO是一个伟大的组织者,但每个范例都与鲟鱼发生冲突

法律过度驱动。

HTH,

Chris


fav DP书籍:

http://www.oreilly.com/catalog / hfdesignpat /

http:// www。 netobjectives.com/dpexplained/


2005-07-19,chris< cf ***** @ hotmail.com>写道:

我已经用python编写了一段时间的脚本。基本上写了一些
函数并在ipython shell中运行。这非常有用。但是我越做这个就越多,我看到我一遍又一遍地做同样的事情。




当发生这种情况时,你可能需要为该功能创建一个

模块。


至于Python中的OO, IMO最好只是潜入而不用担心

方法上正确首先。不幸的是,大多数关于OO的书都使用了

静态语言作为他们的例子,这通常模糊了在Python中非常简单的概念。使用Smalltalk的书籍也不算太差,例如,
,例如_Smalltalk,Objects和Design_ by Chamond Liu。


有关Python OO基础的书籍章节:< > http://diveintopython.org/object_oriented_framework/index.html\" target=\"_blank\"> http://diveintopython.org/object_ori...ork /index.html
http: //www.ibiblio.org/g2swap/byteof...read/oops.html
http://www.pasteur.fr/formation/info...thon/ch18.html


关于设计模式的fraca7系列,其中故事的寓意经常是b $ b你不需要在Python中这样做。:

http://fraca7.free.fr/blog/index.php?Python


Dave Coo k


I''ve been scripting with python for a while now. Basically writing a few
functions and running in the ipython shell. That''s been very useful. But the
more I do this the more I see that I''m doing more or less the same thing
over and over again. So its feels like I need to get into class programming
with all its attendant benefits. However my biggest problem is a conceptual
one. I just can''t get my head around defining suitable classes, how they
aquire data and communicate with each other. I''m hoping some of you python
lamas out there might be able to share some of your wisdom on the subject.

What I basically do is a lot of the following::

1. get arbitrary numerical data (typically large data sets in columnar
format or even via COM from other packages. I generally have to deal with
one or more sets of X,Y data)
2. manipulate the data (scaling, least squares fitting, means, peaks,
add/subtract one XY set from another etc)
3. plot data (original set, results of manipulation, scatterplot, histograms
etc - I use matplotlib)
4. export data (print, csv, shelve)

I have no problem writing bits of functional code to do any of the above.
But for the life of me I can''t see how I can hook them altogether in an OO
based framework that I can build and extend (with more data formats,
manipulations, GUI etc).

When I think about what I should do I end up with a class XY that has a
method for everything I want to do eg.

class XY:
def read_file
def scale_data
def plot_data
def shelve_data

But somehow that doesn''t feel right, especially when I expect the number of
methods will grow and grow, which would make the class very unwieldy.

Even if that was a legitimate option, I don''t understand conceptualy how I
would, for example, plot two different XY objects on the same graph or add
them together point by point. How do two different XY objects communicate
and how do you deal with the thing that they must have in common (the plot
screen for example).

Clearly I''m having some conceptualisation problems here. Hope someone can
shed some light on the subject

bwaha.

解决方案

>>>>> "chris" == chris <cf*****@hotmail.com> writes:

chris> I have no problem writing bits of functional code to do any
chris> of the above. But for the life of me I can''t see how I can
chris> hook them altogether in an OO based framework that I can
chris> build and extend (with more data formats, manipulations,
chris> GUI etc).

Chris,
I echo your sentiment.
My little pet project has recently grown to include a bit of an object
hierarchy. I''ve always felt that the Java-esque byzantine pedigree
for everything last class a trifle over-done.
What I used to trigger factorization was whether or not I needed to
branch based on data type:

if input_data.type == "this":
do_this()
else:
do_that()

meant I should factor my code to:

class input_data_base()
pass

class input_data_this(input_data_base)
pass

class input_data_that(input_data_base)
pass
With this, my project has a modest number of sensible inheritance
hierarchies (two) and the code that wouldn''t benefit from such retains
its procedural character.
OO is a great organizer, but every paradigm runs afoul of Sturgeons
Law if over-driven.
HTH,
Chris


fav DP books:

http://www.oreilly.com/catalog/hfdesignpat/

http://www.netobjectives.com/dpexplained/


On 2005-07-19, chris <cf*****@hotmail.com> wrote:

I''ve been scripting with python for a while now. Basically writing a few
functions and running in the ipython shell. That''s been very useful. But the
more I do this the more I see that I''m doing more or less the same thing
over and over again.



When that happens, it''s probably a good sign that you need to create a
module for that functionality.

As for OO in Python, IMO it''s best just to dive in and not worry about being
"methodologically correct" at first. Unfortunately, most books on OO use
static languages for their examples, which usually obscures concepts that
are extremely simple in Python. Books using Smalltalk are not too bad,
though, for example _Smalltalk, Objects, and Design_ by Chamond Liu.

Book chapters on Python OO basics:

http://diveintopython.org/object_ori...ork/index.html
http://www.ibiblio.org/g2swap/byteof...read/oops.html
http://www.pasteur.fr/formation/info...thon/ch18.html

fraca7 series on design patterns, where the moral of the story is often
"You don''t need to do that in Python.":

http://fraca7.free.fr/blog/index.php?Python

Dave Cook



这篇关于OO设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆