【翻译】【Google Gears】【选择一个离线应用程序架构】Choosing an Offline Application Architecture...

声明: 本人英语特别烂,今天比较无聊就翻译这个文章,意思应该勉强到了,一些细节可能没有翻译到位,有的地方翻译地自己都读了别扭,请大家不要耻笑,谢谢合作!
PS: 由于本人英语水平比较烂,为了尊重大家的阅读习惯,我采用了对照翻译的方式来翻译,有什么翻译不好的地方,请大家指出,但请不要嘲笑我,因为我为我的英语水平已经够自卑了。再次感谢。


【作者】:Google
【原文】: http://code.google.com/apis/gears/architecture.html





Google Gears API Developer's Guide (Beta)

【翻译】:谷歌Gears应用程序接口开发者指南(Beta)

Choosing an Offline Application Architecture

【翻译】: 择一个离线应用程序结构
 

During development of Gears, we experimented with many different architectures for offline-enabled web applications. In this document we briefly look at some of them and explore their advantages and disadvantages.
 
【翻译】: 随着Gears的发展,我们为支持离线的WEB应用程序试验着不同的结构。在这篇文档中我们简要地探究一下一些关于它们的优势劣势。
 
As we experimented, we found some common themes. All the offline-enabled applications we created had the following design issues that needed to be addressed:
  • isolating the data layer
  • deciding which features to implement offline (connection strategy)
  • deciding on the app's modality
  • implementing data synchronization
【翻译】: 就像我们试验的一样,我们发现了一些公共的主题。所有我们制造的可以离线的应用程序有以下一些设计问题需要我们加以注意:
  • 分离的数据层
  • 决定是什么特征实现离线(连接策略)
  • 对应用程序的形式做出决定
  • 实现数据同步

Isolating the Data Layer
【翻译】: 分离的数据层
 
In most web applications today there is no real data layer.
【翻译】: 在今天大多数的WEB应用程序中并没有真正的数据层


Figure: No data layer

 

AJAX calls originate throughout the code, without any single clean communication layer. This is usually fine for AJAX applications because there is only one data source: the server. In effect, the API that the server exposes to AJAX acts as a data layer.
【翻译】: AJAX直接调用代码,而没有任何公共的层。这通常对AJAX应用程序是非常好的,因为只有一个层:服务器。事实上,服务器的API被AJAX当作一个数据层。

Architecture with a data layer

【翻译】: 数据层结构

In general, isolating the data layer is a good first step.

【翻译】: 通常,分离的数据层是好的第一步。

When you add a local datastore to your application, you will have a single place through which all data storage and retrieval requests pass.
【翻译】: 当你为你的应用程序增加一个本地数据存储时,你将有一个单独的位置用来处理你所有的数据存储以及取回经过的请求。

 
Figure: Data Layer

 

For example, if your AJAX application issues a JSON request directly to the server to get all the accounts for a user, you might change this to instead ask an intermediate object for all the accounts for the user. This object could then decide whether to retrieve the data from the server, the local store, or some combination of both. Similarly, when the application wants to update the user's accounts, the app does so by calling the intermediate object. The intermediate object can then decide whether to write the data locally, whether to send the data to the server, and it can schedule synchronization.
【翻译】: 例如,如果你的AJAX应用程序传出一个JSON给服务器获取一个用户的所有帐务,你可能改为请求一个中间对象来取代从服务器读取这个用户的所有帐务。这个对象来决定从服务器,本地存储,还是二者结合的方式重新找回数据。同样的,当应用程序想要更新用户帐户信息的时候,这个应用程序也是去调用这个中间对象。这个中间对象可以决定是否在本地写数据,还是将它们发送到服务器上,这些通过中间对象来决定。
You can think of this intermediate object as a data switch layer that implements the same interface as the data layer. As a first step, you can make the data switch forward all your calls to the data layer that interacts with the server. This step is useful since it is the code path that is followed when Google Gears is not installed or if the user doesn't want to enable the application to work offline. Note that a data switch is not strictly necessary (for example GearPad does not have a data switch layer).
【翻译】: 你可以将这个中间对象考虑成一个数据开关层,它负责实现和数据层相同的接口。第一步,你可以让使你所有的数据开关调用所有你的数据层以与你的服务器相结合。如果Google Gears没有被安装或者用户不想让应用程序离线工作,则这个步骤是非常有用的。注意这个数据开关并不是确实必要的(例如GearPad就没有数据开关层)


Figure: Data Switch Layer

 

