Docsity

Handling Different Tax Calculation Implementations in Software Design - Prof. Jeff K. Salv, Papers of Computer Science

Toggle sidebar
bg1
CS 350
CS 350 –
–Software Design
Software Design
The Strategy Pattern
The Strategy Pattern –
–Chapter 9
Chapter 9
Changes to software, like other things in life, often focus on t
Changes to software, like other things in life, often focus on the immediate concerns and
he immediate concerns and
ignore the longer term.
ignore the longer term.
Two common approaches:
Two common approaches:
•
•Overanalyze and
Overanalyze and overdesign
overdesign: Analysis paralysis
: Analysis paralysis
•
•Jump right in
Jump right in
Somewhere in between is the ideal.
Somewhere in between is the ideal.
This is why I feel the argument in Software Engineering about Ag
This is why I feel the argument in Software Engineering about Agile methodologies vs.
ile methodologies vs.
traditional waterfall development is silly. It should always be
traditional waterfall development is silly. It should always be a combination.
a combination.
In addition, a key element is to be wise enough to
In addition, a key element is to be wise enough to DESIGN FOR CHANGE
DESIGN FOR CHANGE.
.
Simple to say, no so intuitive (at first) to do.
Simple to say, no so intuitive (at first) to do.
With practice, it
With practice, it’
’s not that difficult.
s not that difficult.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Handling Different Tax Calculation Implementations in Software Design - Prof. Jeff K. Salv and more Papers Computer Science in PDF only on Docsity!

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

Changes to software, like other things in life, often focus on t

Changes to software, like other things in life, often focus on the immediate concerns and

he immediate concerns and

ignore the longer term.

ignore the longer term.

Two common approaches:

Two common approaches:

Overanalyze and

Overanalyze and overdesign

overdesign: Analysis paralysis

: Analysis paralysis

Jump right in

Jump right in

Somewhere in between is the ideal.

Somewhere in between is the ideal.

This is why I feel the argument in Software Engineering about Ag

This is why I feel the argument in Software Engineering about Agile methodologies vs.

ile methodologies vs.

traditional waterfall development is silly. It should always be

traditional waterfall development is silly. It should always be a combination.

a combination.

In addition, a key element is to be wise enough to

In addition, a key element is to be wise enough to

DESIGN FOR CHANGE

DESIGN FOR CHANGE

Simple to say, no so intuitive (at first) to do.

Simple to say, no so intuitive (at first) to do.

With practice, it

With practice, it’

’s not that difficult.

s not that difficult.

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

We do not anticipate the exact change, instead we anticipate wha

We do not anticipate the exact change, instead we anticipate what may change.

t may change.

New Case Study: E

New Case Study: E-

-Commerce System

Commerce System

The company is located in the US, but its business has an intern

The company is located in the US, but its business has an international scope

ational scope

A Task controller object handles sales request and passes it to

A Task controller object handles sales request and passes it to the

the SalesOrder

SalesOrder object to

object to

process the order.

process the order.

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

What happens when requirements change?

What happens when requirements change?

Perhaps a new requirement for taxation rules is added.

Perhaps a new requirement for taxation rules is added.

Handle taxation for countries outside the United States.

Handle taxation for countries outside the United States.

Thus, we need to add new rules for computing taxes.

Thus, we need to add new rules for computing taxes.

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

How do we handle different implementations of tasks that are pre

How do we handle different implementations of tasks that are pretty much conceptually the

tty much conceptually the

same?

same?

Copy and paste

Copy and paste

Switches/Ifs on variable specifying the case we have

Switches/Ifs on variable specifying the case we have

Use function pointers (a different one representing each case)

Use function pointers (a different one representing each case)

Inheritance

Inheritance

Delegate the entire functionality to a new object

Delegate the entire functionality to a new object

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

Simple Cases

Simple Cases –

– Two Countries

Two Countries

// Handle Tax// Handle Taxswitch (switch (myNation

myNation) {

) {

case US:case US:

//US Tax Rules here//US Tax Rules herebreak;break;

case Canada:case Canada:

//Canadian Tax Rules here//Canadian Tax Rules herebreak;break;

}} //Handle Currency//Handle Currencyswitch (switch (myNation

myNation) {

) {

case US:case US:

//US Currency Rules here//US Currency Rules herebreak;break;

case Canada:case Canada:

//Canadian Currency Rules here//Canadian Currency Rules herebreak;break;

}} //Handle Date Format//Handle Date Formatswitch (switch (myNation

myNation) {

) {

case US:case US:

//US Date Format Rules here//US Date Format Rules herebreak;break;

case Canada:case Canada:

//Canadian Date Format Rules here//Canadian Date Format Rules herebreak;break;

}}

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

Add a Third Country

Add a Third Country –

– Still a

Still a “

“Clean

Clean”

” implementation

implementation

// Handle Tax // Handle Taxswitch (switch (myNation

