14 Packaging
14 Packaging
1
LEARNING OUTCOMES
• Understand what packages are in Java
2
Our homework 5 package structure
PACKAGES IN JAVA
https://docs.oracle.com/javase/tutorial/java/concepts/package.html
Package
• Contains related classes and interfaces
• The full name of a class includes the package
name: java.util.ArrayList
• The path from your sources root to a given
class — delimited by .
Examples: use_case.signup
use_case.login 3
ACCESS MODIFIERS REVISITED
• Classes can be public or package-private (no explicit modifier)
• Members of a class (methods, variables) can also have private (within
class only) or protected (within package and in subclass outside of
package) modifiers.
• Note that there is no concept of providing access specifically to
“subpackages”.
• Other than for organizational purposes, the packages
use_case.signup and use_case have NO connection.
• For example, if we defined a class in the use_case package, we have
no way of saying that classes in use_case.signup can access it, but
not classes in the app package.
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html 4
EXAMPLE: ONLINE BOOK STORE
• When building an online book store, one of the use
cases we’ve been asked to implement is about
customers being able to view the status of their orders.
This is based on Chapter 34: The Missing Chapter from the CA textbook
5
HOW COULD YOU PACKAGE THIS CODE?
• OrdersController: A web controller, something that
handles requests from the web
6
By Layer By Feature Inside / Outside By Component
7
SCREAMING ARCHITECTURE
https://blog.cleancoder.com/uncle-bob/2011/09/30/Screaming-Architecture.html
The structure of the code should primarily reflect what the program is
doing (what the domain/application is) rather than how it is doing it
(what the frameworks and details of the implementation involve)
9
https://levelup.gitconnected.com/what-is-screaming-architecture-f7c327af9bb2
10
Package by An attempt at
layer of Clean Package by
Architecture component
(screaming)
11
CONCLUSIONS FROM CHAPTER 34 FROM CLEAN
ARCHITECTURE TEXTBOOK
Packages can potentially provide both organization and encapsulation
(using access modifiers)
For the project, your team needs to choose a package structure … and that
takes practice!
• IntelliJ will automatically support you as you move files between packages!
• Choose your access modifiers to reflect your design, exposing only the
necessary methods.
12
ASIDE: PROJECT STRUCTURES
• In IntelliJ, you can create a project which will use one of three build
systems: IntelliJ (default), gradle, or maven.
• Each of these define their own structure for how they organize your files.
• In all cases, you’ll have a Sources Root and a Test Sources Root marked.
• Within these, you will have your usual Java package structure.
• Parallel directory structures in your Sources Root and Test Sources
Root directories will correspond to the same Java packages!
13
ASIDE: PROJECT STRUCTURES
Gradle (https://docs.gradle.org/current/userguide/organizing_gradle_projects.html)
Maven (https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-
layout.html)
Maven Gradle
14