你能解释流的概念? [英] Can you explain the concept of streams?

查看:170
本文介绍了你能解释流的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,甲流是字节序列的重新presentation。每个流提供了用于读取和写入的字节到给定的后备存储。但是,什么是流点?为什么不支持存储本身就是我们互动?

I understand that a stream is a representation of a sequence of bytes. Each stream provides means for reading and writing bytes to its given backing store. But what is the point of the stream? Why isn't the backing store itself what we interact with?

无论出于何种原因,这个概念只是不点击我。我读了一堆的文章,但我想我需要一个比喻什么的。

For whatever reason this concept just isn't clicking for me. I've read a bunch of articles, but I think I need an analogy or something.

推荐答案

这个流已经被选中,是因为它重新presents(在现实生活中)非常相似的含义,我们想要的东西,当我们用它来传达吧。

The word "stream" has been chosen because it represents (in real life) a very similar meaning to what we want to convey when we use it.

让我们忘掉的后备存储的少了,开始思考比喻为水流。您会收到一个连续的数据流,就像水在河里不断流动。你不一定知道的数据是从哪里来的,大多数时候你并不需要;无论是从一个文件,插座,或任何其他来源,它并没有(也不应该)真正的问题。这是非常相似于接收的水,由此不需要知道它是来自一个流;无论是从一个湖,喷泉,或任何其他来源,它没有(也不应该)真正的问题。

Let's forget about the backing store for a little, and start thinking about the analogy to a water stream. You receive a continuous flow of data, just like water continuously flows in a river. You don't necessarily know where the data is coming from, and most often you don't need to; be it from a file, a socket, or any other source, it doesn't (shouldn't) really matter. This is very similar to receiving a stream of water, whereby you don't need to know where it is coming from; be it from a lake, a fountain, or any other source, it doesn't (shouldn't) really matter.

这是说,一旦你开始认为你只在乎得到你需要的数据,无论来自何方,抽象等人津津乐道更加清晰。你开始想,你可以用流,你的方法仍然会很好地工作。例如,你可以这样做:

That said, once you start thinking that you only care about getting the data you need, regardless of where it comes from, the abstractions other people talked about become clearer. You start thinking that you can wrap streams, and your methods will still work perfectly. For example, you could do this:

int ReadInt(StreamReader reader) { return Int32.Parse(reader.ReadLine()); }

// in another method:
Stream fileStream = new FileStream("My Data.dat");
Stream zipStream = new ZipDecompressorStream(fileStream);
Stream decryptedStream = new DecryptionStream(zipStream);
StreamReader reader = new StreamReader(decryptedStream);

int x = ReadInt(reader);

正如你看到的,它变得非常容易改变你的输入源,而不改变你的处理逻辑。

As you see, it becomes very easy to change your input source without changing your processing logic. For example, to read your data from a network socket instead of a file:

Stream stream = new NetworkStream(mySocket);
StreamReader reader = new StreamReader(stream);
int x = ReadInt(reader);

容易,因为它可以。而美下去,因为你可以使用任何类型的输入源,只要你可以为它建立一个流包装。你甚至可以做到这一点:

As easy as it can be. And the beauty continues, as you can use any kind of input source, as long as you can build a stream "wrapper" for it. You could even do this:

public class RandomNumbersStreamReader : StreamReader {
    private Random random = new Random();

    public String ReadLine() { return random.Next().ToString(); }
}

// and to call it:
int x = ReadInt(new RandomNumbersStreamReader());

请参阅?只要你的方法不小心输入源是什么,你可以自定义各种方式源。抽象,您可以从一个非常优雅的方式处理逻辑中分离出的输入。

See? As long as your method doesn't care what the input source is, you can customize your source in various ways. The abstraction allows you to decouple input from processing logic in a very elegant way.

请注意,我们创建了自己的流没有后备存储,但它仍然是完全符合我们的目的。

Note that the stream we created ourselves does not have a backing store, but it still serves our purposes perfectly.

因此​​,要总结,甲流只是输入源,躲着走(抽象)的另一个来源。只要你不破的抽象,您的code将是非常灵活的。

So, to summarize, a stream is just a source of input, hiding away (abstracting) another source. As long as you don't break the abstraction, your code will be very flexible.

这篇关于你能解释流的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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