jeudi 2 mars 2017

How to wrap a C++ library

I'm developing an application which is using a library and I would like to wrap this library so that it does not goes to deep into my application code. Thanks to that I could change the library I'm using just by re-implementing my wrapper classes.

Suppose that I have a library LibA. It gives me 2 objects to work with, LibAObj1 and LibAObj2. LibAObj2 has a method using LibAObj1.

Here can be a simple definition of their declaration

class LibAObj1 {};

class LibAObj2 
{
     void action(LibAObj1 &obj);
};

Now I would like to define an interface that my application can use to wrap those objects in my application code

For instance:

class ItfLibAObj1 {};

class ItfLibAObj2 
{
public:
     void action(ItfLibAObj1 &obj) = 0;
};

The problem comes whenever I want to implement my interface ItfLibAObj2.

class ImplLibAObj2 : public ItfLibAObj2
{
public:
    void action(ItfLibAObj1 &obj)
    {
        <how to get my LibAObj1>?
        obj.action(LibAObj1);
    }
private:
    LibObj2 obj;       
}

The question is actually in the pseudo code. How to get my LibAObj1 contained in my ItfLibAObj1 reference? I could add a getter function in LibAObj1 interface to return a void pointer that I would cast but I don't find that elegant in C++.

Is there any kind of design pattern I could use to solve my problem? Or do I just have a design issue?

Note that I'm not wishing to select which library to use at run time.

Thanks a lot for your help.

Kind regards

which design pattern suitable, in which scenario to develop android application?

How to use design patterns in android application?. I know architectural pattern such as MVC, MVP and MVVM, but I am confusing with design patterns. I want know which design pattern is suitable for which scenario. The design patterns mainly classified into 3 categories. Which are 1)Creational Pattern(how to create object) 2)Behavioral Pattern(how to communication between objects) 3)Structural Pattern(how to compos objects). I want to know the practical scenario to use above design patterns to develop android application.

C++ Redux. Say what? [on hold]

This is was part of conversation with my colleague. We tried to solve a problem in our system, that our solution today doesn't solve it.

Our system is multi-threaded system developed in C++.we have couple of threads in the system, and each thread has his own state machine. Each thread communicate with other processes and changes his state accordingly. To manage the states for each thread it is a nightmare. We tried boost State Machine with boostio but it too complicate and we want KISS. So i thought maybe to take client side logic, which has also concurrency and states, and to modify it a little bit to fit the server logic. So Redux, yes!

let take for example a system which give a service of a conference calls. each customer can register to system and create a conference call and add other clients to his own call. each conference has different capabilities, and those are defined according to the client license. for example 4k bit rate for golden customer and 1K for silver. the system can manage only 50 concurrent conference calls, and in each conference you have 100 participants max. This is an example for my test case.

the challenges are:

  1. creating the thread pool for all the conferences
  2. each conference must manage a state machine to defined the conference state. for example: client send SIP register, 200OK etc. in addition, the conference object needs to communicate with the license server to get the client policy for the conference. this is Async operation (server send policy query, and wait for an answer).
  3. Async events handling: during a call we must have

this is the reason why I come to Redux. "Redux is a predictable state container for JavaScript apps...". yes I know everyone know what Redux is, and this is the reason why I want to adopt it to my solution:

  1. help to manage data flow
  2. state machine
  3. Async events

implementation aspect:

  • Creating thread pool to manage all conferences
  • Add observer pattern for the real time events (and also for the async responses for the requests to the license server)
  • using dispatcher to manage the event Q for all the threads

btw, for messaging and ipc I use thrift and RabbitMQ

My questions are:

Do you think is it a good idea to take redux way to solve my problems? (server side problems)

Do you have any different solution?

mercredi 1 mars 2017

Instead of letting the subclasses provide the object creation implementation why not product obtained using polymorphism

As per GOF book, Factory method pattern

Define an interface for creating an object, but let the subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclass.

Structure of the pattern

public abstract class Factory {
    public abstract IProduct createProduct();
    private void performCriticalJob(){
        IProduct product = createProduct();
        product.serve();
    }
    public void executeJob(){
        //some code
        performCriticalJob();
        //some more code
    }
}

  1. Factory needs an object (whose concrete class is not known or whose concrete class may change as per the different application type ) to perform a task.

  2. As it does not know which class to instantiate, one standard contract is set for the type of object needed, this contract is put in an Interface.

  3. Base factory class declares an abstract method to return an object of type as above defined interface. It lets subclasses decide and provide the implementation of object creation.

  4. For completion of the task it needs an object which it simply fetches by calling the abstract method.

Question To achieve the intent in the scenarios as above (defined scenario for Factory method pattern) of this pattern why just normal polymorphism is not being used as below

public abstract class Factory {
    private void performCriticalJob(IProduct product){
        product.serve();
        //some code
    }
    public void executeJob(IProduct product){
        //some code
        performCriticalJob(product);
        //some more code
    }
}

Differences Between Analysis and Design

What are the main differences between Analysis and Design, could you explain with examples or diagrams.

Is my assumption correct?

Private class data design pattern in Java

I'm reading about private class data design pattern here and I'm trying to understand what it can really accomplish.

From what I understood private class data design pattern is a structural pattern aiming to reproduce "readonly" attributes even for the class itself: while "private" attributes are visible and editable only to the class itself, attributes in the "private class data" can't be changed at all (even by accident). The only solution is to provide a setter in the private class data, although (at least in my opinion) if the private class data has all the setters of the attributes, then we might have defeated the pattern very purpose.

Assuming my understanding is correct, this lead to a question: Even if the main class can't change any private class data attributes, it can set the reference of the private class data itself, populating it with the variables it wants to change.

In other words, an uncaring developer might do something like this:

public class MainData {
    int foo;
    int bar;
    public MainData(int foo, int bar) {
        this.foo = foo;
        this.bar = bar;
    } 
    public int getFoo() {return foo;}
    public int getBar() {return bar;}
}
public class Main {
    private MainData mainData;
    public Main(int foo, int bar) {
        this.mainData = new MainData(foo, bar);
    }
    public doSomeWork() {
        //correct behaviour
        this.mainData.getFoo() + this.mainData.getBar();
        //now I want to trick the pattern
        this.mainData = new MainData(this.mainData.getFoo(), this.mainData.getBar()+4);
        //I've changed bar :(
    }
}

Since the "readonly" attribute is not compile-enforced (unlike C# via readonly reserved word), in Java a lazy developer might do something like this. If it's true, then why should we use this design pattern at all? Unlike other patterns (like singleton) this pattern doesn't enforce anything, so why should we using it at all?

  • It would be great if you can provide example where you've used this pattern and it concretely helped you solving some software issue;
  • Let's stay on Java: I know in C# everything is much easier, but there the pattern is just plain silly because of readonly reserved word;

Thanks for any kind reply!

Singleton pattern in baseclass-subclass scenario

I am under the impression that, using Singleton pattern I can limit the number of instantiation of a class to one object. And keeping that in mind, have a look at the below code:

class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
    if cls not in cls._instances:
        cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
    return cls._instances[cls]

class base1(object):
    __metaclass__ = Singleton

class base2(base1):
    pass

class base3(base1):
    pass

class base4(base2):
    pass        

obj1 = base4()
obj2 = base4()

print obj1 is obj2 #prints True

obj3 = base3()
obj4 = base3()

print obj3 is obj4 #prints True

print obj1 is obj3 #prints False

So the final print statement prints a False. What could be the best way to go ahead in achieving the goal, "Always return the same base1 object no matter which sub class instantiates it".