Database
Networking
That’s the structure in plain English (I like English). Here’s what it might
look like using all those packaging conventions we just went over:
Note that Amazon’s app would be wayyyyyyyy more complex than this.
Also I don’t work for them,
and I don’t know how they structure their code.
But this is a simplistic example of a “package by feature” approach.
Each feature gets its own big boy package, and anything that isn’t quite a
feature is thrown together somewhere else.
Package by layer
Sure, package by “layer” … or whatever you want to call it. By
functionality,
by purpose, etc.
In the last approach, “package by feature”, we had a package for each main
screen of the app…for each “feature” that we provide to the user.
In this approach, “package by layer”, we group by the
kind of class it is. For
example, is the class part of the user interface?
It goes in the userinterface
(ui) package.
Do you feel me? Are you feeling me?
“package by feature” – group things together by the section of your app or
whatever you would consider a “feature” of your program
“package by layer” – group things together solely by their function, whether
that
be user interface, networking, helper classes, creating files, math, that
kind of stuff.
Let’s say there’s some networking related code I want to add that only
affects the ProductDetailScreen. Where would it go?
In
Package by Feature, it goes in the
productdetail package.
In Package by Layer, it goes into the
networking package.
Imports
“It is clear our nation is reliant upon big foreign oil. More and more of our
imports come from overseas.”
– George W. Bush
This quote has nothing to do with anything.
But that last section took fucking forever and I want to reset our minds here.
Whew. Okay…
How do we let our classes interact with each other? What if the
ProductListScreen doesn’t know what products
to show without first
making a network call to Amazon? Surely it needs to ask Amazon for
recommendations seeing as they know everything about every aspect of our
lives.
Can we just do something like this inside our ProductListScreen?
Networker networker =
new
Networker();
Response networkResponse = networker.getProductsToDisplay();
…
It seems reasonable, right? We know that we have a Networker class in our
project. Why wouldn’t we be able to create an object from that class? Why
can’t we just “instantiate” it willy nilly?
Because Java needs you to “import” it. Unless
the class you want to use is
in the exact same package as yours, you must put an import statement at the
top of your Java file.
Remember package declarations? It’s the simple one-liner where you
specify what package your class belongs to.
Right below that, still at essentially
the top of the file, you simply import all
the classes that you’ll need from outside this file.
Do'stlaringiz bilan baham: