Slide Notes on Factory Pattern, Building Pattern | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Memon; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-jil
koofers-user-jil 🇺🇸

4

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Builder Pattern
Mar. 13, 2007
2
Revisit the Factory Pattern
Factory Pattern returns one of several different
subclasses depending on the data passed via
parameters to the creation method(s).
Suppose we aren’t interested in returning objects
that are simple descendents of a base object, but
are assembled from different combinations of
(unrelated) objects.
3
What is the Builder Pattern?
The Builder Pattern assembles and returns a
number of objects in various ways depending on
the data passed via parameters to the creation
method(s).
4
An Example
Lets design a class that will build a User Interface
for us.
Requirements: write a program to keep track of
the performance of our investments. We might
have stocks, bonds and mutual funds, and we’d
like to display a list of our holdings in each
category so we can select one or more of the
investments and plot their comparative
performance.
Wealth Builder
pf3
pf4

Partial preview of the text

Download Slide Notes on Factory Pattern, Building Pattern | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2007

Builder Pattern Mar. 13, 2007

Revisit the Factory Pattern

parameters to the creation method(s).subclasses depending on the data passed viaFactory Pattern returns one of several different

(unrelated) objects.are assembled from different combinations ofthat are simple descendents of a base object, butSuppose we aren’t interested in returning objects

3

What is the Builder Pattern?

method(s).the data passed via parameters to the creationnumber of objects in various ways depending onThe Builder Pattern assembles and returns a

An Example

for us.Lets design a class that will build a User Interface

performance.investments and plot their comparativecategory so we can select one or more of thelike to display a list of our holdings in eachhave stocks, bonds and mutual funds, and we’dthe performance of our investments. We mightRequirements: write a program to keep track of

  • Wealth Builder

5

Example (contd…)

investment we might own at any given time.We cannot predict in advance how many of each kind of

of funds (such as mutual funds).large number of funds (such as stocks) or a small numberWe’d like to have a display that is easy to use for either a

that we can select one or more funds to plot.In each case, we want some kind of a multiple-choice display so

and if there are 3 or fewer funds, we’ll use a set of check boxes.If there is a large number of funds, we’ll use a multi-choice list box

have the same methods for returning the results.depends on the number of items to be displayed, and yetWe want our Builder class to generate an interface that

An Example Final Display

7

Let’s See Some Code!

the methods we need to implementstart with a multiChoice abstract class that defines

Lets Plan for a Second

multiple-choice display.The getUI method returns a Panel container with a

The two displays we’re using here

  • are derived from this abstract class:– a list box panel– a checkbox panel or

class listboxChoice extends multiChoice

  • or

class checkBoxChoice extends multiChoice

13

getSelected()

the

getSelected

method

the user selects.of the investmentsreturns a String array

Summary

builds.A Builder lets you vary the internal representation of the product it

It also hides the details of how the product is assembled.

program.Each specific builder is independent of the others and of the rest of the

relatively simple.This improves modularity and makes the addition of other builders

that a Builder constructs.depending on the data, you have more control over each final productBecause each builder constructs the final product step-by-step,

A Builder pattern is somewhat like a Factory pattern in that:

the data presented to it.objects, the Builder constructs a complex object step by step depending onThe main difference is that while the Factory returns a family of related