Monday, November 30, 2015

Three-tier application architecture

The three tier architecture is a client-server software architecture pattern which consists of 3 layers :

Presentation layer (or tier)

Application's topmost level, also called graphical user interface or GUI.
It displays the services to the user as well as the results of operations.
Example of technologies used for this tier : WPF, HTML.

Logic layer (or tier)

Application's processing tier. The business logic is taken care of at this level, this is the brain of the application.

Data layer (or tier)

Application's data management tier. It handles data storage and data access services. Usually, databases are used in this layer.



Advantages of this architecture:

  • Lightweight client workstation
  • Heterogeneous :  many technologies live together (client, server, ..)
  • Safer due the absence of direct link between the client and the data
  • Better distribution of processing load (e.g. load balancing)

Thursday, November 26, 2015

C++ : Private pure virtual method

class Engine
{
public:
    void SetState( int var, bool val );
    {   SetStateBool( int var, bool val ); }

    void SetState( int var, int val );
    {   SetStateInt( int var, int val ); }
private:
    virtual void SetStateBool(int var, bool val ) = 0;   
    virtual void SetStateInt(int var, int val ) = 0;   
};

Although they are private, private virtual functions can be overridden in the derived classes.
This
"allows for better separation of the specification of interface from the specification of the implementation's customizable behavior
(Herb Sutter).

Why ?


Imagine that the clas was written like this, with non-pure virtual methods :

 class Engine
{
private:
    virtual void SetState(bool val ) { // blablablabla };   
    virtual void SetState(int val )
{ // blablablabla };   
};
 and consider the following child class which overloads one method only:

 class MyEngine : public Engine
{
public:
    void SetState(int val )
{ // blablablabla };   
};

One would get in trouble by calling :

MyEngine* myEng = new MyEngine();
myEng->SetState(true);

This statement would  call SetState(int val), converting bool to int.
To have a proper inheritance, the child class shall use the using keyword to hide base class method:

class MyEngine : public Engine
{
          // To prevent SetState( int var, bool val ) from the base class,
          // from being hidden by the new implementation of the other overload

          // (below),
          // you have to put using declaration in the derived class

         using Engine::SetState;

public:
    void SetState(int val )
{ // blablablabla };   
};

This is something that the developer should always keep in mind and it can end to unexpected behaviors if not applied.
With private non-virtual, the developer could have written the first version of derived class without caring about non-hidden methods.

C++ : Inheritance types

class A
{
public:
    int x;
protected:
    int y;
private:
    int z;
};

class B : public A
{
    // x is public
    // y is protected
    // z is not accessible from B
};

class C : protected A
{
    // x is protected
    // y is protected
    // z is not accessible from C
};

class D : private A
{
    // x is private
    // y is private
    // z is not accessible from D
};

 Note that x,y and z exists in all subclasses but are not accessible in all cases.

Thursday, November 12, 2015

C++ : copy constructor

We already know the basic constructor utility which is : initializing internal members. If not constructor is specified, the default constructor initializes each of class' members with their constructor.

Now let's take a look to the copy constructor :

Point (const Point & p);

This constructor will initialize our object by copying another one :

Point p1 (otherPoint);

Note: It is also possible to define our own copy constructor to define more specifically which members should be copied.

Monday, November 9, 2015

c++ : lvalue and rvalue

l-value

C-definition

An object is a manipulatable region of storage; an lvalue is an expression referring to an object....The name 'lvalue' comes from the assignment expression E1 = E2 in which the left operand E1 must be an lvalue expression.
 C Programming Language (Prentice-Hall, 1988)

r-value


C-definition


What is sometimes called 'rvalue' is in this standard described as the 'value of an expression'.
C-Standard 

C++ definition


Every expression is either an lvalue or an rvalue.
C++ 2003 Standard 

Translation

lvalues and rvalues got their names from their roles in assignment expressions 
lvalues refers to objects and are addressable.

int a; // lvalue
char* p;
// *p is an lvalue. '*' operator returns an l-value
*p = 'c'; // OK, we can assign an l-value
*(p + 1) = 'd'; // OK

Conceptually, an rvalue is just a value; it doesn't refer to an object

int a = 5; // 5 is an rvalue
int b = 8; // 8 is an rvalue
State_te state = State_Idle; // State_Idle is an rvalue;

5 = b; // Error ! we can not assign an rvalue
State_Idle = 8; // Error ! we can not assign an rvalue
a = &5; // Error ! & Operator requires an lvalue and returns an rvalue
&b = 8; // Error ! & returns an rvalue 

Note: In C++, rvalues can refer to objects but are still not lvalues.

 
biz.