软件设计问题 [英] software design question

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

问题描述




我有以下课间关系:


__main__ :(在文件LMCMain.py中)

导入模块FileIO

定义类LMCMain

instanciats main = LMCMain(...)


FileIO。 py:

定义类FileIO

类FileIO需要访问main来自LMCMain.py


在FileIO.py中我试图导入LMCMain,但这会导致主要的
程序重新启动执行。我只想访问主类

实例LMCMain。

我的目标是模块化软件设计。我知道这会产生很多沟通费用,而且很快就会变成汇编。在这里,我依靠它不太慢

并且最终可能是psyco。


我能想到的解决方案是Singleton。 LMCMain.py和

FileIO.py import" singleton.py" ;. LMCMain.py存储主要单例和

FileIO.py访问它。

我也可以使用静态类而不是公共模块变量...我

我真的不知道什么更好。


我想回到一些设计模式,但不知怎的,你只能使用它们的b $ b,一旦你习惯了它们(即理论知识不足以满足要求)。

我也从伐木中学到了很多东西。模块和Qt SIGNAL-SLOT概念

IMO的设计非常出色。


我知道这与Python没有严格关系,但是因为我是工作在

Python我想听听pythoneers的建议。


提前致谢

Uwe

Hi,

I have the following inter-class relationships:

__main__: (in file LMCMain.py)
imports module FileIO
defines class LMCMain
instanciats main = LMCMain(...)

FileIO.py:
defines class FileIO
class FileIO needs to access "main" from LMCMain.py

In FileIO.py I''ve tried to "import LMCMain", but that causes the main
program to be re-executed. I only wanted to access to the main class
instance LMCMain.
My aim is a modular software design. I know this yields much communication
overhead and may become compilcate soon. Here I rely on "its not too slow"
and eventually perhaps psyco.

A solution I could think of would be a Singleton. Both LMCMain.py and
FileIO.py import "singleton.py". LMCMain.py stores "main" in singleton and
FileIO.py accesses it.
I could also use a static class instead of a public module variable... I
don''t really know whats better.

I''d like to fall back on some design pattern, but somehow you can only use
them, once you''re used to them (i.e. theoretical knowledge does not
suffice).
I also learned much from the "logging" module and the Qt SIGNAL-SLOT concept
which IMO are excellently designed.

I know this is not strictly related to Python, but since I''m working in
Python I''d like to hear pythoneers suggestion to it.

Thanks in advance
Uwe

推荐答案

>我有以下类间关系:
> I have the following inter-class relationships:

__ main __ :(在文件LMCMain.py中)
导入模块FileIO
定义类LMCMain
instanciats main = LMCMain(...)

FileIO.py:
定义类FileIO
类FileIO需要访问main来自LMCMain.py

__main__: (in file LMCMain.py)
imports module FileIO
defines class LMCMain
instanciats main = LMCMain(...)

FileIO.py:
defines class FileIO
class FileIO needs to access "main" from LMCMain.py




我不知道应该/应该/叫什么,但你拥有的是我的b $ b b调用循环导入。你试图有两个模块

使用彼此的内部。这是糟糕的设计。


你可以使用以下方法来克服糟糕的设计:


main = LMCMain()

导入系统

sys.modules [''FileIO'']。main = main

将来会提出更好的设计。 />

- Josiah



I don''t know what one /should/ call it, but what you have is what I
would call a circular import. You are attempting to have two modules
use the internals of each other. That is poor design.

You can use a kludge to get past poor design by using the following:

main = LMCMain()
import sys
sys.modules[''FileIO''].main = main
In the future, better design is suggested.

- Josiah


Josiah Carlson写道:
Josiah Carlson wrote:
我不知道是什么一个/应该/叫它,但你有什么我会称之为循环导入。您试图使用两个模块
使用彼此的内部。那是糟糕的设计。


[... kludge ...]将来会提出更好的设计。
I don''t know what one /should/ call it, but what you have is what I
would call a circular import. You are attempting to have two modules
use the internals of each other. That is poor design.
[...kludge...] In the future, better design is suggested.




好​​的,以下是这样的:

我有一个GUI。这是基类(自动生成),我从中派生出一个实现该类的类。但是,由于这可能会变得相当大,我将功能划分为不同的模块,即一个用于演示文稿,一个用于文件IO,一个用于帮助主题相关的东西等等。

所以这个主文件导入实现该功能的模块。

显然这些模块需要访问诸如启用动作之类的东西,安装
来自主要部分的钩子等。

这就是循环依赖的原因。

我知道循环依赖是相当糟糕而不是有用,但唯一的方法来规避这一点就是将所有内容构建成一个大块 - 我发现b $ b发现更糟糕的是:源文件也增长得太多了很大的维护

很好的概述。


所以问题是:如何做出更好的设计?


Ciao

Uwe



Ok, the following is the case:
I got a GUI. This is the base class (automatically generated) from which i
derive a class that does the implementation. However, since that may become
rather big I devided the functionality into different modules, i.e. one for
presentation, one for file-IO, one for help-topic related stuff, etc.
So this main file imports the modules that implement the functionality.
Obviously these modules need to access things like "enable action"s, install
hooks, etc. from the "main part".
And thats why the circular dependency.

I know that circular dependencies are rather bad than helpful, but the only
way to circumvent this is to build everything into one big block - which I
find is rather worse: the source file grows much too large to maintain a
good overview.

So the question is rather: how to make a better design?

Ciao
Uwe


>所以问题是:如何制作更好的设计?


导入模块1

....


class main:

def __init __(self,args ...):

self.c1 = module1.class1(args ...)

#where args ...是您班级的标准初始化,

#以及c1需要的任何其他对象/方法

#访问。


传递所需要的东西。如果初始化外部

模块类时无法通过所需的内容,则稍后再设置该属性。


c1instance.attribute = value


- Josiah
> So the question is rather: how to make a better design?

import module1
....

class main:
def __init__(self, args...):
self.c1 = module1.class1(args...)
#where args... is the standard initialization for your class,
# and any additional objects/methods that c1 needs
# access to.

Pass what is needed. If you can''t pass what is needed when external
module classes are initialized, then set the attribute later.

c1instance.attribute = value

- Josiah


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

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