有没有关于这种编程的文献? [英] Is there some literature on this type of programming?

查看:143
本文介绍了有没有关于这种编程的文献?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大学里,我参加了一门关于现代物理的课程,我们学习了狭义相对论。我完全被不同的参考帧实际上观察到物体的物理属性不同,并且都不正确。随着时间的推移,这个概念慢慢地改变了我的编程方式,现在我倾向于将课程分成两大类,数据对象和观察(仅限函数)对象。



为了避免这个问题变得复杂而冗长,我将通过两个例子来解释我的意思。



首先,举例说明这种类型的代码,我经常这样写:

  class Deck 
{
私人卡[]卡;

public void Shuffle()
{
// shuffle algorithm
}

//其他卡组相关的函数,比如draw,cut, etc ...
}

我通常现在编写相同的脚本:

  class Deck 
{
//根据意图,我只是指某种不可变的卡片集合
private ReadonlyCollection< Card> _牌;

公共卡[]卡片{get {return this._Cards; }}
}

接口IDeckHandler
{
Deck Shuffle(甲板甲板);
//其他与甲板有关的函数,比如draw,cut等。
}

class经销商:IDeckHandler
{
// IDeckHandler实现

$ / code>

Deck 不再是负责实施可以采取行动的职能。或者为了使它符合术语,甲板只是一套价值观,观察者的责任是观察它的方式。很自然地,可以有很多观察者以不同的方式执行这些动作。



对于第二个例子,我将使用一些我曾试图解释这个的人,更容易的时间。在彩色纸上填上彩色字母并拼出单词的情况。我们有一个代理人,他的责任是阅读论文中的单词。现在假设代理是某种类型的色盲。从纸张发出的图像是相同的,但感觉可能不同。观察者对对象没有深入的了解,也不能对其进行修改,只是对其进行解释。



正如我所说,这个概念驱动了我的许多发展决定。所以回到这个问题,是否是已发布的编程类型,如果可以的话,您能否指出我的一些相关文献?我遇到了一些常见和不常见的情况,其中很难做出决定,当然有些事情我还没有想到或遇到过,希望在文献中可以看到。

解决方案

您正在做的是将概念的数据与作用于该概念的操作分开。 与系统的作用。这为许多不同的场景打开了大门,您可以在其中使用不同的行为类来更改系统的行为。这些行为类也可以针对不同的数据类重用。许多模式解决了这个问题,例如访问者,命令,策略或观察者。



但是这里有更深层的东西。也许我们需要(主流)编程语言中的另一个概念(或者也许只是在我们的脑海中),这将允许我们分离和重用这些行为。



DCI体系结构解决了这些问题,并呈现角色或特质(pdf),作为行为和代码重用的基本单位。

In college I took on a class on modern physics, in which we learned about special relativity. I was completely blown away by how different frames of reference could actually observe physical properties of an object to be different and neither be incorrect. Over time, this concept has slowly been changing the way I program, to the point where now I tend to break up classes into 2 main categories, data objects and observing (function only) objects.

For the sake of this not becoming a elaborate and lengthy post for such a simple question, I'll try to explain what I mean through two examples.

First, take for instance this type of code, which I used to often write:

class Deck
{
    private Card[] cards;

    public void Shuffle()
    {
        // shuffle algorithm
    }

    // other deck related functions like draw, cut, etc...
}

I typically now write the same scenario as:

class Deck
{
    // by intention, i just mean some kind of immutable collection of cards
    private ReadonlyCollection<Card> _Cards;

    public Card[] Cards { get { return this._Cards; } }
}

interface IDeckHandler
{
    Deck Shuffle(Deck deck);
    // other deck related functions like draw, cut, etc..
}

class Dealer : IDeckHandler
{
    // IDeckHandler implementation
}

The Deck is no longer responsible for implementing the functions which can act on it. Or to make it fit the terminology, the deck is just a set of values and the way in which it is observed is the responsibility of the observer. Naturally, there can be many observers who perform the actions differently.

For the second example, I'll use something which people I have tried to explain this to have had an easier time with. Take the situation where we have colored letters on colored paper which spell out a word. We have an agent whose responsibility it is to read the word on the paper. Now assume the agent is some type of color blind. The image being emitted from the paper is the same, but the perception could be different. The observer does not have intimate knowledge of the object and cannot modify it, only respond with an interpretation of it.

As I have stated, this concept drives many of my development decisions. So back to the question, is this a published type of programming and if so, can you point me to some literature on it? There are some common and uncommon scenarios I have run into which are difficult to make decisions for, and certainly some things I just haven't thought of or run into yet which would hopefully have been examined in literature.

解决方案

What you're doing is separating the data of the concept from the operations that act on that concept. What the system is from what the system does. This opens the door for many different scenarios, in which you change the behavior of the system by using different behavior classes. These behavior classes can also be reusable for different data classes. Many patterns address this problem, like Visitor, Command, Strategy, or Observer.

But there's something deeper at play here. Maybe we need another concept in our (mainstream) programming languages (or maybe just in our minds), that will allow us to separate, and reuse, these bahaviors.

The DCI architecture addresses these issues, and presents roles, or traits (pdf), as the fundamental unit of behavior and code reuse.

这篇关于有没有关于这种编程的文献?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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