Packages helps us to organise files within project and resolving naming conflicts when different packages have classes with the same name.
There are a number of principles that can help to design packages:
REP (Reuse-Release Equivalence Principle) - Classes under a package must belong to a cohesive group and should be reused and released together. For example: if a class depends on another class in a different package, it will depends on the entire package. The basic idea of this principle is that packages must be separately released, versioned, and tracked to ensure the reusability of the code.CRP (Common Reuse Principle) - Only cohesive classes should be packaged together. We should keep in mind that classes in a package are going to be used together. For example if you are creating a package for enabling test reporting, all the classes under that package should be used together.
CCP (Common Closure Principle) - Packages should be looked at from change and distribution point of view. Classes that are not tightly coupled to each other should not be kept in the same package. Example: We create separate packages for application logic layer (page objects) and test/business logic layer so that we maintain those well when we encounter changes in a specific layer.
ADP (Acyclic Dependency Principle) - If a package A depends on package B and package B depends on package A, this will create problems when we need to release a package that is modified. Dependency cycles makes code hard to understand and reduces maintainability. This can be solve using dependency inversion principle (DIP) or creating a new package that two packages can depend on instead of one depending on the other.
SDP (Stable Dependencies Principle) - The package's stability is measured in terms of number of classes inside a package that depends on number of classes outside the package and number of classes outside the package that depends on classes within the package. A package is called stable if lots of classes outside the package depends on it. A given package should depend only on more stable packages. Whenever a package changes, all packages that depend on it must be validated to ensure they work as expected after the change.