View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Ryan Cino

Team Name

CoWeb Assignment 3

Design Patterns (2 Points)

A) Besides its name, name three characteristics commonly used to describe design patterns. For each characteristic, describe what it is and why it is useful?

3 Characteristics:

1) Problem: This characterisitic is used for knowing when to apply the pattern. This is the initial step in using a design pattern. The user must evaluate
what the problem is at hand and then choose the correct pattern to solve that problem.

2) Solution: This characteristic defines the elements that make up the design, their relationships, responsibilities, and collaborators. This is the characteristic
which tells the user how to apply the design pattern at hand.

3) Consequences: This characteristic defines the results and tradeoffs of a pattern. Every pattern is going to come with some consequences. It is very useful as
a user to know what to expect, and handle these consequences appropriately. This characteristic, along with mainly the problem characteristic, may
also help user decide what design pattern to implement.

B) Observer, Adaptor, and Factory are three design patterns described in this class. For two of these three, briefly describe them using the characteristics you chose.

1) Observer Pattern

a) Problem: Use this pattern when

i) a change to an object requires updating many objects
ii) When an object should be able to notify other objects without making assumptions about who those objects are (loosely coupled)
iii) One aspect is dependent on another, but you want to vary and reuse independently.

b) Solution: Elements in pattern include

i) Subject: adds dependents, removes dependents, notifies observers of change
ii) ConcreteSubject: Implements Subject Interface. Keeps track of subject state.

iii) Observer: dependent on subject. Receives update messages.
iv) ConcreteObserver: Implements Observer Interface. Keeps track of observer state, and also receives update messages.

Ex: MVC Uses this pattern, where the Model is the Subject, Specific Model is a ConcreteSubject, UI Component is an Observer, and a Specific UI Component is the ConcreteObserver.

c) Consequences

i) Abstract coupling between Subject and Observer. Becomes issue in large, multi-layered systems.
ii) Requires support for broadcast communication. This allows freedom to add/remove observers anytime. This can be computationally expensive.
iii) Unexpected updates. Observers and Subjects are blind to the costs of sending each other messages.

2) Factory Pattern

a) Problem: Use this pattern when

i) Defining an interface for creating objects, but let subclasses decide which class to instantiate.
ii) Class can't anticipate the class of objects it must create
iii) A class wants its subclasses to specify object to create
iv) Want to localize knowledge of which specific class gets created for a specific purpose.

Ex. Application Suite that can create Word Processing, Spreadsheets, or Picture Documents.

b) Solution: Elements in pattern include

i) Creator: Define the factory method.
ii) ConcreteCreator: Implements Creator Interface. Overrides factory method to create instance of ConcreteProduct.

iii) Product: defines the interface of objects that factory method creates (ex: all documents can open, close, save, etc.)
iv) ConcreteProduct: implements Product Interface. Keeps track of subject state (ex: a Word Processing document).

c) Consequences

i) Have to have ConcreteCreator for every Product that is created
ii) Connects parallel class hierarchies, which makes class evoluation and devolopment a little more complex.

Object-Oriented Language Design (2 Points)

1) Most OO languages run on a virtual machine. Java and Smalltalk do. C++ does not. What are the advantages and disadvantages of using a virtual machine for OO programming?

Virtual Machine: Runtime Environement.
i) Translates between byte code and machine code
ii) Uses Dynamic Dispatch
iii) Automatic Memory Management (Garbage Collection)

Flexibility, (Safer due to Memory Management)

No Virtual Machine:
Advantages: Faster

The inheritance in Java and Smalltalk is class-based. The inheritance in Javascript and Self is prototype-based. What are the advantages and disadvantages of class-based and prototype-based inheritance?

Class-Based Inhertiance:
i) Objects are instance of an abstract class hierarchy.

Advantages: Scalability, Robustness

Prototyp-Based Inheritance:
i)Objects inherit properties from other objects

Advantages: Simplicity, Faster

CoWeb Assignment 2

Using the Debugger

There are 2 easy ways to get the Debugger tool to pop up to debug.

1) If an error occurs in your code, in which you'll need to debug it anyways.
2) You place a self halt. call at any point in your code.

By doing #2, as soon as the line of code gets reached, the image below will pop up.

External Image

This image will also pop up if 1) occurs. However, it will have the error listed on top instead of Halt.
It will also have the stack trace of code that led to the error.

To continue debugging the code using the Debugger, the user simply has to push debug, or click on the line of code
where they would like to being debugging. Doing this pops up the image below.

External Image

In the bottom left corner are the instance variable for the object being debugged at that time. Since most of the time code
will completely halt when the debugger pops up , these instance variables will almost always be the value they were when the halt was called
or when the error occured. In the bottom right corner, are all the temporary variables to the method being debugged. The user can simply
go to any of these variables and explore or inspect them, and determine any and all values of these variables.

Also, the user may select a block of code and inspect or explore that block of code. This can prove to be very helpful, since many times an
error occurs due to a block of code executing differently than expected.

When using the debugger, the user has many options on how to trace through the code.

The user can click on any of the lines in the list on top and go to that method.
The user has the option to proceed, which will simply continue running through the code.
The user can restart, which will just restart the entire process.
If the user clicks into, the Debugger will go into the method which is highlighted, and the user can then trace through the code in that method.
If the user clicks over, the Debugger will step over that certain block of code.
If the user clicks through, the Debugger will continue on to the very next step of code. If a method is highlighted, the Debugger will run through the whole
method in one step.

These ways of tracing through code are very helpful when dealing with an unexpected bug. The Debugger allows the user to trace through the code step by step
and inspect of explore the variables after every step.

Writing Code
This method is written in the Integer class.
	self < 0
		ifTrue: [^ 'ERROR'].
	self = 0 | (self = 1)
		ifTrue: [^ 1].
	^ (self - 1) fibonacci + (self - 2) fibonacci

Tracing Code
"| data onlyPositiveNumbers |"
Declares data and onlyPositiveNumbers as variables, but does not initiate them.
data = nil

"data := OrderedCollection withAll: #(1 2 3 -4 -5 'error' 6 -7 999 2)."
This initiates data to be an OrderedCollection with each element above listed in order.
data = an OrderedCollection(1 2 3 -4 -5 'error 6 -7 999 2).

"onlyPositiveNumbers := [:i | (i isKindOf: Number) and: [i positive]]."
This line of code just sets onlyPositiveNumbers to a block of code which will iterate through
each element and determine if it is a Number and if its positive.
Still, data = an OrderedCollection(1 2 3 -4 -5 'error' 6 -7 999 2)

"data := data select: onlyPositiveNumbers."
This takes the block from above and iterates through the OrderedCollection data.
Again, it only selects the elements in data which are Numbers and are positive.
Therefore, data = an OrderedCollection(1 2 3 6 999 2).

"data := data copyUpTo: 999."
This copies all the elements of data up to the element 999, but does not include that element.
Therefore, data = an OrderedCollection(1 2 3 6)

"Transcript show: data average"
This line just prints out data average.
data average goes through all the elements and averages them out.
So, in the Transcript, you would see "3" without the quotes.
This line does not change anything in data.
So, data is still an OrderedCollection(1 2 3 6)

Links to this Page