samedi 1 août 2015

Ownership of messages, Which Designpattern to use?

I have a system, that receives messages (data chunks with a type) from somewhere (in this case network). Those are stored in a queue once received. Then these messages should get dispatched to handler functions and this got to happen fast (lots of messages)

Currently the system is designed in a way, that each Message type is an own class and overwrites a virtual function run(Handler&) which basicly calls the correct method in the handler. E.g.:

class PingMessage: public Message{
    ... // Some member variables
    void run(Handler& handler){
      handler.handlePing(*this);
    }
}

class Handler{
  void handlePing(const PingMessage& msg){...}
}

In this design, the Queue deletes the message, after it got dispatched. The problem is: Some handler functions need to store the message to execute them at a later time. Copying the message is not only a waste of memory and time (it gets deleted right after dispatch) but also not possible sometimes (complex deserialized data structures) So best would be to pass the ownership over to the handler.

I have the feeling, there is a design pattern or best practice for this. But I can't find it.

What I could imaging is calling a generic handler function "handleMessage(Type, Message*)" that switches on the type and does a dispatch with the Message static_cast'ed to the right type. Then it is clear by the convention of passing a pointer, that the handler is responsible for deleting the message. Maybe even use a base class, that does the switch and implements all handler functions empty. If a handler functions returns true, the handleMessage function deletes the Message, otherwise it assumes, the callee stored it somewhere. But I'm not sure if this is the right approach or if it incurs to much overhead. There seems to be to much room for errors.

How to deal with the lack of reflection in Swift?

As an experienced Objective-C developer who is now learning Swift , I'm really missing some of the reflection and dynamic features of Objective-C.

For eg: I had written a JSON serializer which automatically mapped keys and values using KVO and Objective C introspection , and there are open source libraries like Mantle which do this.

I could declare my object as an NSObject subclass and proceed but I feel that this is not the Swift way of doing things.

Is there any other way to accomplish the same tasks , while avoiding boilerplate , using what Swift provides ?

What is the best practice (Pattern) to communicate Asynchronous Custom Components with Activities

I did a lot of research but couldn't find a definitive answer about this question. What is the best way to communicate custom components (Like and Async task) with and Activity?

Currently I'm using this approach:

  1. Declaring an interface for the component
  2. Register the interface on the Activity
  3. Maintain a fragment with setRetainInstance(true)on the Activity, registering the component's interface every time the Activity is recreated
  4. Passing the result to the Activity using the interface

My doubt is about the item 3. As far as I understand there will be a moment when the Activity is completely destroyed, and the Custom Components is registered with a null callback ( My fragment unregister it when onDestroy is called ). So, if the component completes it's operation at this time, there won't be any recipients for the message available at the moment. The result could be lost.

I count't reproduce this scenario. But I believe it exists ( by all means, correct me if I'm wrong ).

I considered using a loop with some delay and a counter ( to limit the attempts ) that executes util a callback is found. But this 'solution' looks like a clumsy joke. I also considered using a EventBus or som other library, but I don't know if it work correctly.

Thanks =)

Urgent string pattern program clueless about this

Please help me with this I am absolutely clueless about this program my teacher gave this homework I have got to submit asap

Accept a sentence and print it in the following pattern :

This

This is

This is an

This is an example

What are sub-modules in ES6?

Suppose I have a ES6 module named a.js. a.js modules imports b.js and c.js modules. Similarly b.js imports d.js and c.js imports e.js.

Now what I want to know is which ones are sub-modules and super-modules.

I feel b.js and c.js are sub-modules of a.js. But I am not sure. Really confused. Please help.

Thanks in advance.

What to do if classes with same interface having similar but different method signature?

What to do if classes with same interface having similar but different method signature?

Let's say I have a project to calculate different costs (to get a total cost at last).

In my program, there is several calculator classes, namely ACostCalculator, BCostCalculator and so on. When a calculate() method is invoked to calculate a cost, a cost container is passed to those cost calculators to. In a good scenario, I can make a CostCalculator Interface for every cost calculators.

However, the calculation for different cost required different resources. In my current program, it make be like:

//getResource() are costly method while several costs need this. So do it outside calculate() method.
ResourceA resourceA = getResourceA(); 
ResourceB resourceB = getResourceB();

CostContainer costContainer = new CostContainer();
CostCalculator aCostCalculator = new ACostCalculator();
...
CostCalculator eCostCalculator = new ECostCalculator();

aCostCalculator.calculate(costContainer);
bCostCalculator.calculate(costContainer)
cCostCalculator.calculate(costContainer, resourceA);
dCostCalculator.calculate(costContainer, resourceA);
eCostCalculator.calculate(costContainer, resourceA, resourceB);

If the signature is exactly the same, I may make a loop conveniently to do it at once. However, since they are similar but different, I can't even make a good interface.

I am not sure if there is good ways to do so. What I can think of is generalizing all calculate() method to into

calculate(CostContainer costContainer, List<Object> resources);

Any ideas? Thanks for answering.

How should I make my classes interact

I've been writing PHP within WordPress for a while, but I'm pretty new to straight PHP applications. I'm writing a little application that uses a few different APIs to do cross posting.

I've written the classes, but I'm not sure of the best way to have them interact.

A bit of background on the classes and their functionality. I've got a class for each API (Reddit, Imgur, Twitter), a class with some Curl helper functions, a class that carries out the process. I don't think I'll ever need more than one instance of any of these current classes. Nor will they ever need to be extended.

From what I've read, I've got a few options:

  • Instantiate each class as a global variable (I've heard I should avoid this)
  • Make the classes singletons (Also heard I maybe should avoid this)
  • Static methods - though some of the classes need to be instantiated and I'm not sure how that would work.

Those are the methods I've heard of. I'm expecting the answer will be none of the above. How do frameworks like Laravel do this?

This might be subjective, but I'm sure I'll learn something about patterns.

Edit: I probably should add some examples of the types of interactions required.

  • An API class using the CurlHelper class (I'm currently using a static method: $post_string = CurlHelper::createPostString( $post_data );)
  • Generating a title in one class for use when cross posting to each API