初学者的松耦合和OO实践 [英] Loose Coupling and OO Practices for Beginners

查看:82
本文介绍了初学者的松耦合和OO实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保持类的松散耦合是编写易于理解,修改和调试代码的重要方面-我明白了.但是,作为一个新手,几乎在任何时候我都无法超越自己挣扎的最简单的例子.

Keeping classes loosely coupled is an important aspect of writing code that is easy to understand, modify, and debug--I get that. As a newbie, though, just about anytime I get beyond the most simple examples I struggle.

我或多或少地了解了如何将字符串,整数和简单数据类型封装在它们自己的类中.但是,当我开始处理诸如RTF格式之类的信息时,事情会变得非常复杂-除非我仅使用组件中已经存在的各种方法.为了继续这个示例,假设我正在编写包含UIF备忘录组件的内容.在Delphi中,该组件具有用于保存格式化文本之类的内置方法.此外,有时似乎唯一(或至少是最好的)使用RTF文本本身的方法是通过再次内置在组件中的方法.

I understand, more or less, how I can encapsulate strings, integers, and simple data types in classes of their own. When I start dealing with information like rich text formatting, however, things get really complicated--unless I just use the various methods already present in a component. To continue this example, suppose I was writing something that included an RTF memo component in the UI. In Delp the component has built-in methods for doing things like saving formatted text. In addition, sometimes it seems like the only (or at least best) ways to work with the RTF text itself is via methods again built into the component.

当我已经有一个为我完成所有这些工作的组件时,我将如何(或为什么)完成另一类中的保存,加载和格式化文本的所有工作?

How (or why) would I do all the work of saving, loading, and formatting text in another class when I already have a component that does all of this for me?

我通常自己要么要么(a)做一些看起来比需要的事情复杂得多的事情,要么重新发明已经存在的方法,要么(b)创建性能差的类,这些类仍然紧密地联系在一起.正如他们在情报学中所说的:必须有更好的方法!"

On my own I usually end up either (a) doing something that seems much more complicated than need be, reinventing methods already present, or (b) creating poorly done classes that are still tightly coupled to one another. As they say in informercials, "There has to be a better way!"

我只是概念上失去了怎么说更好的方式"作品.有什么想法吗?

I'm just lost conceptually on how that 'better way' works. Any thoughts?

推荐答案

我相信您已经错过了一些基本概念.

I believe you've missed on some basic concepts.

OOP 背后的想法始于离散的,可重用的逻辑单元.重点是创建自给自足的模块.

The idea behind OOP starts with discrete, reusable units of logic. With an emphasis on creating self-sufficient modules.

对于RTF备忘录组件,它通过处理给定的数据集(备忘录)来满足上述条件,以使您的程序和程序中的其他对象不必关心它是如何工作的.目的是显示一个界面,接受数据,操纵该特定数据,并将该数据传递到程序的另一部分.

In the case of the RTF Memo component, it meets the above criteria by handling a given set of data (the memo) in such a way that your program and other objects within your program don't care how it does it's job. It's purpose is to show an interface, accept data, manipulate that specific data, and pass that data on to another part of your program.

松散耦合背后的想法仅仅是,您可以用另一个备注控件来代替符合相同接口规格的控件.即,您可以实例化它,让用户与它进行交互,并在必要时提取数据.

The idea behind being loosely coupled is simply that you can replace that memo control with another control which meets the same interface specifications. Namely, that you can instantiate it, let the user interact with it, and pull the data out when necessary.

松散耦合与 关注分离的想法紧密相关. (SoC);这是将程序分解为不同功能以减少重叠功能并使其易于管理的过程.但是他们不是同一回事.顺便说一句,这也是从程序化编程风格转换为OOP的主要推动力之一.随着OOP迫使程序设计人员考虑相关和离散的功能.

Being loosely coupled goes hand in hand with the idea of Separation of Concerns (SoC); which is the process of breaking a program into distinct features in order to reduce overlapping functionality and make easier to manage. But they are not the same thing. Incidentaly, this was also one of the main drivers behind moving away from the procedural style of programming into OOP. As OOP forces the programming to think in terms of related and discrete functionality.

听起来您真的是在询问SoC.

It sounds like you are really asking about SoC.

有许多方法可以实现SoC.有时,它涉及使UI,处理逻辑和持久性层保持分离(例如,考虑MVC设计模式).有时只是为了减少复杂性,只是将相关功能保持在一起. RTF控件已经通过包含处理数据所需的所有功能来做到这一点,从而使您不再有依赖关系.

There are many ways to achieve SoC. Sometimes it involves keeping the UI, processing logic, and persistance layers separated (consider the MVC design pattern for example). Sometimes it is simply keeping related functions together in order to reduce complexity; which the RTF control already does by containing all of the functions necessary to manipulate the data so that you don't have further dependencies.

这篇关于初学者的松耦合和OO实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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