Document 15075202

advertisement
Mata kuliah
Tahun
: T0144 – Advanced Topics in Software Engineering
: 2010
Pertemuan 08
Design Patterns & Anti-Patterns
Learning Outcomes
Pada akhir pertemuan ini, diharapkan:
– Mahasiswa dapat mendesain software dengan
menerapkan Design Pattern dan menghindari AntiPattern
3
Outline Material
•
•
Design Patterns Definition
Design Patterns Examples
–
–
–
–
–
–
•
•
Singleton
Adapter
Decorator
Proxy
Observer
Façade
How to use Design Pattern
Anti-Patterns
4
What is a Design Pattern?
• In software engineering, a design pattern is a general reusable
solution to a commonly occurring problem in software design.
• A design pattern is not a finished design that can be
transformed directly into code. It is a description or template
for how to solve a problem that can be used in many different
situations.
• Object-oriented design patterns typically show relationships
and interactions between classes or objects, without specifying
the final application classes or objects that are involved.
Example1 : Singleton
• Singleton pattern is a design pattern that is
used to restrict instantiation of a class to one
object.
• This is useful when exactly one object is needed
to coordinate actions across the system.
Example2: Adapter
• Adapter design pattern (often referred
to as the wrapper pattern or simply a
wrapper) translates one interface for a
class into a compatible interface.
• An adapter allows classes to work
together that normally could not
because of incompatible interfaces, by
providing its interface to clients while
using the original interface.
• The adapter translates calls to its
interface into calls to the original
interface, and the amount of code
necessary to do this is typically small.
Example3: Decorator
• In object-oriented programming, the decorator pattern is a design
pattern that allows new/additional behaviour to be added to an existing
object dynamically.
Example4: Proxy
• A proxy is a class functioning as an
interface to something else.
• The proxy could interface to
anything: a network connection, a
large object in memory, a file, or
some other resource that is
expensive or impossible to
duplicate.
Example5: Observer
• The observer pattern is a
software design pattern in
which an object, called the
subject, maintains a list of its
dependents, called observers,
and notifies them
automatically of any state
changes, usually by calling
one of their methods.
• It is mainly used to implement
distributed event handling
systems.
Example6: Façade
• A facade is an object that
provides a simplified
interface to a larger body of
code, such as a class library.
Façade Pattern Benefits
• A facade can:
– make a software library easier to use and understand, since the facade
has convenient methods for common tasks;
– make code that uses the library more readable, for the same reason;
– reduce dependencies of outside code on the inner workings of a library,
since most code uses the facade, thus allowing more flexibility in
developing the system;
– wrap a poorly-designed collection of APIs with a single well-designed API
(as per task needs).
Source Code Demo
How to use Design Patterns
• Each pattern describe a particular design problem, with specific
constraints and context.
• The “pattern” is a well proven generic solution scheme.
• Aims to decouple strongly interdependent objects
– Strong Coupling == more interdependency == bad
• Distribution of methods/data to other components
– Avoids the ‘God object’ syndrome.
Anti-Patterns
• Anti-Pattern is a design pattern that may be commonly used but
is ineffective and/or counterproductive in practice
• Symptoms:
– Some repeated pattern of action, process or structure that initially
appears to be beneficial, but ultimately produces more bad
consequences than beneficial results
– A refactored solution exists that is clearly documented, proven in actual
practice and repeatable.
• By formally describing repeated mistakes, one can recognize the
forces that lead to their repetition and learn how others have
refactored themselves out of these broken patterns.
Anti-Pattern Categories
•
•
•
•
•
•
•
Organizational anti-patterns
Project management anti-patterns
Analysis anti-patterns
Software design anti-patterns
Programming anti-patterns
Methodological anti-patterns
Configuration management anti-patterns
Example 1: Copy&Paste Programming
• Copy and paste programming is a pejorative term to describe
highly repetitive computer programming code apparently
produced by copy and paste operations.
• It is frequently symptomatic of a lack of programming
competence, or an insufficiently expressive development
environment, as subroutines or libraries would normally be
used instead.
• In certain contexts it has legitimate value, if used with care.
Example 2: God Object
• In object-oriented programming, a God object is an object that
knows too much or does too much.
• Why is this bad ?
References
•
•
•
Design Patterns: Elements of Reusable Object-Oriented Software.
Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides (1995).
Addison-Wesley. ISBN 0-201-63361-2.
AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis.
Brown, William J.; Raphael C. Malveau, Hays W. "Skip" McCormick, Thomas
J. Mowbray, Theresa Hudson (ed) (1998).
John Wiley & Sons, ltd. ISBN 0-471-19713-0.
Anti-pattern on Wikipedia
http://en.wikipedia.org/wiki/Anti-pattern
19
Download