QUITERS NEVER WIN

0. Background

Actually, this article is an extended one for the last ‘State Machine for Multi-thread Object’. In that article, a scenario of multi-thread object is dressed to described the importance of status machine usage. Operation requests could be rejected if the target object is not in expected status. It normally occurs in the object that maintains its own job/event thread to deal with the operations that could not be executed and completed intermediately. By the use of that ‘reject’ strategy, we could ensure the object not response the invalid request, but what if the requirement is ‘postpone’, which means if the object is not in valid status(such as working instead of idle), operation requests should be postpone until the instead simply throw away? Here we need a event queue/loop to cache the operation request/event until the object is idle, just like a data buffer. In this the event queue could be introduced at first, then event loop in the future.

0. Background

State machine, the most famous pattern I think, could be not only used in object oriented programming, but also in process oriented one. We could define state for anything, object, process, conclusion, data, etc… Just let me introduce one scenario for state machine usage, an useful one to deal with the multiple thread object.

Just as usual, we start with code.

Supposing we are designing a data fetcher to get data from web server/local server… anything, through any protocol, named ‘ZPayloadFetcher’

0. Background

Image that, we are designing a system/platform/application, which could have multiple kinds of output, like image/video/audio/text/…. Oooh, in fact, every thing has output, whatever you are working on. Facebook shows image/text as comment, amazon generates orders, google list everything you need, links/images/articles….So let us start with the simplest scenario, ImageBox and TextBox, most common controller objects in every MVC coding framework, naming them class ‘TImageBox’ & ‘TTextBox’, to show image and text. To make it object oriented, at least we need one interface for them, let me name it IOutput.

0 Background

0 Background

Get the index from input value of a container, like array/vetcor/etc…, is the basic feature every programmer needs to implement in daily work. When the volume is really low, no one cares about the performance, a correct output is all the requirement. If the volume is really high, facing million/billions of data stored in the container, the performance would become more and more important. We have to use all we have to speed up the scenario to get find the input value. In fact, the key is as simple as everyone knows, what is just reduce the loop count and use as much informations we have as we can. Let’s start with the very beginning.

0 Background

Observer, I think every programer is too familiar with this name, even when we are still in campus. If just open any of open source projects, so many observer patterns would make feel tricky, and make the code not straight forward enough about the implementation at the first sight.