Docsity

CMPSCI 220 Midterm Review at UMass Amherst - Spring 2009, Exams of Computer Science

Toggle sidebar
bg1
1
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science
Midterm Review
CMPSCI 220 (291A)
Programming Methodology
Spring 2009
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 2
Exam Information
Date and Time:March 09, 2009 From 7 PM
to 9 PM
Place: Marston 132
Format: CLOSED BOOK, CLOSED
NOTES
Format: Short answers, Short code
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 3
Description of midterm
zMatch pattern names with pattern descriptions
zState and describe some design principles and relate them to design
patterns
zGiven a description of a situation, indicate which pattern to use and
why
zGiven a description of a code configuration, or an actual code
fragment, indicate the pattern it is implementing and why that is
appropriate
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 4
Description of midterm
zFill in interfaces or class definitions to complete a partial pattern
zUnderstand and explain use of advanced Java features we have
covered, such as generics and nested / anonymous classes
zKnow, and be able to explain the rationale for, the Effective Java
items we have covered
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 5
OO Designer ToolBox
OO Basics
Abstraction
Encapsulation
Inheritance
Polymorphism
OO Design Principles
OO Design Patterns
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 6
What is a pattern?
A Pattern is a:
Solution -- a general design that anyone can apply
resolving the problem
Problem -- goal to be achieved, plus any constraints that
occur in the context
Context -- situation in which pattern applies; should be a
recurring situation
pf3
pf4

Partial preview of the text

Download CMPSCI 220 Midterm Review at UMass Amherst - Spring 2009 and more Exams Computer Science in PDF only on Docsity!

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science

Midterm Review

CMPSCI 220 (291A) Programming Methodology Spring 2009 U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 2 Exam Information „ Date and Time:March 09, 2009 From 7 PM to 9 PM „ Place: Marston 132 „ Format: CLOSED BOOK, CLOSED NOTES „ Format: Short answers, Short code UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 3 Description of midterm

zMatch pattern names with pattern descriptions

zState and describe some design principles and relate them to design

patterns

zGiven a description of a situation, indicate which pattern to use and

why

zGiven a description of a code configuration, or an actual code

fragment, indicate the pattern it is implementing and why that is

appropriate

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 4 Description of midterm

zFill in interfaces or class definitions to complete a partial pattern

zUnderstand and explain use of advanced Java features we have

covered, such as generics and nested / anonymous classes

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

items we have covered

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 5 OO Designer ToolBox

„ OO Basics

„ Abstraction

„ Encapsulation

„ Inheritance

„ Polymorphism

„ OO Design Principles

„ OO Design Patterns

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 6 What is a pattern?

„ A Pattern is a:

„ Solution -- a general design that anyone can apply

resolving the problem

„ Problem -- goal to be achieved, plus any constraints that

occur in the context

„ Context -- situation in which pattern applies; should be a

recurring situation

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 7 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 MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 8 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

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 9 More Design Principles

„ Talk only with your immediate friends

„ Don’t call us, we’ll call you

„ Allow low-level components to hook into a system

„ But high-level components determine when and how they are

needed

„ A class should have only one reason to change

„ More generally: Strive for high cohesion

„ Module or class designed around a set of related functions has

high cohesion

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 10 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

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 11 Advanced Java Features

„ Java Generics

„ Allows you to abstract over types

„ Anonymous classes

„ Inner class that can be declared within the body of a method

without naming it.

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 12 Effective Java Items

„ 1: Consider static factory methods instead of constructors

„ 2: Consider a builder when faced with many constructor parameters

„ 3: Enforce the singleton property with a private constructor or

enum type

„ 4: Enforce noninstantiability with a private constructor

„ 5: Avoid creating unnecessary objects

„ 6: Eliminate obsolete object references

„ 7: Avoid finalizers

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 19 Match pattern with intent

„ Pattern

„ Decorator

„ Adapter

„ Facade

„ Intent

„ Convert one interface to

other

„ Don't alter interface, add

responsibility

„ Make interfaces simpler

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 20 Java generics

„ Generics abstract over Types

„ Classes, Interfaces and Methods can be Parameterized by Types

„ Generics provide increased readability and type safety

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 21 Java 1.0 vs Generics

class Stack {

void push(Object o) { ... }

Object pop() { ... }

String s = "Hello";

Stack st = new Stack();

st.push(s);

s = (String) st.pop();

class Stack {

void push(A a) { ... }

A pop() { ... }

String s = "Hello";

Stack st =

new Stack();

st.push(s);

s = st.pop();

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 22 Homeworks

„ 1: Decorator and strategy patterns

„ 2: Observer pattern and anonymous classes