HowTos - Application Architecture: Shop

Implement a Shop

Description:
The Shop is the central class of a SalesPoint application. You can run Processes on the Shop itself, you can add SalesPoints where you can run Processes too. The Shop provides a central MenuSheet, where important actions could be invoked (save, load, quit etc.). You may wish to change this MenuSheet in order to open SalesPoints or to offer additional user interaction.
All sorts of central data should be stored here, like Catalogs, Stocks, Currencys, MoneyBags, UserManagers and other attributes that are of global use.
It is important to make a singleton instance of Shop and declare it with the static Shop.setTheShop() method. If you need the Shop instance, call Shop.getTheShop(). Don´t try to use the instance, you can get in your Shops methods, because very unlovely runtime errors may be the result.

Used classes:

ToDo:

  1. Create a subclass of Shop.
  2. Add constructor to create singleton instance of Shop. You can initialize some data in the constructor, like setting the Shop's frame size, but that is also possible in the main class (see below).
  3. For invoking the Shop, create a simple class with the public static void main (String[] noArgs) method.
  4. Therein create an instance of the Shop and initialize its attribute of the singleton Shop instance by the setTheShop(Shop TutorialShop) method. Furthermore call the start() method to start the Shop.
  5. The main class is also used to add a SalesPoint (or even more) to the Shop here.

Example Source Code:

Shop class:

// necessary imports
import java.awt.Rectangle;

import sale.AutoTimer;
import sale.CalendarTime;
import sale.Shop;
import sale.events.TimerEvent;
import sale.events.TimerListener;
1
public class ArchitectureShop extends Shop
{
    2
    public ArchitectureShop()
    {
        super();
        setShopFrameBounds(new Rectangle(0, 0, 640, 480));

        CalendarTime calendarTime = new CalendarTime();
        calendarTime.setTimeToCount(CalendarTime.SECOND);
        AutoTimer autoTimer = new AutoTimer(calendarTime, (long) 992);
        autoTimer.addTimerListener(new TimerListener()
                {
                    public void onGoneAhead(TimerEvent timerEvent)
                    {
                        System.out.println(timerEvent.getTime());
                    }

                    public void onTimeSet(TimerEvent timerEvent)
                    {

                    }

                    public void onIntervalSet(TimerEvent timerEvent)
                    {

                    }
                });
        autoTimer.start();
    }
}
		

Main class:

// necessary imports
import sale.Shop;
import application_architecture.ArchitectureSalesPoint;
import application_architecture.ArchitectureShop;
import display.DisplaySalesPoint;
3
public class Tutorial
{

    public static void main(String[] args)
    {
	4
        ArchitectureShop myTutorialShop = new ArchitectureShop();
        Shop.setTheShop(myTutorialShop);
        myTutorialShop.start();
	5
        myTutorialShop.addSalesPoint(new ArchitectureSalesPoint("ArchitectureSalesPoint"));
        myTutorialShop.addSalesPoint(new DisplaySalesPoint("DisplaySalesPoint"));
    }

}
		

Back to:


Change quit behaviour

Description:
If the Shop is going to be shutted down, it will ask to make it's state persistant.
Sometimes this feature makes no sense, therefore it can be modified.

Used classes:

Related topics:

ToDo:

  1. Open the designated Shop class.
  2. Implement the public void quit() method to change the quit behaviour.

Example Source Code:

1
public class ArchitectureShop extends Shop
{
    .
    .
    .
    2
    public void quit()
    {
        if(Shop.getTheShop().shutdown(false))
        {
            System.exit(0);
        }
    }
    .
    .
    .
}
		

Back to:


Change shutdown conditions of a Shop

Description:
Sometimes it may be useful to alter the shutdown conditions of the shop without changing the shutdown routine itself. That way, for example, you can impose additional shutdown conditions.

Used classes:

Related topics:

ToDo:
This example describes how to correctly impose addtional shutdown conditions.

  1. Open the designated Shop class.
  2. Override the protected boolean canShutdown(boolean fPersistify) method to change the quit behaviour.
    Note:
    Shop.canShutDown will suspend the Shop and all processes running in it before testing any conditions. If any of the standard conditions does not hold (i.e. if there are background processes unwilling to be stopped or any SalesPoints that disagree), the Shop will be resumed again before canShutDown returns. Therefore, if the Shop was running before the method was called and your own conditions did not hold, you must explicitly resume the Shop again, before returning from the method.

Example Source Code:

1
public class ArchitectureShop extends Shop
{
    .
    .
    .
    2
    protected boolean canShutdown(boolean fPersistify) {
        int oldState = getShopState();
        if (!super.canShutdown(fPersistify)) {
            return false;
        }
        if (!checkAdditionalShutdownConditions()) {
            if (oldState == RUNNING) {
                resume();
            }
            return false;
        }
        return true;
    }
    .
    .
    .
}
		

Back to:


React on adding and removing SalesPoints

Description:
This is required whenever reaction on adding or removing SalesPoints is needed, such as manipulating menus or notifying SalesPoints already opened.

Used classes:

Related topics:

ToDo:

  1. Open the designated Shop class.
  2. Override the protected void onSalesPointAdded() and/or protected void onSalesPointRemoved() methods to react on the adding or removing.
    Be sure to synchronize your method with the one of the original Shop by first calling it's super.onSalesPoint....

Example Source Code:


1
public class ArchitectureShop extends Shop
{
    .
    .
    .
    2
    protected void onSalesPointAdded(SalesPoint sp) {
        super.onSalesPointAdded(sp);
        . . . react on adding a SalesPoint . . .
    }

    protected void onSalesPointRemoved(SalesPoint sp) {
        super.onSalesPointRemoved(sp);
        . . . react on removing a SalesPoint . . .
    }
			
}
		

Back to:


previous OverviewApplication Architecture: SalesPoint next



by Thomas Ryssel