CS代写 SWEN30006 Software Modelling and Design Applying GoF Design Patterns

APPLYING GOF DESIGN PATTERNS
The shift of focus (to patterns) will have a profound and enduring effect on the way we write programs.
—Ward Cunningham (co-author of the Manifesto for Agile Software Development) and (co-author of Design Patterns: Elements of Reusable Object-Oriented Software)
Software Modelling and Design

Copyright By PowCoder代写 加微信 powcoder

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
A Brief History of Design Patterns
❑ 1977: publishes: “A Pattern Language: Towns, Buildings, Construction”
❑ 1987: Cunningham and Beck used Alexander’s ideas to develop a small pattern language for Smalltalk
❑ 1990: The Gang of Four (Gamma, Helm, Johnson and Vlissides) begin work compiling a catalog of design patterns
❑ 1991: gives first Patterns Workshop at OOPSLA
❑ 1993: and sponsor the first meeting of what is now
known as the Hillside Group
❑ 1994: First Pattern Languages of Programs (PLoP) conference
❑ 1995: The Gang of Four (GoF) publish their ground breaking book: “Design Patterns: Elements of Reusable Software”

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Patterns in Architecture
“The street cafe provides a unique setting, special to cities: a place where people can sit lazily, legitimately, be on view, and watch the world go by… Encourage local cafes to spring up in each neighborhood. Make them intimate places, with several rooms, open to a busy path, where people can sit with coffee or a drink and watch the world go by. Build the front of the cafe so that a set of tables stretch out of the cafe, right into the street.”
— et al., A Pattern Language, pages 437,439
and the co-authors didn’t create something new; they actually observed what already existed and document them as patterns.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Gang of Four Design Patterns
Creational Patterns (5):
❑ abstract the object instantiation process. Structural Patterns (7):
❑ describe how classes and objects can be combined to form larger structures.
❑ provides a way to define relationships between classes or objects.
Behavioural Patterns (11):
❑ are most specifically concerned with communication/interaction between objects.
Named “Gang of Four” for the four authors.
Based around C++.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Objectives
On completion of this topic you should be able to:
❑ Apply some GoF design patterns o Adapter
o (Concrete) Factory o Singleton
o Strategy
o Composite
o Observer o Decorator
❑ Recognise GRASP principles as a generalization of other design patterns. ❑ We don’t cover all GoF 23 design patterns in this course.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Problem 1:
External Services with Varying Interfaces
❑ POS system needs to support several kinds of external services, including:
o Tax calculators
▪ e.g., TaxMaster, GoodAsGoldTaxPro
o Accounting systems
▪ e.g., SAP, GreatNorthern
o Credit authorization services
❑ Each service has a different API, which cannot be changed.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Adapter (GoF)
❑ How to resolve incompatible interfaces, or provide a stable interface to
similar components with different interfaces?
Solution (advice):
❑ Convert the original interface of a component into another interface, through an intermediate adapter object.
o We don’t mean programming language interfaces. E.g. Java interfaces.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
The Adapter Pattern
We define this interface.
For each external system we interact with, we define an adapter.
«interface» ITaxCalculatorAdapter
getTaxes( Sale ) : List of TaxLineItems
Adapters use interfaces and polymorphism to add a level of indirection to varying APIs in other components.
TaxMasterAdapter
getTaxes( Sale ) : List of TaxLineItems
GoodAsGoldTaxPro Adapter
getTaxes( Sale ) : List of TaxLineItems
«interface» IAccountingAdapter
postReceivable( CreditPayment ) postSale( Sale )
«interface» ICreditAuthorizationService Adapter
requestApproval(CreditPayment,TerminalID, MerchantID) …
«interface» IInventoryAdapter
SAP AccountingAdapter
postReceivable( CreditPayment ) postSale( Sale )
GreatNorthernAccountingAdapter
postReceivable( CreditPayment ) postSale( Sale )

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Using an Adapter
Doesn’t know what external package we are using.
makePayment
: SAPAccountingAdapter
SOAP over HTTP
postSale( sale )
: SAPSystem
the Adapter adapts to interfaces in other components

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Example: Disney Madness
❑ Using an external libraries for Disney characters o APIs cannot be modified
These have different interfaces. We want to make these easier to use (with one general command).

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Disney Madness: Design without Adapter
Main has to interact directly with all three classes – high coupling.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Disney Madness: Design with Adapter
Create a common interface for the Main class and create an adapter for each library

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Disney Madness: Design with Adapter
We implement a method called move()
Each character has an adapter which implements move()

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Disney Madness: Implement with Adapter
public interface IDisneyAdapter { public void move();
public class SlothAdapter implements IDisneyAdapter { private Sloth theSloth;
public SlothAdapter(String name) {
theSloth = new Sloth(name); }
public void move() { theSloth.wake(); theSloth.pushToClimb();

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Disney Madness: Implement with Adapter
public class DisneyMadess {
public static void main(String[] args) {
//With adapter
IDisneyAdapter slothAdt = new SlothAdapter(“Flash”); slothAdt.move();
IDisneyAdapter ironManAdt = new IronManAdapter(“Tony”); ironManAdt.move();
IDisneyAdapter trooperAdt = new TrooperAdapter(”Storm”);
trooperAdt.move(); }
Now the code is simpler.
We don’t need to know how each character moves. This is hidden away.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Issues arising from Adapter
❑ Who creates the adapters?
❑ Which class of adapter should be created?
o Should the Register object create SAPAccountingAdapter?
: SAPAccountingAdapter
makePayment
SOAP over HTTP
postSale( sale )
: SAPSystem
the Adapter adapts to

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Issues arising from Adapter
❑ Who creates the adapters?
❑ Which class of adapter should be created?
o Should the Register object create SAPAccountingAdapter?
Partial Answer:
❑ Separation of Concerns/High Cohesion
o Creating adapters is not pure application logic o Adapter object is not a domain object
Doesn’t correspond to anything in real world.
❑ Use GRASP Pure Fabrication pattern
So we fabricate an
object to manage this. 24

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
❑ Who should be responsible for creating objects when there are special considerations, such as complex creation logic, a desire to separate the creation responsibilities for better cohesion, and so forth?
Solution (advice):
❑ Create a Pure Fabrication object called a Factory that handles the creation.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
(Concrete) Factory
Aka Simple Factory
❑ Simplified version of GoF Abstract Factory pattern
Advantages:
❑ Separate responsibility of complex creation into cohesive helper objects.
❑ Hide potentially complex creation logic.
❑ Allow introduction of performance-enhancing memory management strategies, such as object caching or recycling.
o e.g. connecting to a database many times.

SWEN30006 Software Modelling and Design
Applying GoF Design Patterns
The Factory Pattern
note that the factory methods return objects typed to an interface rather than a class, so that the factory can return any implementation of the interface
Properties File Entry
! taxcalculator.class.name=TaxMasterAdaptor taxcalculator.class.name=GoodAsGoldTaxProAdaptor
The adapters we looked at before
ServicesFactory
accountingAdapter : IAccountingAdapter inventoryAdapter : IInventoryAdapter taxCalculatorAdapter : ITaxCalculatorAdapter
getAccountingAdapter() : IAccountingAdapter getInventoryAdapter() : IInventoryAdapter getTaxCalculatorAdapter() : ITaxCalculatorAdapter …
if ( taxCalculatorAdapter == null ) {
// a reflective or data-driven approach to finding the right class: read it from an // external property
String className = System.getProperty( “taxcalculator.class.name” );
taxCalculatorAdapter = (ITaxCalculatorAdapter) Class.forName( className ).newInstance();
This uses “lazy creation” – don’t create adapter unless required.
What makes the factory pattern is the ability to be able to get the adapter (or what the factory is making), and to get the right one. The means by which the right one is set can vary.
} 27 return taxCalculatorAdapter;

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Example: Disney Madness
❑ Using an external libraries for Disney characters o APIs cannot be modified

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Implement DisneyMadness with Adapter and Factory public class DisneyFactory {
private IDisneyAdapter characterAdapter = null;
/* Retrieve character adapter based on character name */
public IDisneyAdapter getDisneyAdapter(String character, String name) { if(character.equals(“ironman”)) {
characterAdapter = (IDisneyAdapter) new IronManAdapter(name); } else if(character.equals(“sloth”)) {
characterAdapter = (IDisneyAdapter) new SlothAdapter(name); } else if(character.equals(“trooper”)) {
characterAdapter = (IDisneyAdapter) new TrooperAdapter(name); }
return characterAdapter; }
We could use a method, rather than a variable in a properties file, to determine adapter type.
We could use an Enum above instead of a string.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Issues arising from Factory
❑ Who creates the Factory and how is it accessed? o It may need to be called from various places
❑ Oftentimes only one instance of this factory is needed.
❑ How to get visibility to this single ServicesFactory
access Factory?
:Register makePayment
: SAPAccountingAdapter
postSale( sale )
SOAP over HTTP
: SAPSystem
the Adapter adapts to interfaces in other components

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Issues arising from Factory
❑ How is Factory accessed?
o It may need to be called from various places
❑ How to get visibility to this single ServicesFactory instance?
Partial Answer:
❑ Only one instance of the factory is needed
❑ Pass the factory to methods or initialise objects with
a reference? No. Adds a lot of complexity.
❑ Use Singleton pattern to provide a single access point through global visibility
Singleton pattern is often used with Factory pattern.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Singleton (GoF)
❑ Exactly one instance of a class is allowed—it is a “singleton.” Objects need a global and single point of access.
Solution (advice):
❑ Define a static method of the class that returns the singleton.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Same as we did before for Adapters.
// static method
public static synchronized ServicesFactory getInstance() {
if ( instance == null )
Use syncronized modifer to avoid race conditions.
in ServicesFactory Class
UML notation: in a class box, an underlined attribute or method indicates a static (class level) member, rather than an instance member
ServicesFactory instance : ServicesFactory
singleton static attribute
singleton static method
instance = new ServicesFactory() return instance
UML notation: this ‘1’ can optionally be used to indicate that only one instance will be created (a singleton)
accountingAdapter : IAccountingAdapter inventoryAdapter : IInventoryAdapter taxCalculatorAdapter : ITaxCalculatorAdapter
getInstance() : ServicesFactory
getAccountingAdapter() : IAccountingAdapter getInventoryAdapter() : IInventoryAdapter getTaxCalculatorAdapter() : ITaxCalculatorAdapter …
Lazy Initialization

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Accessing the
public class Register
public void initialize() {
… do some work …
// accessing the singleton Factory via the getInstance call
accountingAdapter =
ServicesFactory.getInstance().getAccountingAdapter();
Solving the problem from slide 31.
… do some work … }
// other methods…
} // end of class
SingletonClass.getInstance()
is globally visible

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Implicit getInstance Message
initialize
aa = getAccountingAdapter
the ‘1’ indicates that visibility to this instance was achieved via the Singleton pattern
aa = ServicesFactory.getInstance().getAccountingAdapter();
1 :ServicesFactory

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Static: why some and not all?
Why aren’t all Singleton service methods static?
E.g. static getAccountingAdapter(): …
❑ May want subclasses: static methods are not polymorphic (virtual); no overriding in most languages
❑ Most remote communication mechanisms (e.g. Java’s RMI) don’t support remote-enabling of static methods.
❑ A class is not always a singleton in all application contexts.

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Problem 1 Revisited:
External Services with Varying Interfaces
Possible solution:
❑ “To handle this problem, let’s use Adapters
generated from a .”
This is language we can use to talk to our colleauges in terms of patterns.
We can discuss solutions in a simpler way.
Register in POS example.
❑ Design reasoning based on: o Controller
o Protected Variations o Low Coupling
o High Cohesion
o Indirection
o Polymorphism o Adaptor
o Singleton

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Adapter, Factory, and s
:Regis ter
Creating the Solution
accountingAdapter = getAccountingAdapter
Using the Solution
c re ate (cas hTe nd er ed ) postSale( sale )
:ServicesFactory
accountingAdapter: SAP Acc ou n ti ngAd ap te r
: SAPAccounting Adapter
:Regis ter
make Payment
SOAP over HTTP
«a ct or »
: SAPSystem

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
More examples, on YouTube
From LMS Design Pattern Resources:
❑ Tank Design Patterns Tutorials o Adaptor Design Pattern
o Factory Design Pattern
❑ Design Patterns in OO Programming: by C. Okhravi based on the textbook Head First Design Patterns
o FactoryDesignPattern
o Pattern

SWEN30006 Software Modelling and Design Applying GoF Design Patterns
Lecture Identification
Semester: S1 2022
© University of Melbourne 2022
These slides include materials from:
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, Third Edition, by , Inc., 2005.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com