Leftover Pattern
Leftover Pattern
pattern
• Composite pattern is used where we need to treat a group of objects in a similar
way as a single object.
• This pattern creates a class that contains a group of its own objects. This class
provides ways to modify its group of the same objects.
It is described by Gof:
"Compose objects into a tree structure to represent part-whole
hierarchies. Composite lets client treat individual objects and
compositions of objects uniformly".
• Composite design pattern treats each node in two ways: composite or leaf.
Applicability
Use the Composite pattern when:
• There is a component model with a branch-leaf structure (whole-part
or container-contained).
Name =Michael
Salary =20000.0
Name =Daniel
Salary =25000.0
Name =John
Salary =10000.0
Name =David
Salary =15000.0
Flyweight pattern
Purpose
To reduce the number of very low-level, detailed objects within a system by sharing objects.
Introduction
This places a big load on the Java Virtual Machine’s (JVM) memory.
One way to alleviate the problem of having many objects is to share objects. Many of these
low-level objects only differ slightly, while most of their state and behaviour are identical.
Sharing instances reduces the number dramatically without losing any functionality.
For a set of objects, the Flyweight pattern separates those parts of the objects that are the
same from the parts that are different.
• The data that distinguishes the different instances (also called the externalized data)
is provided to the single generic instance when needed.
• For each nearly identical object, the non-identical parts can be separated from the
identical part allowing that identical part to be shared.
• Groups of nearly identical objects can be replaced by one shared object once the non-
identical parts of the state have been removed.
• If the application needs to distinguish among the nearly identical objects in their original
state.
Implementation
• Flyweight – The interface defines the methods clients can use to pass external state
into the flyweight objects.
• Client – The client is responsible for creating and providing the context for the
flyweights. The only way to get a reference to a flyweight is through flyweightFactory.
public interface Shape {
void draw(); }
Taxonomy in Taxonomy in
Application Domain Solution Domain
Motivation for the Bridge Pattern
Seat imp
(in Vehicle Subsystem)
VIP SeatImplementation
GetPosition()
SetPosition()
Problem
"Hardening of the software arteries" has occurred by using subclassing of an abstract base class
to provide alternative implementations.
You want to extend this class hierarchy to incorporate colors. So you plan to create
Red and Blue shape subclasses. However, since you already have two subclasses,
you’ll need to create four class combinations such as BlueCircle and RedSquare.
Another use of the Bridge Pattern:
Support multiple Database Vendors
Builder
An Object Creational Pattern
Intent / Applicability
Reference: Design Patterns, Gamma, et. al., Addison Wesley, 1995, pp 97-98
UML Structure
Example: building different types of airplanes
AerospaceEngineer:
director
AirplaneBuilder: abstract
builder
Airplane: product
Sample concrete builders:
CropDuster
FighterJet
Glider
Airliner
Director
package builder;
/** "Director" */
public class AerospaceEngineer {
}
Product
package builder;
/** "Product" */
public class Airplane {
}
ConcreteBuilder 2
package builder;
/** "ConcreteBuilder" */
public class FighterJet extends AirplaneBuilder {
}
ConcreteBuilder 3
package builder;
/** "ConcreteBuilder" */
public class Glider extends AirplaneBuilder {
}
ConcreteBuilder 4
package builder;
/** "ConcreteBuilder" */
public class Airliner extends AirplaneBuilder {
}
Client Application
package builder;
/** Application in which given types of airplanes are being constructed.
*/
public class BuilderExample {
public static void main(String[] args) {
// instantiate the director (hire the engineer)
AerospaceEngineer aero = new AerospaceEngineer();
// build a CropDuster
aero.setAirplaneBuilder(crop);
aero.constructAirplane();
Airplane completedCropDuster = aero.getAirplane();
System.out.println(completedCropDuster.getType() +
" is completed and ready for delivery to " +
completedCropDuster.getCustomer());
Advantages:
Allows you to vary a product’s internal representation
Encapsulates code for construction and representation
Provides control over steps of construction process
Disadvantages:
Requires creating a separate ConcreteBuilder for each different
type of Product
Related Patterns
return 9;
}
public class ProxyPatternClient {
public static void main(String[] args)
{
OfficeInternetAccess access = new ProxyInternetAccess(“A
BC");
access.grantInternetAccess();
}
}
Factory
public interface Shape {
void draw();
}