Dofactory.com
Dofactory.com
 Back to list
Views:   7.5K
Replies:  1
Archived

What design pattern shall I use

Hi,

I'm studying pattern and i want implement a class that build Charts.

In my case, charts could be made by legend, a description and a graphical representation(bar chart, line chart, pie chart). But I could create a chart without a legend, or without a description. So i have some variations to create the chart.

What pattern is better in these case ? I try the builder pattern but i cant make a concret class for all the combination that i have.

Sorry for the bad english.

I need some help, please.

Thanks,

Filipe



Filipe Tatarli, Nov 16, 2011
Reply 1
Some indirect inputs that may be useful


Basically a chart is composed of
 -  An X- axis
 -  A Y- axis
 -  Or No Axis (Pie chart and others)
 -   Has a Title
 -   Has Legends
 -   Different renderings (bar, column, pie, candle-stick etc).
 -   Has model or data to bind to
 -   et el

If you analyze this you may observe that the only changing part in this is the rendering, i.e. a line chart, a pie chart etc rest other things are composable or in other words you may thing it this way...

A chart  has
-   a data to bind to
-   Render as

And is decorated with a 
- Title
- Axis
- Legends 
etc.


So, you can create various decorators for these elements and compose your main chart with these decorators at runtime and as and when required.

Approach 1 - Decorator pattern

The higher level API could be something like this

// Create a chart with legends
Chart lineChart = new LineChart();
ln.DataSource =  "Your datasource here";
lineChart.Render();   // Renders the linechart without legend

LegendsChart  ln = new LegendsChart(lineChart);

ln.Render();   // Renders the line chart with legends as well

Refer Decorator patterns here  http://www.dofactory.com/Patterns/PatternDecorator.aspx
You can add more decorators as needed without modifying your existing code


Approach 2 - Fluent API
It will look something like this
Chart lineChart  = new Linechart().ShowLegend();
linChartDataSource = "Your datasource here";
lineChart.Render();

lineChart.AddBorder();
lineChart.Render();

A good example of fluent interface can be found here.. http://weblogs.asp.net/jgalloway/archive/2006/12/06/a-simple-example-of-a-fluent-interface.aspx



NOTE:  This is just couple of  approaches, you an find numerous other approaches as well
 
Rajesh Pillai, Nov 21, 2011
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.