简体   繁体   English

设计模式的名称:将数据从一种语言转移到另一种语言

[英]Name of a design pattern: Moving data from one language to another

I'm trying to determine the name of this pattern if it exists. 我正在尝试确定此模式的名称(如果存在)。 Essentially taking data from one language and using it in another. 本质上是从一种语言中获取数据并在另一种语言中使用它。 ie PHP -> Javascript. 即PHP-> Javascript。 I was thinking it may be delegation, but I've seen this done from time to time, and wanted to know if it was considered a pattern, or is is just a protocol. 我当时以为可能是委派,但是我不时看到这一点,并且想知道它是否被视为一种模式,或者仅仅是一个协议。

Edit: This more clearly defines what I'm talking about. 编辑:这更清楚地定义了我在说什么。 You're not just generating one code or the other, but using it to send values. 您不仅在生成一个或另一个代码,还使用它来发送值。 And so I guess this could be defined as source preprocessing. 因此,我想这可以定义为源预处理。

<?php $foo = $_POST[MY_VAR]; ?>
<script>
    $bar = <?php echo $foo; ?>
    alert($bar)
</script>

If what you wanted to know is about communication between processes , then see sockets, TCP, HTTP and related technologies and data exchange formats like XML, JSON and similar. 如果您想了解有关进程之间的通信的信息 ,请参阅套接字,TCP,HTTP和相关技术以及诸如XML,JSON之类的数据交换格式。

If what you want to know is how to use code written in one language in a program written in another language , like what .Net or various JVM languages do, then it is interoperability you are talking about. 如果您想知道的是如何在以另一种语言编写的程序中使用以一种语言编写的代码 ,例如.Net或各种JVM语言,那么您正在谈论的是互操作性。 Just keep in mind that in both cases I mentioned, programs in both languages are compiled to a common language (MSIL and Java bytecode, respectively). 请记住,在我提到的两种情况下,两种语言的程序都被编译为通用语言(分别为MSIL和Java字节码)。

In any case, I don't know if this has a name, or even if it is considered a pattern. 无论如何,我不知道它是否具有名称,甚至不被认为是一种模式。

EDIT: 编辑:

Since you specifically mentioned PHP and JavaScript, it is possible that you meant using PHP to generate a JavaScript variable with a literal value, so that you could pass a value from PHP to JavaScript code. 由于您专门提到了PHP和JavaScript,因此可能意味着要使用PHP生成具有文字值的JavaScript变量,以便可以将PHP中的值传递给JavaScript代码。 This could be called source preprocessing or maybe code generation . 可以称为源预处理代码生成

In terms of the example you link to, that's not a design pattern or protocol and there's no interoperability between PHP and JavaScript whatsoever - the JavaScript interpreter isn't even running as far as PHP is concerned and vice-versa. 就您链接到的示例而言,这不是一种设计模式或协议,并且PHP和JavaScript之间没有任何互操作性-就PHP而言,JavaScript解释器甚至没有运行,反之亦然。

You're simply using PHP as a JavaScript code generator - nothing more, nothing less. 您只是将PHP用作JavaScript 代码生成器 -仅此而已。

If you were using JavaScript to extract information in real-time (via "AJAX", etc.) then there would at least be some interaction between the two languages and you could (sort of) consider a data format like JSON (or indeed XML) to be a form of XDR . 如果您使用JavaScript实时(通过“ AJAX”等)提取信息,那么两种语言之间至少会有某种交互,您可以(考虑)考虑一种像JSON(或XML)这样的数据格式)作为XDR的一种形式。 However, that's not the case in this instance. 但是,在这种情况下并非如此。

I don't think it's a design pattern at all - well maybe in broader sense than the term is usually used. 我根本不认为这是一种设计模式-也许在广义上比通常使用的术语更广泛。 You basically have two applications talking to each other over HTTP protocol. 基本上,您有两个应用程序通过HTTP协议相互通信。 Languages used are irrelevant. 使用的语言无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM