All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.puremvc.java.patterns.command.MacroCommand Maven / Gradle / Ivy

Go to download

PureMVC is a lightweight framework for creating applications based upon the classic Model-View-Controller design meta-pattern. This is the specific implementation for the Java language. It does not support modular programming since it uses Singletons as Core actors rather than the Multiton used in the MultiCore Version.

The newest version!
//
//  PureMVC Java Standard
//
//  Copyright(c) 2019 Saad Shams 
//  Your reuse is governed by the Creative Commons Attribution 3.0 License
//

package org.puremvc.java.patterns.command;

import org.puremvc.java.interfaces.ICommand;
import org.puremvc.java.interfaces.INotification;
import org.puremvc.java.patterns.observer.Notifier;

import java.util.Vector;
import java.util.function.Supplier;

/**
 * 

A base ICommand implementation that executes other ICommands.

* *

A MacroCommand maintains an list of * ICommand Class references called SubCommands.

* *

When execute is called, the MacroCommand * instantiates and calls execute on each of its SubCommands turn. * Each SubCommand will be passed a reference to the original * INotification that was passed to the MacroCommand's * execute method.

* *

Unlike SimpleCommand, your subclass * should not override execute, but instead, should * override the initializeMacroCommand method, * calling addSubCommand once for each SubCommand * to be executed.

* * @see org.puremvc.java.core.Controller Controller * @see org.puremvc.java.patterns.observer.Notification Notification * @see org.puremvc.java.patterns.command.SimpleCommand SimpleCommand */ public class MacroCommand extends Notifier implements ICommand { private Vector> subCommands; /** *

Constructor.

* *

You should not need to define a constructor, * instead, override the initializeMacroCommand * method.

* *

If your subclass does define a constructor, be * sure to call super().

*/ public MacroCommand() { subCommands = new Vector>(); initializeMacroCommand(); } /** *

Initialize the MacroCommand.

* *

In your subclass, override this method to * initialize the MacroCommand's SubCommand * list with ICommand class references like * this:

* *
     * {@code
     * // Initialize MyMacroCommand
     * protected void initializeMacroCommand( )
     * {
     *      addSubCommand( () -> new com.me.myapp.controller.FirstCommand() );
     *      addSubCommand( () -> new com.me.myapp.controller.SecondCommand() );
     *      addSubCommand( () -> new com.me.myapp.controller.ThirdCommand() );
     * }
     * }
     * 
* *

Note that SubCommands may be any ICommand implementor, * MacroCommands or SimpleCommands are both acceptable.

*/ protected void initializeMacroCommand() { } /** *

Add a SubCommand.

* *

The SubCommands will be called in First In/First Out (FIFO) * order.

* * @param commandSupplier a reference to the commandSupplier of the ICommand. */ protected void addSubCommand(Supplier commandSupplier) { subCommands.add(commandSupplier); } /** *

Execute this MacroCommand's SubCommands.

* *

The SubCommands will be called in First In/First Out (FIFO) * order.

* * @param notification the INotification object to be passsed to each SubCommand. */ public void execute(INotification notification) { for(Supplier commandSupplier : subCommands) { ICommand command = commandSupplier.get(); command.execute(notification); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy