mardi 2 février 2016

load and save data struts2

I need to load some data from a table to show in a JSP page.

Then, when the user pushes on a button, they'll send the form to save in a database.

I wonder if I can create a method to load the init data only (I don't want to call the method execute, which will save the data).

I've tried it with implements Preparable but it calls execute method. In Struts2 I have a method called init to load data but I can't do it in Struts2.

What should I do, create an action to load data (loading with execute method) and another action to save data?

lundi 1 février 2016

Game AdModule Architecture

UML Diagram I'm making this as a sample project in Unity. AdManager is a singleton Monobehaviour all other entities are C# files. I would like to discuss the architecture of this module. The diagram is not complete because the free version of the tool only supported this much. My basic requirements are simple Develop an Ad module that shows ads on multiple platforms catering multiple ad networks. Can be easily integrated into any project.

How I'm thinking of implementing it. Client only interacts with the AdManager which will be provided a PlatformAdController using AbstractFactory pattern. AdNetworkController will implement its corresponding AdNetwork. Then according to need multiple (Waterfall) AdNetworkController can be present in a PlatformAdController.

Am I going in the right direction?

What would be a good name for the Interface?

How to implement the lower(AdNetwork) layer i.e. Is there a need of interface/baseClass?

How would i mapping REST API to Multiple Java instances

working on a side project and wondering how i would be mapping a single rest api to multiple java instances using rest. My first though would be to use the path to differentiate between instances. Example api/instance1/... and api/instance2/ ... Any suggestions on how i would solve this problem or any patterns.

If Singletons are so bad, how would one go about managing application configuration?

I know that the Singleton design pattern is frowned upon for various reasons. They're not easy to mock for unit testing, they're not thread safe, etc, etc. But now and again, I'm faced with a situation where an application is driven by a configuration file (application metadata). Everything about this application is contained in its configuration file (let's say json, that the app downloads when its config changes). The only purpose of this app is to serve the user a UI that wraps the configuration and allows flow. Without this config file, the app is useless. I'm thinking that this is a pretty common scenario for a lot of applications.

So given an application that's 100% driven from a config file, how would you design code to avoid using a singleton? Would you constantly read from this json file only when needed, and only a specific section? Meaning, you'll have to deserialize the json into objects whenever the UI calls for it? I mean, isn't this config file the same thing as a singleton anyway? I'd be interested in hearing how people would approach this.

Which design pattern to use to process different files in java?

I have simple task to read the information from different files xml, rdf, txt with different structure and put it in some POJO custom object(MyObject). SO I am wondering which design pattern to use to make my code with better design.

I think on the problem and I think I need Factory Patter and the Iterator Pattern. On the factory to pass the file:

ReaderFactory factory = new ReaderFactory("input.rdf");
//ReaderFactory factory = new ReaderFactory("input.xml");
Iterator<MyObject> iter = factory.getIterator();

So the factory base on the file extension to choose which implemnetation of the iterator to return - these which reads rdf,xml or txt. Then with this iterator I easily can process the objects. Add add new files extensions reading in the future.

Multiple instances with revealing module pattern

At every project I'm asking myself which solution is better if I need multiple instances of an module.

1: Revealing module pattern:

var Module = (function () {

    var init = function () {
        $('[data-slider]').each(function() {
            $(this).slider();
            remove($(this));
        });
    };

    // always need current element
    var remove = function($this) {
        $this.remove();
    };

    return {
        init: init
    };

})();

Module.init();

2: Real multiple instances:

var Module = function () {

    var options;

    var init = function (data) {
        options = data;
        options.$this.slider();
        remove();
    };

    // $this is always stored in options
    var remove = function() {
        options.$this.remove();
    };

    return {
        init: init
    };

};

$('[data-slider]').each(function() {
    new Module().init({
        $this: $(this)
    });
});

In my opinion the second solution is much "cleaner" but I almost always see the first solution. Or is there something better for doing this? What are you using?

Function that acts like a decorator, but uses mutation instead of wrapping?

A decorator adds additional functionality to an object by "wrapping" around the original value.

Is there a name for an object or function that ingests a pre-constructed object and adds additional functionality to it via mutation?.

Example:

function mutatesObjects(target) {
  target.additionalFunctionality = "I've been mutated!";
}

This is similar to a contrcutor or factory except that it is adding functionality to an object that was already created.

Is there a name for this pattern?