Modularity and packages

Splitting software into modules: packages, public interface and hidden details, visibility between packages in Java, packaging by layer or by feature, dependencies without cycles.

  • 25 cards
  • 4 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios

A few cards from the deck

  • Module: what is it?

    Show the answer

    A part of a piece of software that groups related elements, offers others a narrow interface and can be understood, tested and changed separately.

    The goal is for a change to stay local: you edit the inside of a module without touching the others as long as its interface does not move.

  • javac refuses to compile this code and prints the error below. Which fix respects modularity?

    ```java
    package fr.iut.tarif;
    class Remise { static double taux() { return 0.1; } }

    package fr.iut.app;
    import fr.iut.tarif.Remise;

    Main.java:2: error: Remise is not public in fr.iut.tarif;
    cannot be accessed from outside package
    `

    • Expose what Main needs in a public class of the tarif package, and keep Remise hidden
    • Replace the specific import with import fr.iut.tarif.* to import every class of the package
    • Write protected in front of the declaration of the class Remise
    • Copy the class Remise into Main's package
    Show the answer

    Expose what Main needs in a public class of the tarif package, and keep Remise hidden

    A class with no modifier is hidden from other packages, and that is intended. The package decides its public interface; import fr.iut.tarif.* does not change visibility, protected is not allowed on a top-level class, and a copy duplicates code.

  • In Java, a class declared with no modifier is visible only inside its …; only the word … opens it to other packages.

    Show the answer

    In Java, a class declared with no modifier is visible only inside its package; only the word public opens it to other packages.

    That is what produces the error Remise is not public in fr.iut.tarif; cannot be accessed from outside package when another package imports it.

  • Package: what is it?

    Show the answer

    A namespace that holds related classes together; in Java, it matches a folder and is declared at the top of the file.

    package fr.iut.tarif; places the class in fr/iut/tarif/. The package is both a unit of organisation and a visibility boundary.

  • To add a field to Customer, you edit a controller, a service, a repository and a view, kept in four separate packages. What is the cause, and what is the fix?

    • Cyclic dependency: remove one of the four packages and spread its classes over the other three
    • Insufficient hiding: make every class private in its package
    • Interface too small: make every method public
    • Package by layer: gather the classes that change together into a customers package
    Show the answer

    Package by layer: gather the classes that change together into a customers package

    When a business change goes through every package, the slicing follows the technique, not the business. One package per feature brings together what changes together.