I want you to consider the following program and think about the output logged in the console,
Is it 11 or 21? As most of you would guess correctly, the answer would be 11. That’s because, once the value of b is computed, it is no longer dependent on the value of a. As a result, changing the value of a does not change the value of b.
Suppose the value of a changes over time, e.g., it may be a user input from a webpage that can be modified by the user anytime. And we want b to be calculated as soon as a is changed. A new programming paradigm called Reactive Programming is used for such problems.
An example of Reactive Programming would be a spreadsheet. If you have used it before, you know that when you write a formula for a cell like “=A1+B1″, the value of the cell gets modified as soon as the value in either cell A1 or cell B1 is modified.
Let us now modify our example using Reactive Programming. The value of a is going to change every 3 sec, and as soon as it does, b will be modified. The resulting reactive program is following:
- Jafar Husain (from Netflix) explains Reactive Programming based on known design patterns and how it is being used in Netflix. [video]
- Why do we care about reactive systems – The Reactive Manifesto. [link]
[Prereq: Windows, Visual Studio, Eclipse]
[Project link: https://github.com/rfaisal/ThriftStarterCSharpServerJavaClient]
How it started:
Recently I have been introduced with a very cool technology called Apache Thrift. It is originally developed by Facebook and later open-sourced in Apache Software Foundation. It is one of the core building block of the Facebook technology. The technical paper introducing Thrift can be found here.
What is it:
In layman’s terms, Thrift allows an application written in one language (e.g., Java) to exchange data with an application written another language (e.g., C#). Probably the most popular technology in this area is SOAP. Although SOAP is more general, but Thrift has less overhead. In technical terms, Thrift is a Remote Procedure Call (RPC) framework that can be used to develop scalable cross-language services. Thrift is an interface definition language, i.e., you can only write interfaces or pure abstract classes by Thrift. The Thrift compiler can generate corresponding classes and interfaces for any particular language (e.g., C#) from the Thrift interface. The server (e.g., C#) should implement these interfaces and start a service, and the client (e.g., Java) can call the functions of these interfaces by communicating with the service.
Dynamic Programming is a discrete optimization technique. It means, the variables we want to calculate using this method are discrete. As an example, if x is one such variable then is acceptable, but ‘x is a real number between 0 and 1′ is NOT acceptable since there are infinitely many real numbers between 0 and 1. In math terms, we can say that the domain of the variable x is a countable set.
Problems that are solved by dynamic programming are recursive nature. Let’s look at the problem that ask us to calculate the sum up to a number, i.e., if is such a function then . The recursive definition (assuming i is non negative) is following:
Few years back, I wrote a simple logging library in ActionScript3 using the Chain of Responsibility pattern. Recently, I dug up the old codes, made some modifications, and put it in github for other people to use and contribute. In this post, I will mainly focus on how to use the library and do some project specific modifications, but I will also briefly discuss my reasons for using the Chain Of Responsibility and the Singleton Pattern. I started writing this library with the following 4 goals in mind:
- It should be easily accessible to all of the classes of a project.
- It should be able to write logging information to different output sources, i.e., file, database, console, etc., and additional output sources can be added easily.
- It should be able to handle different levels of logging like FATAL_ERROR or INFO (as different actions may be needed for handling different levels), and additional levels can be added easily.
- It should require minimal setup.