State Machine for Multi-thread Object

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’

// ZPayloadFetcher.h

#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>

#define TRACE(fmt, args...)      \
        printf("[Payload Fetcher]\t " fmt "\n", ## args);

class ZPayloadFetcher
	void Initialize();
	void Release();
	int Process();


Continue reading “State Machine for Multi-thread Object”

Proxy Pattern Instruction

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.

// IOutput.h
#ifndef _I_OUTPUT_H_
#define _I_OUTPUT_H_
#include <string>
#include <stdio.h>

#define OUTPUT_TRACE(fmt, args...)      \
printf("[Output][%s][%d]\t " fmt "\n", Name.c_str(), Index, ## args);
class IOutput
    virtual void Initialize() = 0;
    virtual bool Process() = 0;
    virtual void Release() = 0;
    std::string Name;
    int Index;


Continue reading “Proxy Pattern Instruction”

DataItem vs Struct(class) in Cpp

0. Background

Just as usual, we start this topic with simple case and code. We need one person’s information, whatever it for, we just need them, maybe only print out. So what will we do?

struct Person
    std::string Name;
    std::string NickName
    std::string Address;
    std::string Org;
    std::string Gender;
    int Age;
    int Height;
    int Weight;

    // Other information
    // ...

Then, wherever we get these data, whether from database, user input, file, manipulated, etc…, when we need to use it, ex, print it out, we write the code as follows,

void PrintPersonInformation(const Person& person)
    printf("Name: [%s]\n", person.Name.c_str());
    printf("NickName: [%s]\n", person.NickName.c_str());
    printf("Address: [%s]\n", person.Address.c_str());
    printf("Org: [%s]\n", person.Org.c_str());
    printf("Gender: [%s]\n", person.Gender.c_str());
    printf("Age: [%d]\n", person.Age);
    printf("Height: [%d]\n", person.Height);
    printf("Weight: [%d]\n", person.Weight);

    // Print other information
    // ...

Continue reading “DataItem vs Struct(class) in Cpp”

Anti-Arrow Code Vs Goto

0 Background

Let’s start with code as usual, Continue reading “Anti-Arrow Code Vs Goto”

Optimazation in Sorted Container

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.

Continue reading “Optimazation in Sorted Container”

Tiny Observer Pattern in Cpp

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.

Continue reading “Tiny Observer Pattern in Cpp”

Singleton Details in Cpp

0 Background

What is singleton?

Here is the wiki reference:

In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton. Continue reading “Singleton Details in Cpp”

Secret of Forward Declaration in Cpp,

0. Background

Let’s start with demo code, for programmer, I think there is nothing more straight forward than code,

Attention, my environment is,

Linux version 3.17.7-200.fc20.x86_64 (gcc version 4.8.3 20140911 (Red Hat 4.8.3-7) (GCC) ) Continue reading “Secret of Forward Declaration in Cpp,”

Blog at

Up ↑