Factorization
• Problem: code duplication
• Solution: move the code to a method and call the method
instead
• (A standard refactoring procedure)
javaprogrammering
Factorization
class FactorizedComputation {
class Computation {
void computeAll() {
void method1() {
computeStep1();
//…
computeStep2();
computeStep1();
computeStep3();
computeStep2();
}
computeStep3();
void method1() {
}
//…
void method2() {
computeAll();
//…
}
computeStep1();
void method2() {
computeStep2();
//…
computeStep3();
computeAll();
}
}
}
}
javaprogrammering
Factorization by Inheritance
- What if the duplicated code is in two different classes?
class ComputationA { class ComputationB {
void method1() { void method2() {
//… //…
computeStep1(); computeStep1();
computeStep2(); computeStep2();
computeStep3(); computeStep3();
//… //…
} }
} }
javaprogrammering
Factorization by Inheritance
class Common {
void computeAll() {
computeStep1();
computeStep2();
computeStep3();
}
}
class ComputationA extends class ComputationB extends
Common { Common {
void method1() { void method2() {
//… //…
computeAll(); computeAll();
//… //…
} }
} }
- What if the (single) inheritance is occupied?
javaprogrammering
Factorization by delegation
class Helper {
void computeAll() {
computeStep1();
computeStep2();
computeStep3();
}
}
class ComputationA { class ComputationB {
Helper helper; Helper helper;
//… //…
void method1() { void method2() {
//… //…
helper.computeAll(); helper.computeAll();
//… //…
} }
} }
javaprogrammering
Factorization
- What if a piece of unique (non-duplicated) code is found
within the duplicated code?
javaprogrammering
Template method
• Defines the skeleton of an algorithm
• Allows for subclasses to redefine certain steps of the
algorithm without changing it’s structure
• Useful in situations when you have unique code inside a
section of duplicate code (i.e. where simple factorization
doesn’t work)
javaprogrammering
Template method, ex
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
firstMethod();
} catch (SomeException se) {
se.printStackTrace();
messageTextField.setText(se.getMessage());
}
}
}
javaprogrammering
Template method, ex
class MyOtherListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
anotherMethod();
} catch (SomeException se) {
se.printStackTrace();
messageTextField.setText(se.getMessage());
}
}
}
This actionPerformed() is identical to the previous,
except for anotherMethod() !
javaprogrammering
Template method, ex
abstract class ParentListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
action();
The template method
} catch (SomeException se) {
se.printStackTrace();
messageTextField.setText(se.getMessage());
}
}
public abstract void action() throws SomeException;
}
A “hook method”
javaprogrammering
Template method, ex
class MyListener implements ParentListener {
public void action() throws SomeException {
firstMethod();
}
}
class MyOtherListener implements ParentListener {
public void action() throws SomeException {
anotherMethod();
}
}
javaprogrammering
Proxy
• Provides a surrogate for the real object
• Useful when the real object
– needs protection (e.g. limited access users get access to a proxy)
– is a remote object (e.g. RMI)
– lacks some good-to-have actions (“smart reference”, e.g. load at first access)
• The proxy knows about the real object and implements the
same interface
javaprogrammering
Proxy
Client <<interface>>
ObjectInterface
RealObject Proxy
javaprogrammering
Adaptor
• Converts the interface of one class into another interface
• Lets classes work together that couldn't otherwise
because of incompatible interfaces
ClassA Adaptor Adaptee
request() newRequest()
Component WitdrawListener
BankSystem
actionPerformed()
withdraw()
javaprogrammering
About design patterns
• Schematic descriptions of solutions to recurring problems
in software design
• Purpose:
– capture experience (expertise)
– support reuse of proven solutions
– provide a common vocabulary
• Simply put : “…a bag of tricks that every competent
programmer should know.”
javaprogrammering
GOF – “Gang of four”
• The book on design patterns:
– “Design Patterns” by Gamma, Helm, Johnson & Vlissides (1995)
• Contains 23 of the most common patterns divided into
– Creational patterns (e.g. Singleton)
– Structural patterns (e.g. Adapter)
– Behavioral patterns (e.g. Observer)
• The patterns in the book “Design Patterns” are often referred to as
“GOF patterns”
• There are many other patterns, often applied to specific areas (EJB,
DB, thread handling, etc.)
javaprogrammering
Pattern description
• The description of a pattern may contain:
– Pattern name (e.g. Singelton)
– Category (e.g. Creational)
– Intent (the problem addressed)
– Also known as… (other names used)
– Applicability
– Structure (usually a UML class diagram)
– Participants (classes / objects involved)
• Note: a design pattern is only a suggestion, or a hint, on
how to solve a problem!
javaprogrammering
AWT vs. Swing
• AWT is limited – based on a highest common factor model
• Many platform specific things cannot be done in AWT…
• Swing is “lightweight” – components are drawn by Java
(not OS)
• Swing has many more components than AWT
• Swing is slower than AWT…
• “look-and-feel” is platform dependent in AWT
• “look-and-feel” is platform independent in Swing
• Swing looks better, is more predictable, and more fun…
• (Rule of thumb: Don’t dispose of components, just hide
them!)
javaprogrammering
This document was created with Win2PDF available at http://www.daneprairie.com.
The unregistered version of Win2PDF is for evaluation or non-commercial use only.