of applications for all sorts of domains ranging from mobile phone to enterprise applications.
Most of these endeavors require, or could at least benefit from, modularity, so Java's lack of
explicit support does cause some amount of pain for developers. From this point of view, we
do feel the following criticisms are valid.
LOW-LEVEL CODE VISIBILITY CONTROL
While Java provides a fair complement of access modifiers to control visibility (e.g., public,
protected, private, and package private), these tend to address low-level object-
oriented encapsulation and do not really address logical system partitioning. Java has the
notion of a package, which is typically used for partitioning code. For code to be visible from
one Java package to another, the code must be declared public (or protected if using
inheritance). Sometimes the logical structure of your application calls for specific code to
belong in different packages, but then this means any dependencies among the packages
must be exposed as public, which makes it accessible to everyone else too. Often this can
expose implementation details, which makes future evolution more difficult since users may
end up with dependencies on your non-public API.
To illustrate this, let's consider a trivial hello world application that provides a public
interface in one package, a private implementation in another and a main class in yet
another.
Listing 1.1.1 Trivial example of the limitations of Java's object-orientated encapsulation
package org.foo.hello;
public interface Greeting { #1
void sayHello();
}
package org.foo.hello.impl;
import org.foo.hello.Greeting;
public class GreetingImpl implements Greeting {
final String m_name;
public GreetingImpl(String name) { #2
m_name = name;
}
public void sayHello() {
System.out.println("Hello, " + m_name + "!");
}
}
package org.foo.hello.main;
import org.foo.hello.Greeting;
import org.foo.hello.impl.GreetingImpl;
©Manning Publications Co. Please post comments or corrections to the Author Online forum:
http://www.manning-sandbox.com/forum.jspa?forumID=507
评论3
最新资源