Design Patterns

S

sck10

Hello,

I just started reading about Design Patterns and UML Class Diagrams on the
web. Anyway, I have a method that I use to format currency given certain
parameters that are supplied from the dataset from SQL Server. I was going
to create another one for Fixed, Percent ect. I know I could create a
switch statement to do this, but wanted to try to use UML as a learning
process.

So I'm trying to figure out how to break up the simple method below to do
this. Any suggestions or references would be appreciated.

sck10


public class StringFormat
{
public string NumberFormater(
object CompareValue, string CompareToValue,
object FormatValue,
string StartReturn, string EndReturn)
{
string strCompareValue = Convert.ToString(CompareValue);
double dblFormatValue;
string strFormatValue;

if(strCompareValue == CompareToValue)
{
dblFormatValue = Convert.ToDouble(FormatValue);
return StartReturn + string.Format("{0:C0}", FormatValue) + EndReturn;
}
else
{
return string.Format("{0:C0}", FormatValue);
} // end if
}

}
 
C

Cowboy \(Gregory A. Beamer\)

Create an interface that takes the number as input and outputs as a string.
You can then create a factory pattern that serves up the correct class
depending on what you ask the factory. If you want to go full bore with
design patterns, you create an abstract factory that returns the class that
adheres to the interface and call a concrete factory for each type. This is
probably overkill for this example, but will give you great experience in
patterns (and is most "technically" correct).

For examples:
http//:www.dofactory.com

UML is a bit dated for .NET, in some ways, and the design surfaces in team
system have kept up. Do not let this discourage your UML use, as the models
are very similar.
 
S

sloan

You can look at my blog for the Simple Factory pattern:
http://sholliday.spaces.live.com/blog/ 12/1/2005

As previously stated, you can create an interface.

IFormatCurrency

2 classes that implement this interface (these are called "concrete
classes")

FixedCurrencyFormatter : IFormatCurrency

PercentCurrencyFormatter : IFormatCurrency

then you create "Factory" that returns a IFormatCurrency

pubic static IFormatCurrency ( string param1 , string param2 , string
param3)
//implement the logic to either return a FixedCurrencyFormatter or a
PercentCurrencyFormatter.


You can also look a the Strategy Pattern
http://www.dofactory.com/Patterns/PatternStrategy.aspx
You would choose the Strategy pattern if you knew your Entity/BusinessObject
had to have one CurrencyFormatter, and if you wanted to change its
CurrencyFormatter at run time. (or compile time, but at run time also).
 
S

sck10

Thanks Cowboy,

appreciated the help...

sck10


Cowboy (Gregory A. Beamer) said:
Create an interface that takes the number as input and outputs as a
string. You can then create a factory pattern that serves up the correct
class depending on what you ask the factory. If you want to go full bore
with design patterns, you create an abstract factory that returns the
class that adheres to the interface and call a concrete factory for each
type. This is probably overkill for this example, but will give you great
experience in patterns (and is most "technically" correct).

For examples:
http//:www.dofactory.com

UML is a bit dated for .NET, in some ways, and the design surfaces in team
system have kept up. Do not let this discourage your UML use, as the
models are very similar.
 
S

sck10

Thanks for the links sloan,

sck10


sloan said:
You can look at my blog for the Simple Factory pattern:
http://sholliday.spaces.live.com/blog/ 12/1/2005

As previously stated, you can create an interface.

IFormatCurrency

2 classes that implement this interface (these are called "concrete
classes")

FixedCurrencyFormatter : IFormatCurrency

PercentCurrencyFormatter : IFormatCurrency

then you create "Factory" that returns a IFormatCurrency

pubic static IFormatCurrency ( string param1 , string param2 , string
param3)
//implement the logic to either return a FixedCurrencyFormatter or a
PercentCurrencyFormatter.


You can also look a the Strategy Pattern
http://www.dofactory.com/Patterns/PatternStrategy.aspx
You would choose the Strategy pattern if you knew your
Entity/BusinessObject
had to have one CurrencyFormatter, and if you wanted to change its
CurrencyFormatter at run time. (or compile time, but at run time also).
 
S

sck10

Thanks for the help Sloan...

sck10


sloan said:
You can look at my blog for the Simple Factory pattern:
http://sholliday.spaces.live.com/blog/ 12/1/2005

As previously stated, you can create an interface.

IFormatCurrency

2 classes that implement this interface (these are called "concrete
classes")

FixedCurrencyFormatter : IFormatCurrency

PercentCurrencyFormatter : IFormatCurrency

then you create "Factory" that returns a IFormatCurrency

pubic static IFormatCurrency ( string param1 , string param2 , string
param3)
//implement the logic to either return a FixedCurrencyFormatter or a
PercentCurrencyFormatter.


You can also look a the Strategy Pattern
http://www.dofactory.com/Patterns/PatternStrategy.aspx
You would choose the Strategy pattern if you knew your
Entity/BusinessObject
had to have one CurrencyFormatter, and if you wanted to change its
CurrencyFormatter at run time. (or compile time, but at run time also).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top