• Keine Ergebnisse gefunden

Design Patterns and Refactoring Creational Patterns: Motivation Oliver Haase

N/A
N/A
Protected

Academic year: 2022

Aktie "Design Patterns and Refactoring Creational Patterns: Motivation Oliver Haase"

Copied!
8
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Design Patterns and Refactoring

Creational Patterns: Motivation

Oliver Haase

HTWG Konstanz

(2)

A Simple, Motivating Example . . .

Assume an oversimplified document manager that can create, open, and close a LaTeX document:

p u b l i c c l a s s DocManager { p r i v a t e L a t e x D o c doc ;

p u b l i c v o i d newDoc ( ) { doc = new L a t e x D o c ( ) ; }

p u b l i c v o i d openDoc ( ) { doc . open ( ) ;

}

p u b l i c v o i d c l o s e D o c ( ) { doc . c l o s e ( ) ;

} }

(3)

But. . .

. . . What about the first principle of reusable software design?

OK, makeLatexDocan implementation of an interface:

interface Document open(): void close(): void

Lat exDoc open(): void close(): void

(4)

But. . .

. . . What about the first principle of reusable software design?

OK, makeLatexDocan implementation of an interface:

interface Document open(): void close(): void

Lat exDoc open(): void close(): void

(5)

But. . .

. . . What about the first principle of reusable software design?

OK, makeLatexDocan implementation of an interface:

interface Document open(): void close(): void

Lat exDoc open(): void close(): void

(6)

Then . . .

Make docan instance ofDocument, rather thanLatexDoc:

p u b l i c c l a s s DocManager { p r i v a t e Document doc ;

p u b l i c v o i d newDoc ( ) { doc = new L a t e x D o c ( ) ; }

p u b l i c v o i d openDoc ( ) { doc . open ( ) ;

}

p u b l i c v o i d c l o s e D o c ( ) { doc . c l o s e ( ) ;

} }

(7)

Dependency Situation

Now, DocManagerhas become dependent on both Documentand LatexDoc:

interface Document open(): void close(): void DocManager

Lat exDoc open(): void close(): void

(8)

So What?

What’s wrong with this creational dependeny?

DocManagercannot be used for other document types, e.g. plain text documents, even though its functionality is applicable to other document types as well;

Difficult to use document mock-up for testing;

In a framework, DocManagerwould not even know – and care about – what document type to create.

Creational patterns are all about removing this creational dependency.

Referenzen

ÄHNLICHE DOKUMENTE

Assume, a framework defines a base class (interface) that the specific custom class has to extend (implement), in order to be used in place of the base class (interface).. The

A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior..

Communication is centralized in broker (mediator) object Broker is responsible to preserve consistency. Broker is single point of failure and scalability bottle-neck Communication

Implementor (CommImpl): defines interface for implementation classes (can differ from Abstraction interface). ConcreteImplementor (TcpCommunication): provides implementation

knows which subsystem classes are responsible for which requests delegates client requests to respective subsytem

Purpose: Define method for object creation in abstract class, leave actual creation to concrete subclasses.. Also known as:

Divide object state into intrinsic and extrinsic state, such that there is only a small number of distinct objects with different intrinsic states.. Share these

Element: defines an accept operation with a Visitor as argument ConcreteElement: implements the accept operation, usually by calling the visitor’s appropriate visit