myNation) {

) {

case US:case US:

//US Tax Rules here//US Tax Rules herebreak;break;

case Canada:case Canada:

//Canadian Tax Rules here//Canadian Tax Rules herebreak;break;

case Germany:case Germany:

//Germany Tax Rules here//Germany Tax Rules here

break;break;

}} //Handle Currency//Handle Currencyswitch (switch (myNation

myNation) {

) {

case US:case US:

//US Currency Rules here//US Currency Rules herebreak;break;

case Canada:case Canada:

//Canadian Currency Rules here//Canadian Currency Rules herebreak;break;

case Germany:case Germany:

//Germany Currency Rules here//Germany Currency Rules herebreak;break;

}}

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

Variations

Variations “

“dirty

dirty”

” the implementation

the implementation

// Handle Language // Handle Languageswitch (switch (myNation

myNation) {

) {

case Canada:case Canada:

if (if (inQuebec

inQuebec) {

) {

//use French//use Frenchbreak;}break;}

elseelse

//use English//use Englishbreak;break;

case Germany:case Germany:

//use German//use German

break;break;

}}

Flow of the switches is hard to read

Flow of the switches is hard to read

When new cases come in, you must find every place it can be invo

When new cases come in, you must find every place it can be involved

lved

Called Switch Creep

Called Switch Creep

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

Function Pointers

Function Pointers

Nice

Nice

Compact

Compact

Can

Can’

’t retain state on a per

t retain state on a per-

-object basis

object basis

Inheritance

Inheritance

Nothing really wrong with it per say

Nothing really wrong with it per say

Certainly allows reuse

Certainly allows reuse

Could create new

Could create new SalesOrder

SalesOrder objects from existing objects

objects from existing objects

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

A Better Approach

A Better Approach

Find out what varies and encapsulate it in a class of its own.

Find out what varies and encapsulate it in a class of its own.

Contain this class in another class.

Contain this class in another class.

What varies in this example?

What varies in this example?

Tax rules, therefore create an abstract class that defines how t

Tax rules, therefore create an abstract class that defines how to accomplish taxation

o accomplish taxation

conceptually.

conceptually.

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

A Better Approach

A Better Approach

Now use aggregation to give the

Now use aggregation to give the SalesOrder

SalesOrder object the ability to handle Tax.

object the ability to handle Tax.

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

public

class

public

class

SalesOrder SalesOrder

public

void

process

public

void

process

(CalcTax

CalcTax

taxToUse taxToUse)

longlong

itemNumberitemNumber=0;

double

price=0;

double

price=0;

//given

the

tax

object

to

use,

calculate

the

tax

//given

the

tax

object

to

use,

calculate

the

tax

double

tax

double

tax

taxToUse.taxAmount(itemNumbertaxToUse.taxAmount(itemNumber,

price);

price);

}} public

class

public

class

TaskController TaskController

public

void

process()

public

void

process()

this

code

is

an

emulation

of

a

processing

task

controll

this

code

is

an

emulation

of

a

processing

task

controller

er

figure

out

what

country

you

are

in

figure

out

what

country

you

are

in

CalcTax CalcTax

myTaxmyTax;

myTax myTax

getTaxRulesForCountrygetTaxRulesForCountry();

SalesOrder SalesOrder

mySO mySO

new

new

SalesOrder SalesOrder();

mySO.process(myTax mySO.process(myTax);

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

A Better Approach

A Better Approach

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

Strategy Pattern

Strategy Pattern

Define a family of algorithms, encapsulate each one, and make th

Define a family of algorithms, encapsulate each one, and make them interchangeable.

em interchangeable.

Strategy lets the algorithm vary independently from clients that

Strategy lets the algorithm vary independently from clients that use it.

use it.

The Strategy Pattern is based on a few principles:

The Strategy Pattern is based on a few principles:

Objects have responsibilities

Objects have responsibilities

Different specific implementations of these responsibilities ar

Different specific implementations of these responsibilities are manifested through the use

e manifested through the use

of polymorphism

of polymorphism

There is a need to manage several different implementations of

There is a need to manage several different implementations of the same basic algorithm.

the same basic algorithm.

It is good design practice to separate behaviors that occur in t

It is good design practice to separate behaviors that occur in the same problem domain from

he same problem domain from

each other.

each other.

CS 350

CS 350 –

– Software Design

Software Design

The Strategy Pattern

The Strategy Pattern –

– Chapter 9

Chapter 9

Strategy Pattern

Strategy Pattern

Intent: Enable you to use different business rules or algorithms

Intent: Enable you to use different business rules or algorithms depending on the context in

depending on the context in

which they occur.

which they occur.

Problem: The selection of an algorithm that needs to be applied

Problem: The selection of an algorithm that needs to be applied depends on the client

depends on the client

making the request or the data being acted on.

making the request or the data being acted on.

Solution: Separate the selection of the algorithm from the imple

Solution: Separate the selection of the algorithm from the implementation of the algorithm.

mentation of the algorithm.