Design Patterns: Singleton, Structural (Adapter), and Their Implementations, Study notes of Software Engineering

An overview of the singleton and adapter patterns from the book 'design patterns' by anton roukhline, john setzer, and jingfeng yu. The singleton pattern ensures a class has exactly one instance and provides global access to it. The adapter pattern makes interfaces compatible. Examples, class diagrams, and benefits of each pattern.

Typology: Study notes

Pre 2010

Uploaded on 03/18/2009

koofers-user-fl9-1
koofers-user-fl9-1 🇺🇸

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Singleton, Structural, and
Adapter Patterns
By Anton Roukhline,John Setzer, and
Jingfeng Yu
Information taken from Design Patterns
Overview
nSingleton
nSummary of Structural Patterns
nAdapter
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Design Patterns: Singleton, Structural (Adapter), and Their Implementations and more Study notes Software Engineering in PDF only on Docsity!

Singleton, Structural, and

Adapter Patterns

By Anton Roukhline, John Setzer, and

Jingfeng Yu

Information taken from Design Patterns

Overview

n Singleton

n Summary of Structural Patterns

n Adapter

Singleton Creational Pattern

n Intent: To ensure that a class has

exactly one instance and provide global

access to that class.

n Pages 127-134 in Design Patterns

Printer Example

Printer 1 Printer 2 Printer 3 Printer 4

Print Spooler

A system may have many printers, but

it should should only have one printer

spooler.

Singleton’s Code

public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance ==null) { instance = new Singleton(); } return instance; }

Singleton Benefits

1. Controlled access to sole instance

2. Reduced name space

3. Easy refinement of operations and

representation

4. Allows a variable number of instances

5. More flexible that static methods that

do the same thing

Discuss with a Partner

n Try to name as many programs that

you have used or written that could

benefit from implementing a Singleton

structure.

n For Example: “It might be good for a

program X to use the Singleton pattern

because…”

Structural Patterns

n “Structural patterns are concerned with

how classes and objects are composed

to form larger structures.”

n “Structural class patterns use

inheritance to compose interfaces or

implementations.”

Solution

Adaptee Adapter Target

Adapter

n Converts the interface of one class into

a form that a second class understands.

n Lets two classes with previously

incompatible interfaces work together.

n Pages 139-150 in Design Patterns

Discuss with a Partner

n Why would a developer want to be

able to make two interfaces

compatible?

Adapter That Uses Object

Composition

Code

class Adapter extends Quadrilateral {

public Adapter() {} // draw is an abstract method in class Quadrilateral public draw ( point1, point2, point3, point4) { bottomLeftCorner = point1; sideLength = point2.x – point1.x; Square.draw(bottomLeftCorner, sideLength); }

Example: Make SQL Easier to

Use

public query(table, column, target) { … // initialize table connection here or preferably in a constructor … resultSet = Statement.executeQuery( “SELECT ” + target + “ FROM ” + table+ “ WHERE “ + column+ ” = “ +target+ ”;” ); … list = resultSet.toList(); return list; }

Benefits

n Makes old code more reusable.

n Lets you override adaptee’s behavior.

Consequences

n Adapter will have to be changed when

adaptee’s interface is changed.

n Makes it harder to override adaptee.