ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17:...

10
ANU COMP2110 Software Design in 2004 Lecture 17 Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1 The Abstract Factory pattern 2 Adapter pattern

Transcript of ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17:...

Page 1: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 1

COMP2110 in 2004 Software DesignLecture 17: Software design patterns (4)

1 The Abstract Factory pattern

2 Adapter pattern

Page 2: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 2

Abstract Factory (1)

• Abstract Factory is a creational pattern – like these

• abstract factory

• factory

• singleton

• prototype

• other types: structural

• façade, composite, adapter, ...

• behavioral

• observer, iterator, command, state, visitor, ...

Page 3: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 3

Creational patterns

Creational patterns are ways of organising the creation of many objects (beyond new, creation, constructor)

Factory create new objects when the constructoralone is not enough

Singleton create only one widely shared objectwith a public accessor method

Prototype create sets of objects like example objects

Abstract Factory create families of objects in related groups

Page 4: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 4

Abstract Fcatory - an example [2001 exam]A software system for handling student results has these requirements:

1. There are two classes of students: Arts and Science. Each Faculty will have its own set of records, one for all Arts students, the other for all Science students.

2. A student's record contains an ID number, type of degree, and marks for all subjects taken this semester.

3. Arts students’ records have their major discipline (a string like "History").4. Arts students only take Arts subjects; Science students only take Science

subjects.5. Each arts student takes 4 subjects, each science student takes only 3.6. Every subject has a name and a subject code.7. Arts subjects have an exam mark and an essay mark, each out of 100.8. Science subjects have an exam mark out of 100 and two assignment

marks 9. Every subject has a final mark that can be computed by a function called

final.10. The same method called grade is used for all subjects for calculating the

grade from the final mark.

Page 5: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 5

Students and subjects

ArtsStudt

major

SciStudt

examessay

final()

examasg1asg2

final()

ArtsSubjt ScienceSubjt

StudentID,degree

Subject

ScienceRecordMakerCreateStudent()CreateSubject()

<<create>>

ArtsRecordMakerCreateStudent()CreateSubject()

AbstractRecordMakerCreateStudent()CreateSubject()

RM = new ArtsRecordMaker() // or new ScienceRecordMaker()s = RM.CreateStudent() -- includes setNSubjsfor sc in [1..s.NSubjs] s.subject[sc]= RM.CreateSubject()

Page 6: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 6

Abstract Factory – the Pattern (GoF p87+)

Intent: provide an interface for creating families of related or dependent objects without specifying their concrete classes

Applicability: use when

• a system should be independent of how its products are created, composed, represented

• a system should be configured with one or more multiple families of products

• a family of related products is designed to be used together, and you need to enforce this constraint

• you want to provide a class library of products, and you want to reveal their interfaces, not their implementations

Page 7: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 7

Abstract Factory – the general structure

• see diagram – Jezequel p. 87

Page 8: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 8

Abstract Factory – the Pattern (GoF p87+)

Consequences:

• isolates concrete classes

• makes exchanging product families easy(e.g. add a new Faculty – just another ConcreteFactory class like ArtsRecordMaker)

• promotes consistency among products

• BUT – supporting new families of products is difficultbecause the interface fixes the set of products in the family(e.g. [CreateStudent, CreateSubject])

Page 9: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 9

ArtsStudt

major

SciStudt

examessay

final()

examasg1asg2

final()

ArtsSubjt ScienceSubjt

StudentID,degree

Subject

ScienceRecordMakerCreateStudent()CreateSubject()

ArtsRecordMakerCreateStudent()CreateSubject()

AbstractRecordMakerCreateStudent()CreateSubject()

Abstract Factory

AbstractFactory

ConcreteFactory1

ConcreteFactory2

Abstract ProductA

Abstract ProductB

ProductA1 ProductA2

ProductB2ProductB1

Page 10: ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.

ANU COMP2110 Software Design in 2004 Lecture 17 Slide 10

lecture continues...

• more on Abstract Factory – see separate HTML notes 17.2 abstract-factory.html

• Adapter – see separate HTML notes17.3 adapter.html