Docsity

Strategies for Effective Java Programming: Function Objects and Static Factory Methods, Study notes of Computer Science

Toggle sidebar
bg1
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•
Department of Computer Science
Department of Computer Science
5 - Design: Effective Java - Items 21, 1 & 4
CMPSCI 220 (291A)
Programming Methodology
Fall 2008
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•
Department of Computer Science
Department of Computer Science 2
Highly Skilled Programming
!Proficient with
!Tools
!Methods
!The two are often interrelated
!E.g., design methods and programming
language
!Specifically, OO language features and design
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•
Department of Computer Science
Department of Computer Science 3
OO Programmer’s Toolbox
!Language Basics
!Fundamental language constructs
!Standard usage idioms
!Advanced Language Features
!Sometimes added in later releases or versions
!Customary and Effective Usage
!Guidance from experienced users
!Elements of Style => Cobol with Style
!Effective C++ => Effective Java
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•
Department of Computer Science
Department of Computer Science 4
Effective Java - Item 21
!Use function objects to represent strategies
!In many languages, functions can be passed as parameters
to other functions via function pointers, etc.
!E.g., a sort function might be passed a comparator
!Different comparators yield different sorting behaviors
!An example of the Strategy Pattern: the comparator
function represents a strategy for sorting elements
! Java has no function pointers
!So how to achieve the strategy pattern?
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•
Department of Computer Science
Department of Computer Science 5
Item 21: Function Objects
!Invoking a Java method on an object typically
performs some operation on that object
!Possible to define objects whose methods
operate on other objects passed as parameters
!Instance of a class exporting exactly one such
method is effectively a pointer to that method
!Such instances are known as function objects
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•
Department of Computer Science
Department of Computer Science 6
Example Function Object
class StringLengthComparator {
public int compare(String s1, String s2) {
return s1.length() -s2.length() ;
}
}
•Method orders string by length, not lexicographically
•Reference to StringLengthComparator serves as a
function pointer
•Instance of class is a concrete strategy for string
comparison
pf3
pf4

Partial preview of the text

Download Strategies for Effective Java Programming: Function Objects and Static Factory Methods and more Study notes Computer Science in PDF only on Docsity!

U

UNIVERSITY OFNIVERSITY OF

M

MASSACHUSETTSASSACHUSETTS

, A

, AMHERSTMHERST • •

Department of Computer Science

Department of Computer Science

5 - Design: Effective Java - Items 21, 1 & 4

CMPSCI 220 (291A)

Programming Methodology

Fall 2008

U

UNIVERSITY OFNIVERSITY OF

M

MASSACHUSETTSASSACHUSETTS

, A

, AMHERSTMHERST • •

Department of Computer Science

Department of Computer Science 2

Highly Skilled Programming

! Proficient with

! Tools

! Methods

! The two are often interrelated

! E.g., design methods and programming

language

! Specifically, OO language features and design

UUNIVERSITY OFNIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERSTMHERST • • Department of Computer ScienceDepartment of Computer Science 3

OO Programmer’s Toolbox

! Language Basics

! Fundamental language constructs

! Standard usage idioms

! Advanced Language Features

! Sometimes added in later releases or versions

! Customary and Effective Usage

! Guidance from experienced users

! Elements of Style => Cobol with Style

! Effective C++ => Effective Java

UUNIVERSITY OFNIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERSTMHERST • • Department of Computer ScienceDepartment of Computer Science 4

Effective Java - Item 21

! Use function objects to represent strategies

! In many languages, functions can be passed as parameters

to other functions via function pointers, etc.

! E.g., a sort function might be passed a comparator

! Different comparators yield different sorting behaviors

! An example of the Strategy Pattern: the comparator

function represents a strategy for sorting elements

! Java has no function pointers

! So how to achieve the strategy pattern?

Item 21: Function Objects

! Invoking a Java method on an object typically

performs some operation on that object

! Possible to define objects whose methods

operate on other objects passed as parameters

! Instance of a class exporting exactly one such

method is effectively a pointer to that method

! Such instances are known as function objects

Example Function Object

