Docsity

Midterm Review for CMPSCI 220 (Spring 2009) - Programming Methodology and Design Patterns, Exams of Computer Science

Partial preview of the text

Download Midterm Review for CMPSCI 220 (Spring 2009) - Programming Methodology and Design Patterns and more Exams Computer Science in PDF only on Docsity!

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Midterm Review

CMPSCI 220 (291A)

Programming Methodology

Spring 2009

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Exam Information

Date and Time:March 09, 2009 From 7 PMto 9 PM

Place: Marston 132

Format: CLOSED BOOK, CLOSEDNOTES

Format: Short answers, Short code

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Description of midterm

z

Fill in interfaces or class definitions to complete a partial pattern

z

Understand and explain use of advanced Java features we have

covered, such as generics and nested / anonymous classes z

Know, and be able to explain the rationale for, the Effective Java

items we have covered

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

OO Designer ToolBox

OO Basics

Abstraction

Encapsulation

Inheritance

Polymorphism

OO Design Principles

OO Design Patterns

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Design Patterns

Strategy:

Defines a family of encapsulated algorithms

Decorator:

Attaches additional responsibilities to an object

dynamically

Observer:

Defines a one-to-many “publish/subscribe”

dependency between objects

Factory Method:

Defines an interface for creating an

object,

lets subclasses decide which classes to instantiate

Abstract Factory Method Pattern:

Provides an interface for

creating families of related or dependent objects

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Design Principles

Encapsulate aspects of your application that vary

Program to interfaces, not an implementations

Favor composition over inheritance

Classes should be open for extension but closed for modification

Strive for loosely coupled designs between objects that interact

Depend upon abstraction, not concrete classes

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

More Design Patterns

Adapter:

Converts the interface of a class into another interface

the clients expect

Facade:

Provides a unified interface to a set of interfaces in a

subsystem

Template Method:

Defines the skeleton of an algorithm in a

method, deferring some steps to subclasses

Iterator:

Provides a way to access the elements of an aggregate

object sequentially

Composite:

Allows composing objects into tree structures to

represent part-whole hierarchies

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Advanced Java Features

Java Generics

„

Allows you to abstract over types

Anonymous classes

„

Inner class that can be declared within the body of a methodwithout naming it.

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Effective Java Items

14: In public classes, use accessor methods,not public fields

16: Favor composition over inheritance

20: Prefer class hierarchies to tagged classes

21:Use function objects to represent strategies

22: Favor static member classes over nonstatic

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Effective Java Items

Ch 5: Generics [23-29]

Ch 6: Enums and Annotations

„

30: Use enums instead of int constants

„

31: Use instance fields instead of ordinals

„

32: Use EnumSet instead of bit fields

„

33: Use EnumMap instead of ordinal indexing

„

34: Emulate extensible enums with interfaces

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

How does observer use this designprinciple?

Identify aspects of application that vary and separate fromwhat stays the same

„

Varies: State of the Subject, number and types of observers

„

Use pattern to vary the objects dependent on the state of thesubject without having to change the state of the subject

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

How does observer use this designprinciple?

Program to an interface not an implementation

„

Subject and Observers use interfaces

„

Subject tracks objects implementing the Observer interface

„

Observers notified by the Subject interface

„

Loose coupling achieved

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Match pattern with intent

Pattern

„

Decorator

„

Adapter

„

Facade

Intent

„

Convert one interface toother

„

Don't alter interface, addresponsibility

„

Make interfaces simpler

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Java generics

Generics abstract over Types

Classes, Interfaces and Methods can be Parameterized by Types

Generics provide increased readability and type safety