Lesson 12

Software design patterns II

During this lesson we will create an application that contains 2 windows. First window is going to have an impact on the second one in dynamic way.

Singleton

A single object which is accessible everywhere in a project.

It is perceived as anti-pattern.

Practice

Huge companies (Apple, RedHat etc.) often use this anti-pattern. It is hard not to come across it.

Hints
  • Singleton is not going to solve all the problems (code is not going to be written itself).
  • In bigger projects it becomes a place full of low quality code and often the project had to be rewritten from 0.
  • It helps to achieve quickly "it works" effect - liked by most of students.

I. Singleton programming


Note that a field is static and constructor is private(!):


public class SlidesApplication {

    private static SlidesApplication instance;

    public static SlidesApplication getInstance() {
        if (instance == null) {
            instance = new SlidesApplication();
        }
        return instance;
    }

    private SlidesApplication() {
    }

}

Class represents one slide (omitting GUI):


public class Slide {

    private String message;
    private String title;

    public Slide(String message, String title) {
        this.message = message;
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
                    

II. Skeleton of the application


Task 0

Let's modify main class of the application:


public class SlidesApplication {

    private static SlidesApplication instance;

    public static SlidesApplication getInstance() {
        if (instance == null) {
            instance = new SlidesApplication();
        }
        return instance;
    }

    private SlidesApplication() {
        stageMap = new HashMap<>();
        slideData = new ArrayList<>();
        currentSlide = 0;
        initSlidesData();
    }

    private Map<String, Stage> stageMap;
    private int currentSlide;
    private List<Slide> slideData;

    public void registerStage(String name, Stage stage) {
        stageMap.put(name, stage);
    }

    public void loadScene(String stageName, Scene scene) {
        Stage stage = stageMap.get(stageName);
        stage.setScene(scene);
    }

    private void initSlidesData() {
        slideData.add(new Slide("Hint for a speaker 0", "Slide 0"));
        slideData.add(new Slide("Hint for a speaker 1", "Slide 1"));
        slideData.add(new Slide("Hint for a speaker 2", "Slide 2"));
    }

    public Scene renderScene() {
        Slide current = slideData.get(currentSlide);
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/slide.fxml"));

        SlideController controller = new SlideController(current);
        loader.setController(controller);
        try {
            Pane pane = loader.load();
            return new Scene(pane);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void createSlideStage() {
        Stage stage = new Stage();
        stage.setTitle("SlideStage");
        stage.setScene(renderScene());
        stage.show();
        registerStage("SlideStage", stage);
    }

    // TODO Change index of the current slide
    // TODO Return a scene with current slide view
    public Scene turnLeft() {
        return null;
    }

    // TODO Same like turnLeft(), but in different direction
    public Scene turnRight() {
        return null;
    }

    // TODO We have to return an information about a hint for a speaker
    public String currentMessage() {
        return null;
    }
}
Task 1

Analyze the code from SlidesApplication class, next add the missing code.

This time there is too much code to copy. Below you will find a link to download src catalog for your project. There are to pairs of files (.java + .fxml) for two windows.

Download source code
Task 2

Make sure that there are no exceptions thrown during testing an application.

Task 3

Modify a project:

  • Let a slide contain more information (not only a title but also additional block of text)
  • Let a slide contain its own image (as its background)
  • Format the text so it will be half transparent (css opacity)