Android project package structure

烈酒焚心 提交于 2019-12-04 11:22:01
David Snabel-Caunt

Wikipedia has useful notes on Java packages. Packages are mainly useful for two reasons:

  1. A package provides a unique namespace for the types it contains.
  2. Classes in the same package can access each other's package-access members.

The first point means that you can group items by logical functionality. Activities could reside under an activity package, and your services under a service package.

The second point is quite important and often overlooked. Package access allows you to do some clever things. For example, you can have a 'builder' class which can build and populate models which have package access properties, without adding lots of setter methods or using public properties. This can make object creation really simple and intuitive, while objects remain immutable outside the package.

A really good example of this principle can be found in Romain Guy's Shelves app. The BookStore class can create Book objects and modify their members, without exposing these fields to other classes (in other packages).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!