class StringLengthComparator {

public int compare(String s1, String s2) {

return s1.length() -s2.length() ;

• Method orders string by length, not lexicographically

• Reference to StringLengthComparator serves as a

function pointer

• Instance of class is a concrete strategy for string

comparison

U

UNIVERSITY OFNIVERSITY OF

M

MASSACHUSETTSASSACHUSETTS

, A

, AMHERSTMHERST • •

Department of Computer Science

Department of Computer Science 7

Singleton Function Object

class StringLengthComparator {

private StringLengthComparator() { }

public static final StringLengthComparator

INSTANCE = new StringLengthComparator() ;

public int compare(String s1, String s2) {

return s1.length() -s2.length() ;

• Concrete strategy class is typically stateless

• Hence all instances are functionally equivalent

• Singleton saves unnecessary object creation costs

U

UNIVERSITY OFNIVERSITY OF

M

MASSACHUSETTSASSACHUSETTS

, A

, AMHERSTMHERST • •

Department of Computer Science

Department of Computer Science 8

Passing the Function Object

• What type for the function object parameter?

• StringLengthComparator no good -- allows only one

strategy

• Define a Comparator interface

• Modify StringLengthComparator to implement it

// Strategy interface

public interface Comparator {

public int compare(T t1, T t2) ;

class StringLengthComparator implements Comparator {

// Same class body as before

UUNIVERSITY OFNIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERSTMHERST • • Department of Computer ScienceDepartment of Computer Science 9

Anonymous Strategy Classes?

Arrays.sort(stringArray, new Comparator() {

public int compare(String s1, String s2) {

return s1.length() -s2.length() ;

• Concrete strategy classes are often declared using

anonymous classes

• Each call creates a new instance

• If used often, better to store function object in a private

static final field (with descriptive name) and reuse it

UUNIVERSITY OFNIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERSTMHERST • • Department of Computer ScienceDepartment of Computer Science 10

Hosting Strategy Classes

! Concrete strategy class need not be public to

export a concrete strategy

! A host class can export a public static field

! Or a static factory method (Item 1)

! The strategy interface will be the type

! Concrete strategy class can be a private nested

class of the host

! Unlike anonymous class, allows implementing

additional interfaces

Hosting Strategy Classes

//Exporting a concrete strategy

class Host {

private static class StrLenCmp

implements Comparator, Serializable {

public int compare(String s1, String s2) {

return s1.length() -s2.length() ;

//Returned comparator is serializable

public static final Comparator

STRING_LENGTH_COMPARATOR = new StrLenCmp();

... //Rest of class omitted

Item 21: Summary

! Implementing the Strategy pattern is a primary use

of function pointers

! To implement the Strategy pattern in Java:

! Declare an interface to represent the strategy

! Create a class implementing this interface for each

concrete strategy

! For single use strategies, an anonymous class

! For repeated use strategies, a private static member class

exported as a public static final field whose type is the

strategy interface

U

UNIVERSITY OFNIVERSITY OF

M

MASSACHUSETTSASSACHUSETTS

, A

, AMHERSTMHERST • •

Department of Computer Science

Department of Computer Science 19

Making a Class Uninstantiable

! Providing no constructor doesn’t work

! If no explicit constructors, compiler provides a

public, parameterless default constructor

! Looks just like any other constructor to clients

! Allows for unintentionally instantiable classes

! Making a class abstract doesn’t work

! Class can be subclassed and subclass can be

instantiated

! Also misleads user into thinking class was

designed for inheritance

U

UNIVERSITY OFNIVERSITY OF

M

MASSACHUSETTSASSACHUSETTS

, A

, AMHERSTMHERST • •

Department of Computer Science

Department of Computer Science 20

Making a Class Uninstantiable

! Including a private constructor does work

! A default constructor is generated only if a class

contains no explicit constructors

// Noninstantiable utility class

public class UtilityClass {

// Suppress default constructor for

// noninstantiability

private UtilityClass() {

throw new AssertionError();

UUNIVERSITY OFNIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERSTMHERST • • Department of Computer ScienceDepartment of Computer Science 21

Making a Class Uninstantiable

! Constructor is private, so inaccessible outside

of class

! AssertionError not necessary, but flags

accidental invocation of constuctor from

inside class

! Comment helpful since usage counterintuitive

! Also prevents class from being subclassed

! All constructors must invoke a superclass

constructor

! No accessible superclass constructor