The present invention relates generally to updating observable values for multiple user-interface components, and more particularly to a lightweight mechanism of updating the observable values.
There are a few ways to handle updating observable values when multiple user-interface components (e.g. grids) show the simple or native data. The first way is a flyweight pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation uses an unacceptable amount of memory. Often some parts of the object state can be shared and it's common to put them in external data structures and pass them to the flyweight objects temporarily when they are used. The second way is an observer pattern. An object called the subject maintains a list of its dependents called observers. The object notifies observers automatically of any state changes, usually by calling one of their methods. The observer is a key part in the MVC (Model-View-Controller) pattern. The third way is an observable wrapper. The observable wrapper combines the first and second ways. By wrapping a native object in an observable container that exposes getters and setters according to Java Beans standards, changes of the native type may be observed. But, the third way is a rather expensive way.
Embodiments of the present invention provide a method, computer program product, and computer system for updating observable values for multiple user-interface components. A computer system, in response to receiving a request from the multiple user-interface components, reads first values from a cache, wherein the first values are indexed by keys. The computer system reads second values from persistent storage, wherein the second values are indexed by the keys. The computer system compares the first values and the second values based on the keys. The computer system writes to the cache the second values as new values of the first values. The computer system notifies one or more observers for respective ones of the first values, wherein the respective ones of the first values are changed. And, the computer system notifies the one or more observers for the first values that reading and writing operations are finished.
As will be appreciated by one skilled in the art, aspects of the present invention may be embodied as a system, method or computer program product. Accordingly, aspects of the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment (including firmware, resident software, micro-code, etc.) or an embodiment combining software and hardware aspects that may all generally be referred to herein as a “circuit,” “module” or “system.” Furthermore, aspects of the present invention may take the form of a computer program product embodied in one or more computer readable medium(s) having computer readable program code embodied thereon.
Any combination of one or more computer readable medium(s) may be utilized. The computer readable medium may be a computer readable signal medium or a computer readable storage medium. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples (a non-exhaustive list) of the computer readable storage medium would include the following: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Computer program code for carrying out operations for aspects of the present invention may be written in any combination of one or more programming languages, including an object oriented programming language such as Java®, Smalltalk, C++ or the like and conventional procedural programming languages, such as the “C” programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the latter scenario, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet Service Provider).
Aspects of the present invention are described below with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and/or block diagrams, and combinations of blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions/acts specified in the flowchart and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer readable medium that can direct a computer, other programmable data processing apparatus, or other devices to function in a particular manner, such that the instructions stored in the computer readable medium produce an article of manufacture including instructions which implement the function/act specified in the flowchart and/or block diagram block or blocks.
The computer program instructions may also be loaded onto a computer, other programmable data processing apparatus, or other devices to cause a series of operational steps to be performed on the computer, other programmable apparatus or other devices to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide processes for implementing the functions/acts specified in the flowchart and/or block diagram block or blocks.
Please note that in the drawings and descriptions below, the term “Observer” in the general Observer pattern is replaced by the term “Listener”. The Observer pattern is very common in the Java community. However, the implementation of an Observer is often called a Listener, and related methods to add and remove observers follow the same naming conventions. In the text below, the terms Observable and Listener refer to exactly the same concept and can be used interchangeably. The diagrams and descriptions use the term Listener, since this reflects the Java naming conventions and the exemplary embodiment.
In the first option, the changes of the individual values are respectively notified to multiple UI components 130. If a native value of a specific one of lightweight observable values 113 is updated, all listeners of the specific one of lightweight observable values 113 are notified. Specific one or more of multiple user-interface components 130 show the native value; thus, the specific one or more of multiple user-interface components 130 are notified by lightweight observable values 113 through observable values interface 117 thereof. In the second option, notification of changes of massive native values is handled in one go rather than one by one native value. The notification of the changes of massive native values is implemented by observable updater 111 through updater interface 115 thereof. These two options enable listeners to choose a best way to monitor the changes of the native values: one call for one native value change or one call for changes of many native values. The best way depends on the runtime context, nature of data, and how frequently data are changed.
Referring to
In the exemplary embodiment shown in
To maximize performance where many listeners are registered for a value, the listeners are grouped by an identifier: groupId in ObservableValue 213. The groupId is typically a UI control; when the UI control is disposed, all the listeners registered by this control can be detached by a single method call. The identifier groupId allows all the listeners in a group to be removed in one go rather than one by one. In ObservableValue 213, clear(groupId:Object) removes the listeners grouped by the identifier of groupId.
ObservableValue 213 holds a key used to register a value in the cache of ObservableUpdater 211. In ObservableValue 213, updaterKey keeps the reference in the cache alive as long as the value has at least one registered listener.
In the exemplary embodiment shown in
At step 305, value update program 110 compares the values from the persistent storage 120 and the values from the internal cache. At decision block 307, value update program 110 determines whether a matched key is found in the internal cache. In response to determining that the matched key is not found in the internal cache (NO branch of decision block 307), value update program 110, at step 311, write a respective one of the values from the persistent storage to the internal cache. At this step, value update program 110 wraps the value whose key is not found in the internal cache and adds it into the internal cache. Because no listeners have been registered for the value whose key is not found in the internal cache, value update program 110 does not fire any event.
In response to determining that the matched key is found in the internal cache (YES branch of decision block 307), value update program 110, at step 309, checks a respective one of the values from the internal cache. At decision block 313, value update program 110 determines whether the respective one of the values in the internal cache has been changed. In response to determining that the respective one of the values in the internal cache has been changed (YES branch of decision block 313), at step 315, value update program 110 writes the respective one of the values from persistent storage 120 to the internal cache. In this exemplary embodiment, this is done by setValue( ) in ObservableValue 213 of value update program 110.
Value update program 110, at step 317, notifies one or more listeners of the respective one of the values in the internal cache that a change is made. Here, value update program 110 gives interested parties (e.g. grids showing the cached values) an option to act on the changes of individual values (typically to update the UI component).
After step 311, step 317, or in response to determining that the respective one of the values in the internal cache has not been changed (NO branch of decision block 313), value update program 110, at decision block 319, determines whether value update program 110 has written all values from persistent storage 120 to the internal cache.
In response to determining that value update program 110 has not written all values from persistent storage 120 to the internal cache (NO branch of decision block 319), value update program 110 goes back to step 305.
In response to determining that value update program 110 has written all values from persistent storage 120 to the internal cache (YES branch of decision block 319), value update program 110 notifies, at step 321, all listeners that read and writing operations have finished. Here, value update program 110 gives interested parties (e.g. grids showing the cached values) an option to act on the read/write events and fetch all interesting values (e.g. the cached values shown in grids) in one call when ObservableUpdater 211 has made changes of the values. Thus, multiple UI components 130 that have requested the values register themselves as listeners for the values in the internal cache.
Referring to
One or more operating system(s) 431 and one or more computer program(s) 433 reside on one or more computer-readable tangible storage device(s) 430. In the exemplary embodiment, value update program 110 shown in
The computer device further includes I/O interface(s) 450. I/O interface(s) 450 allow for input and output of data with external device(s) 460 that may be connected to the computer device. The computer device further includes network interface(s) 440 for communications between the computer device and a computer network.
The flowchart and block diagrams in the Figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems that perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.