lundi 12 mars 2018

alternative to command pattern invoking list of dao implementations

In my application, I am using a Command Pattern which has similar structure to the below:

Command Pattern

I have different database DAO implementations instead of the HomeElectronics receiver implementation as in the linked example above.

One Macro command implementation looks like this for example:

public class AllUnprocessedEvents implements Command {

    List<ITradeDAO> tradeDAOs;

    public AllUnprocessedEvents (List<ITradeDAO> tradeDAOs){
        this.tradeDAOs = tradeDAOs;     
    }

    @Override
    public void execute() {
        for (ITradeDAO dao : tradeDAOs) {
            dao.processUnprocessedEvents();
        }
    }
}

I create such macro command implementation because I have a spring component that on schedule I want to invoke a method that will process all my unprocessed events

public class TypeATradeDAO implements ITradeDAO {

    @Override
    public void processUnprocessedEvents() {
        //query Type A unprocessed trade events
    }
}

public class TypeBTradeDAO implements ITradeDAO {

    @Override
    public void processUnprocessedEvents() {
        //query Type B unprocessed trade events
    }
}

My Spring Scheduler Component something like this:

@Component
public class ProcessUnprocessedEvents {

    AllUnprocessedEvents allUnprocessedEvents;

    List daoList = new ArrayList<>;

    @PostConstruct
    private void postConstruct() {
        //initialise DAO list 
        daoList = Arrays.asList(new TypeATradeDAO, new TypeBTradeDAO);
        allUnprocessedEvents = new AllUnprocessedEvents(daoList);
    }


    @Scheduled(...)
    private void process() {
       allUnprocessedEvents.execute();
    }
}

As you can see, this approach means I have to add each of the DAO implementations to a list in my @PostConstruct code. I would like to understand if there are better alternative ways of achieving this instead of the command pattern?

I have several dao implementation for each types I support in my application. There is potential to support more in which case I will have create a new DAO and then add to this list. Is there a better alternative design which may not involve adding DAO implementations to the list?

Aucun commentaire:

Enregistrer un commentaire