The next step, as shown in the figure below, is to create a new local data layer that uses a Google Gears database instead of going to the web server for data. It's simpler if this data layer has the same interface as the existing data layer used to communicate with the server. If the interface is different then some translation needs to be done and you might as well do that inside this data layer.
【翻译】: 下一步,也是关键的一步,就是创建一个本地的数据层来使用Google Gears数据库代替访问WEB服务器上的数据。如果这个数据层拥有和已经存在的数据层相同的接口则与服务器通信将变得容易。如果这些接口不同,你则需要在这个数据层内做一些数据翻译的工作。
To test this step, you can set the data switch layer to talk to this new (local) data layer. You might want to pre-populate the database to make things easier to test.
【翻译】:测试这些步骤,你可以设置你的数据开关曾与新的(本地)数据层之间进行对话。你可能想要预先组装你的数据库让测试变得容易些。
 
 

Figure: Local Data Layer

 

Architecture without a data layer

【翻译】:没有数据层的结构

If the application is not structured with a data layer and adding a data layer is not an option, it is still possible to isolate the data layer by intercepting all the calls to the web server just before they are sent. For example, you could intercept a form submit (listen to the submit event) and decide if the application should use the local data store or the data on the server.
【翻译】: 如果这个应用程序没有一个数据层结构,并且添加一个数据层是无法选择的,这个数据层仍然可能通过中途阻止所有的发向WEB服务器的请求来分离数据层。例如,你可以截断一个窗体的提交(监听提交事件)并且决定应用程序究竟该使用本地数据存储还是服务器上的数据存储。
Implementing this approach involves finding all functions and methods that send requests to the server, and rerouting them. The challenge is that this method requires a lot of extra work, like parsing URLs, iterating over forms as well as generating the same result as the server would. In practice you end up reimplementing large parts of the web server on the client side. Regardless, this can be a viable option for existing AJAX applications that can't be otherwise rearchitected.
【翻译】: 实现这个步骤包括寻找所有发送请求给服务器的函数和方法,对它们重新路由。这个方法的挑战性在于他要求很多额外的工作量,就像分解URL, 迭代表,以及生成和服务器可以生成的相同的结果. 事实上你重新实现WEB服务器客户端部分中很大的一部分。不管怎样,这个是对一个已经存在的AJAX应用程序可行的一个选择,不能被其他方式重构。

Feature Availability Offline

【翻译】: 可用的离线特征
For practical reasons, every feature of an application may not become a feature that's available offline. You need to choose which features you want to support locally and implement the logic that decides when to use the local store and when to connect to the server. We call this the "connection strategy."
【翻译】: 因为实践的原因,每一个应用程序的特征可能不会成为可用的离线的特征。你需要选择哪个特征是你想要支持的本地地并且实现这个逻辑来决定什么时候使用本地存储,什么时候使用服务器连接。我们称这个为“连接策略”。

You might think that you would always want to use the local store since it is faster. However, there are many practical reasons why you may want or need to access the data on server instead. For instance:

  • Data may be so transient in nature that it makes no sense to cache it.
    For example, an application providing real-time stock quotes would not benefit from serving old stock quotes.
  • Some application's data makes sense only while online.
    As an extreme example, an Instant Messaging application makes sense only while connected.
  • The app may choose to store only the most frequently accessed data.
    For example, if the user can set a preference that changes the language of the application, this preference change might not be supported offline because the cost of implementing this offline feature is not worth the benefit for a preference that is seldom changed.
  • Computational and/or disk space requirements make it unfeasible to recreate the feature offline.
    For example, if the feature requires huge amounts of data to be useful, beyond a reasonable amount for a personal computer.
【翻译】: 你可能认为你可能总是使用本地存储,因为他比较快。然而,有很多实际的原因来解释为什么你想或者必须使用服务器数据访问来作为一种替代。例如:
  • 数据可能是瞬时的,缓存它们将没有意义。例如,一个应用程序提供一个实时股票报价,那么旧的数据将没有意义。
  • 一些应用程序的数据只有在线的时候有意义。一个极端的例子,一个即时消息应用程序只有处于连接的状态才有意义。
  • 应用程序可能选择频繁访问数据存储。例如,如果一个用户设置它们的语言偏好,这个偏好的改变可能不需要支持离线,因为这样的偏好几乎很少改变,实现它们的离线则不值得。
  • 计算和/或磁盘空间是否可以实现离线特征。例如:如果这个特征请求大量的有用空间,超出一个合理的个人电脑的空间总量。
Typically, the optimal solution is to use the local store as much as possible, since it's usually faster than a remote connection. However, the more work an application does locally, the more code you need to write to implement the feature locally and to synchronize the corresponding data. There is a cost/benefit tradeoff to consider, and some features may not be worthwhile to support locally.
 【翻译】: 典型的,最理想的方案是尽可能地使用本地存储,因为它通常比远程连接要来的快速。然而,应用程序更多的工作处于本地,更多的代码必须实现本地并且同步通讯数据。这里就得考虑花费和好处之间的权衡,并且一些支持本地的特征并不总是值得去做的。
 
