Lib Tech

download Lib Tech

of 57

Transcript of Lib Tech

  • 8/13/2019 Lib Tech

    1/57

  • 8/13/2019 Lib Tech

    2/57

    1.01.0LibrariesLibraries

    TechnicalTechnicalOverviewOverview

    Jonni KanervaJonni KanervaFrank YellinFrank Yellin

    JavaSoftJavaSoft

  • 8/13/2019 Lib Tech

    3/57

    Outline

    Introduction Functionality in the 1.0 Libraries

    Distributed design of the Java platform Instructive oddities

    Design patterns Extensibility

    Wrapup

  • 8/13/2019 Lib Tech

    4/57

    Approach

    Yes: explore the universe General principles and overview

    Instructive oddballs and treats

    Larger-scale patterns

    No: even-handed skim survey

  • 8/13/2019 Lib Tech

    5/57

    Background Reading

    The Java Application ProgrammingInterface, Vols. 1 & 2

    James Gosling, Frank Yellin, The Java TeamAddison-Wesley, 1996

  • 8/13/2019 Lib Tech

    6/57

    Central Themes

    Functionality What can you do with 1.0 java.*?

    Design

    The pieces

    How they fit together and cooperate

    Extensibility

    java.* provides a simple, ubiquitous,

    extensible base for your programs.

  • 8/13/2019 Lib Tech

    7/57

    API Slogans

    Simplicity is power,accuracy is leverage

    Provide partial designs, ready to be

    woven together

    The simple should be simple,

    the complex should be possible

  • 8/13/2019 Lib Tech

    8/57

    API Goals

    Function Overall scope is good

    Important problems are solvable

    Partition

    Divide and conquer complexity

    Pieces are understandable

    Pieces are changeable

  • 8/13/2019 Lib Tech

    9/57

    API Goals (cont.)

    Names Simple, direct, systematic

    Reduce chances for misunderstanding

    Extensible

    Easy to specialize

    Easy to connect

    Platform independent

    Implementable

  • 8/13/2019 Lib Tech

    10/57

    Good API = ???

    Learnable Easy to learn, easy to remember

    Usable

    Easy to code to, easy to build tools on

    Flexible

    Allow different solutions and solution styles

  • 8/13/2019 Lib Tech

    11/57

    Next

    Introduction Functionality in the 1.0 Libraries

    Distributed design of the Java platform Instructive oddities

    Design patterns Extensibility

    Wrapup

  • 8/13/2019 Lib Tech

    12/57

    Functionality

    in the 1.0 Libraries

    Javacompatible library = Java package Classes

    Interfaces

    Subpackages

    Functionality spread through 8 packages

  • 8/13/2019 Lib Tech

    13/57

    Packages in the 1.0 API

    java.lang: core language support java.io: input/output streams, data types

    java.net: networking support

    java.util: HashTable, StringTokenizer

    java.awt: cross-platform window toolkit

    java.awt.peer: interfaces to native GUI

    java.awt.image: image processing

    java.applet: applets and applet contexts

  • 8/13/2019 Lib Tech

    14/57

    21 classes, 2 interfaces, 40 excep/errors Class wrappers for primitive data types:

    Boolean, Integer, Double

    Classes for core language concepts:

    String, Thread, Object

    Access to system resources:

    Process, Runtime, System,

    Imported by every Java-powered program

    Package java.lang

  • 8/13/2019 Lib Tech

    15/57

    Package java.io

    23 classes, 3 interfaces, 5 sections Byte-oriented stream abstraction for

    input and output

    Mix and match filtering

    Cross-platform file abstraction

    Stream tokenizer

  • 8/13/2019 Lib Tech

    16/57

    Package java.net

    11 classes, 3 interfaces, 5 exceptions URLs

    URL connections Sockets

    Internet addresses

  • 8/13/2019 Lib Tech

    17/57

    Package java.util

    10 classes, 2 interfaces, 2 exceptions Generic utilities:

    Vector, HashTable, Stack, Enumeration,

    BitSet, StringTokenizer

    Date

  • 8/13/2019 Lib Tech

    18/57

    Package java.awt

    42 classes, 2 interfaces, 1 exception, 1 error GUI elements:

    Button, TextField, Window, Menu,

    Event handling

    Fonts and font metrics

    Graphics

    Colors

  • 8/13/2019 Lib Tech

    19/57

    Package java.awt.peer

    0 classes, 22 interfaces Interfaces for communicating with

    native GUI elements:

    ButtonPeer, TextFieldPeer,

    Decouples AWT classes from platform-

    specific toolkit implementations

  • 8/13/2019 Lib Tech

    20/57

    Package java.awt.image

    9 classes, 3 interfaces Image creation

    Image filters Color mapping

  • 8/13/2019 Lib Tech

    21/57

    Package java.applet

    1 class, 3 interfaces The Applet class

    Applet audio and images Applet context:

    Applet-browser relationship

    Inter-applet communication

  • 8/13/2019 Lib Tech

    22/57

    Distributed Design of

    the Java Platform

    Java platform = runtime + language + classes

    java.* standard API

    On all Java-compatible platforms

    Write once, compile once, run everywhere

  • 8/13/2019 Lib Tech

    23/57

    Classes Complement the

    Language and Runtime

    java.* classes are integrated with corelanguage mechanisms:

    Primitive data types

    Operators

    Class and interface membership

    Control flow Runtime and environment

  • 8/13/2019 Lib Tech

    24/57

    Primitive Data Types

    and java.* Classes

    boolean: Boolean char: Character

    int, long: Integer, Long

    float, double: Float, Double

    Double d = new Double(3.14159);

    double simplePi = d.doubleValue();

  • 8/13/2019 Lib Tech

    25/57

    Operators and

    java.* Classes

    Language: + Classes: String, StringBuffer

    String s = a + b;String s = new

    StringBuffer().append(a).append(b).toString();

  • 8/13/2019 Lib Tech

    26/57

    Class/Interface Types

    and java.* Classes

    Language: class, interface, implements, extends, instanceof

    Classes: Class, Object

    interface Fooable { public void fooIt(); }

    class Foo implements Fooable { public void fooIt() {} }

    Fooable obj = new Foo();obj.getClass() ==> class Foo

    obj.getClass().getSuperclass().isInterface() ==> false

    Class.forName("Fooable").isInterface() ==> true

    M l i h di d

  • 8/13/2019 Lib Tech

    27/57

    Multithreading and

    java.* classes

    Language: synchronized (methods and blocks)

    Classes:

    Thread, ThreadGroup,

    Object: waitand notifymethods

    Thread thread1, thread2; Runnable r;

    thread1 = new Thread(r);

    thread2 = new Thread(r);

    thread1.start(); thread2.start();

    C l Fl d

  • 8/13/2019 Lib Tech

    28/57

    Control Flow and

    java.* Classes

    Language: try, catch, finally, throws

    Classes:

    Throwable, Error, Exception

    try {

    sleep(500);} catch (InterruptedException e) {

    System.out.println(e = + e);

    }

    R ti /E i t

  • 8/13/2019 Lib Tech

    29/57

    Runtime/Environment

    and java.* Classes

    Runtime/Environment: Object allocation, security, garbage

    collection

    Classes:

    Class, ClassLoader, Object

    Runtime, System, SecurityManager

    String s1 = (String)Hello.getClass().newInstance();

    String s2 = System.getProperty(java.version);

  • 8/13/2019 Lib Tech

    30/57

    Recap

    Integrated Java-compatible platformincludes java.* classes

    Standard on all Java-compatible systems

    java.lang.* is the core of the core

    Most tightly integrated

    Imported automatically

  • 8/13/2019 Lib Tech

    31/57

    Instructive Oddities

    Typical class in java.*: Public, concrete, subclassable

    Has instance methods, instance variables

    Variety is the spice of life: A classy class

    A very protected class A very abstract class

    An abstract but not abstract class

    A half class, half language primitive

    j l g M th

  • 8/13/2019 Lib Tech

    32/57

    java.lang.Math

    A Classy Class

    Two numerical constants: Math.E, Math.PI

    Range of standard math functions

    All as class methods:public static double tan(double a)

    Declared as final no subclasses No constructor

    No instances

    java lang ClassLoader

  • 8/13/2019 Lib Tech

    33/57

    java.lang.ClassLoader

    A Very Protected Class

    Key class for security: Methods and constructor accessible only from

    your own subclass

    Only one method can/must be overridden

    protected ClassLoader()

    protected final void resolveClass(Class c)protected final Class findSystemClass (String name)

    protected final Class defineClass(byte[] data, int offset, int length)

    protected abstract Class loadClass (String name, boolean resolve)

    java lang Number

  • 8/13/2019 Lib Tech

    34/57

    java.lang.Number

    A Very Abstract Class

    Abstract superclass for number objects: Integer, Long, Float, Double

    public abstract class Number { public abstract int intValue();

    public abstract long longValue();

    public abstract float floatValue();

    public abstract double doubleValue();}

    java awt Component

  • 8/13/2019 Lib Tech

    35/57

    java.awt.Component

    Abstract or Not?

    Contains no abstract methods Declared as an abstract class

    Cannot be instantiated

  • 8/13/2019 Lib Tech

    36/57

    Array Half Class

    Not in any package One final instance variable: length

    Cannot be extended (subclassed)

    Superclass is Object

    Inherits methods from Object

    (new int[5]).getClass().getSuperclass() ==> java.lang.Object

  • 8/13/2019 Lib Tech

    37/57

    Design Patterns

    Weaving partial designs together Interactions of classes, interfaces, and

    instances

    Design units within larger picture

    Tool for understanding

  • 8/13/2019 Lib Tech

    38/57

    Recommended Reading

    Design Patterns: Elements of ReusableObject-Oriented Software

    Gamma, Helm, Johnson, VlissidesAddison-Wesley, 1995

  • 8/13/2019 Lib Tech

    39/57

    Decorator The Pattern

    Extend functionality of an object Not statically through subclassing

    By wrapping it in another object, a decorator

    Decorators interface is superset of

    decoratees

    Decorator forwards some requests todecoratee

    Decorator Example:

  • 8/13/2019 Lib Tech

    40/57

    Decorator Example:

    java.io Input And Output

    Byte-oriented stream input and output Base classes are InputStream, OutputStream

    Mix and match filtering:

    Filter, Buffered, Data, LineNumber...

    FilterInputStream.read():

    Invokes read() on the decoratee InputStream:

    return in.read();

    Buffered Numbered

  • 8/13/2019 Lib Tech

    41/57

    Buffered, Numbered,

    Line Input from a File

    FileInputStream in1 = new FileInputStream(data.txt); BufferedInputStream in2 = new BufferedInputStream(in1);

    LineNumberInputStream in3 = new LineNumberInputStream(in2);

    DataInputStream in4 = new DataInputStream(in3);

    String line; while ((line = in4.readLine()) != null) {

    System.out.println(in3.getLineNumber() + : + line);

    }

    in4.close();

    data.txt readLine()File Buffer Line Data

  • 8/13/2019 Lib Tech

    42/57

    Composite The Pattern

    Compose objects into tree structures to represent

    part-whole hierarchies. Composite lets clients treatindividual objects and compositions of objectsuniformly. (p. 163)

    Composite Example: java awt

  • 8/13/2019 Lib Tech

    43/57

    Composite Example: java.awt

    Component and Container

    Abstract superclass: Component

    Presence on screen, size, location

    Receive, handle, and deliver events

    Most AWT GUI elements inherit fromComponent

    Container is subclass of Component

    Contains a group of components (children)

    Can create arbitrarily deep containment

    hierarchy

  • 8/13/2019 Lib Tech

    44/57

    Component-Container Trees

    Frame

    Label

    Panel

    Button Button

    BorderLayout

    FlowLayout

    Frame

    LabelPanel

    Button Button

  • 8/13/2019 Lib Tech

    45/57

    Strategy The Pattern

    Define a family of algorithms, encapsulate each

    one, and make them interchangeable. Strategy letsthe algorithm vary independently from the clientsthat use it. (p.315)

    Strategy Example:

  • 8/13/2019 Lib Tech

    46/57

    Strategy Example:

    java.awt.LayoutManager

    How to place components in a container

    Dynamic constraint-based layout

    Interface with 5 methods:

    Add, remove, layout, minimum,

    preferred

    java.awt package provides: BorderLayout, CardLayout, FlowLayout,

    GridLayout, GridBagLayout

  • 8/13/2019 Lib Tech

    47/57

    Flow and Border Layouts

  • 8/13/2019 Lib Tech

    48/57

    Bridge The Pattern

    Decouple an abstraction from its implementation

    so that the two can vary independently. (p. 151)

    Bridge Example: java.awt

  • 8/13/2019 Lib Tech

    49/57

    g p j

    Components and Peers

    MFrame

    MLabelMPanel

    MButton MButton

    Frame

    LabelPanel

    Button Button

    Java AWT Motif Peers

    Chain of Responsibility

  • 8/13/2019 Lib Tech

    50/57

    p y

    The Pattern

    Avoid coupling the sender of a request to its

    receiver by giving more than one object a chance tohandle the request. Chain the receiving objects andpass the request along the chain until an object

    handles it. (p. 223)

    Chain of Responsibility

  • 8/13/2019 Lib Tech

    51/57

    p y

    Example: AWT Events

    EventEvent

    Frame

    LabelPanel

    Button Button

  • 8/13/2019 Lib Tech

    52/57

    Extensibility

    Extensibility pervades java.*

    Subclass a concrete class

    Subclass an abstract class, prespecified holes

    Implement an interface

  • 8/13/2019 Lib Tech

    53/57

    Extend a Concrete Class

    Six favorite concrete classes to extend

    java.applet.Applet: custom applets

    java.awt.Canvas: custom GUI components

    java.awt.Panel: custom GUI containers

    java.awt.Frame: custom top-level windows

    java.lang.Thread: custom execution thread ???

  • 8/13/2019 Lib Tech

    54/57

    Extend an Abstract Class

    extend java.io.InputStream

    Implement: read()

    extend java.lang.ClassLoader

    Implement: loadClass(String, boolean)

    extend java.awt.Graphics

    29 abstract methods to implement

    Example: write your own PSGraphics class

  • 8/13/2019 Lib Tech

    55/57

    Implement an Interface

    As part of the classs duties

    java.lang.Runnable in an Applet subclass

    As all of the classs duties

    java.awt.LayoutManager

  • 8/13/2019 Lib Tech

    56/57

    Upcoming Extensions

    Security: digital certificates, authentication

    Multi-media: 2-D, 3-D, video, audio

    JDBC: database access and connectivity

    Remote Objects: remote method invocation

    Persistent Objects

    Electronic Commerce

  • 8/13/2019 Lib Tech

    57/57

    Wrapup 1.0 java.*

    Simple, ubiquitous, extensible base foryour programs:

    Integrated with language

    Available on all Java-compatible platforms

    Goals and progress:

    Learnable, usable, flexible, platformindependent, implementable