Modality
 【翻译】: 形式

One fundamental question that all offline-enabled applications must answer early is that of "modality".

  • Modal applications have distinct offline versus online modes, usually indicated through some change in the user interface. The user is made aware of the state and participates in switching states in some manner.
  • Modeless applications attempt to transition seamlessly between online and offline states, without significant UI changes. The user does not need to participate in switching states, which the application does automatically.
 【翻译】: 一个基础的问题是所有可以离线的应用程序必须提前回答它的"“形式".
  • 模态应用程序离线和在线模式有明显的区别,通常需要一些用户界面的改变。用户需要知道状态并且在一些风格中参与交换状态
  • 非模态应用程序尝试着让在线和离线之间无缝集成,没有重要的界面改变。用户不需要参与交换状态,这样的应用程序变得自动。”

 

Modal

【翻译】:模态的

In a modal application, when the application is online it communicates with the server. When it's offline, it uses the local store. Data must be synchronized when the app switches between modes.
【翻译】:在一个模态应用程序中,当应用程序是在线的,它与服务器进行通信。当它是离线的,它使用本地存储,数据必须在应用程序转变模式的时候进行数据同步。
The advantage of making a modal application is that it's relatively simple to implement and therefore a reasonable way to bootstrap the application to function offline.
【翻译】:优势:使用一个模态应用程序的优势在于它的实现相对简单,所以它提供了一个解决应用程序离线的合理的方法

The disadvantages are:

  • The user must remember to switch modes. If they forget, they will either not have the data they need when offline, or will work online in an unintentionally disconnected mode.
  • If the network connection is intermittent, the user will either need to choose one of the settings or will have to constantly switch between modes as the connection comes and goes.
  • Since the local store is not always up-to-date, it can't be used to improve the application's responsiveness when connected to the server.

 

【翻译】:劣势:
  • 用户必须记住交换模式。如果它们忘记了,它们将在离线的时候无法得到它们所需要的数据,或者当它们在线的时候它们无意识地在离线模式下工作。
  • 如果网络链接是断断续续的,用户将需要不断地随着网络状态的改变切换程序的模式。
  • 因为本地数据库并不总是最新的,它不能在连接至服务器的时候被用于改进应用程序的响应

Modeless

【翻译】:非模态的

In a modeless application, the application works with an assumption that it is offline, or that it can lose the network connection at any time. The app uses the local store as much as possible, and does continuous, small data syncs in the background when the server is available. Data synchronization is also done when coming back online.
【翻译】:在非模态的应用程序中,应用程序在一种看上去像是离线的状态下工作,或者可能在任何时候失去网络连接。应用程序尽可能使用本地存储,并且持续这么做,当服务器可用的时候在后台进行小的数据同步。数据同步同样存在于从在线转为离线的情况。

The advantages of modeless applications are:

  • A better user experience. The user does not have to be aware of network connectivity or switching states.
  • The application works smoothly even with intermittent network connections.
  • Since the local store is kept up-to-date, it can be used to optimize the server connection.
【翻译】:非模态应用程序的好处:
  • 更好的用户体验。用户不需要知道网络的连接情况和交换状态。
  • 应用程序甚至可以在是断断续续的网络条件下平稳地工作。
  • 因为本地存储保持最新,它可以使用最优化的网络连接。

The disadvantages of modeless applications are:

  • It is more difficult to implement.
  • Care must be taken to avoid letting the synchronization background process consume too many resources and make the overall application feel sluggish.
  • Testing the app can be more challenging, since synchronization logic occurs in the background and not in reaction to specific user actions.
 【翻译】:非模态的应用程序的劣势:
  • 实现比较困难.
  • 必须要注意保持同步的后台进程不会消耗过多的资源而导致应用程序变得缓慢。
  • 应用程序的测试也是一个挑战,因为同步逻辑发生在后台,并且没有明显的用户举动。
The sample application Gearpad is an example of a modeless offline application. It always writes to the local database, and then independently synchronizes the changes with the server.
【翻译】:实例程序Gearpad就是一个非模态的离线应用程序的例子。它总是被写在本地数据库,并且独立和数据库实现同步不同数据。
 
Google Reader is currently an example of a modal application. It has a distinct offline mode that a user must explicitly enable. In Reader's case, implementing a modal state was a pragmatic choice, as it was faster to implement and made it possible to release an early version of Google Reader with Gears.
【翻译】:Google Reader则是一个模态应用程序的例子。它有一个明显的离线模式,用户必须使他可用。根据读者的场合,实现一个实际的选择来决定状态,就像它们快速来实现并且使一个早期版本的Google Reader通过Gears变成可能
 

Data Synchronization

【翻译】:数据同步

No matter which connection and modality strategy you use, the data in the local database will get out of sync with the server data. For example, local data and server data get out of sync when:

  • The user makes changes while offline
  • Data is shared and can be changed by external parties
  • Data comes from an external source, such as a feed
【翻译】:你使用哪种链接和形态策略没有关系,本地数据库中的数据将与服务器的数据进行同步。例如,本地数据和服务器在以下情况进行同步:
  • 当离线的时候用户改变数据
  • 数据被共享并且可以被外部改变
  • 数据来自一个外部源,就像一个feed
 

Resolving these differences so that the two stores are the same is called "synchronization". There are many approaches to synchronization and none are perfect for all situations. The solution you ultimately choose will likely be highly customized to your particular application.

Below are some general synchronization strategies.
【翻译】:将这些不同进行分解,以致两个存储被相同地“同步”调用。这里有许多途径来进行同步,但是没有一个方案是完美的。解决方案最终选择将对你的特殊应用程序高度用户化地

Manual Sync

【翻译】:手动同步

The simplest solution to synchronization is what we call "manual sync". It's manual because the user decides when to synchronize. It can be implemented simply by uploading all the old local data to the server, and then downloading a fresh copy from the server before going offline.
【翻译】:最简单的解决方案就是手动同步。之所以称之为手动是因为让用户决定什么时候进行同步。它可以被简单地实现为上传所有旧的本地数据给服务器,并且在变成离线状态前从服务器下载最新的拷贝。

Manual sync requires that:

  • The amount of data is small enough to download in a reasonable amount of time.
  • The user explicitly indicates when he or she is going offline, typically via a button in the user interface.
【翻译】:手动同步要求:
  • 总的数据是足够小的以保证在一个合理的时间内进行下载。
  • 用户明确指示什么时候他或者她将要离线,典型的通过一个设置一个按钮来实现这个功能。

The problems with this method and with the offline mode it creates, are:

  • Users don't always know the state of their network connections. Internet connections may die unexpectedly or be intermittent, for example, on a bus.
  • Users may forget to synchronize before going offline.
【翻译】:这个方法以及离线模式创建的问题是:
 
  • 用户并不总是知道他们网络连接的状态。互联网连接可能出乎意料地中断了或者断断续续,例如,在一个公交车上。
  • 用户可能忘记在它们将要离线的时候进行数据同步。
Manual sync can be a good way to get started as it is relatively easy to implement. However, it requires the user to have awareness and involvement in the synching process.
【翻译】:手动同步可是以一个好方法的开始因为它相对容易实现。然而,它要求用户知道并且包含进同步的过程中。
 

Background Sync

【翻译】:后台同步

In a "background sync", the application continuously synchronizes the data between the local data store and the server. This can be implemented by pinging the server every once in a while or better yet, letting the server push or stream data to the client (this is called Comet in the Ajax lingo).
【翻译】:在后台同步中,应用程序持续在本地数据存储和服务器之间进行同步数据。这个可以通过在每一次要连接的时候PING一下服务器,让服务器推或者流数据给客户端(这个用Ajax的行话叫做Comet)

The benefits of background synching are:

  • Data is ready at all times, whenever the user chooses to go offline, or is accidentally disconnected.
  • The performance is enhanced when using a slow Internet connection.
【翻译】:后台同步的好处:
  • 数据在任何时候都是准备完毕的,无论用户选择离线还是意外断线。
  • 当使用一个缓慢的互联网连接的时候这个表现就有所提高了。
The downside is that the sync engine might consume resources or slow down the online experience with its background processing (if it's not using the WorkerPool). Using WorkerPool the cost of synching is minimized and no longer affects the user's experience.
【翻译】:不好的趋势是同步引擎通过后台进程将消耗资源或者减慢你的在线体验(如果他不是使用线程池的话)使用线程池这个同步的代价将减到最小,并且不再影响用户的体验。


Figure: Background sync architecture

 

Conclusion

【翻译】:结论

There are many design choices to be made along the way to enabling an application to work offline. This document reviewed a few of the possible choices for the common design issues that arise. The decisions made for an application will need to reflect the user experience you want to achieve as well as the application's limitations and goals.
 
【翻译】:有许多可以让应用程序离线工作的设计选择。这篇文档回顾一些可用的选择。这个将决定你将需要达到什么样的用户体验,并且有什么样的应用程序限制以及所能达到的目标。



【附加】

Samples and Tools download

All of the sample applications and the tools described below are found in the zip file provided here. Download the file to your computer and unzip it to access the files.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值