Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So I've looked at the Builder Pattern, Abstract Interfaces, other design patterns, etc. - and I think I'm over thinking the simplicity behind what I'm trying to do, so I'm asking you guys for some help with either recommending a design pattern I should use, or an architecture style I'm not familiar with that fits my task.

So I have one model that represents a Budget in my code. At a high level, it looks like this:

public class Budget
{
    public int Id { get; set; }

    public List<MonthlySummary> Months { get; set; }

    public float SavingsPriority { get; set; }

    public float DebtPriority { get; set; }

    public List<Savings> SavingsCollection { get; set; }

    public UserProjectionParameters UserProjectionParameters { get; set; }

    public List<Debt> DebtCollection { get; set; }

    public string Name { get; set; }

    public List<Expense> Expenses { get; set; }

    public List<Income> IncomeCollection { get; set; }

    public bool AutoSave { get; set; }

    public decimal AutoSaveAmount { get; set; }

    public FundType AutoSaveType { get; set; }

    public decimal TotalExcess { get; set; }

    public decimal AccountMinimum { get; set; }
}

To go into more detail about some of the properties here shouldn't be necessary, but if you have any questions about those I will fill more out for you guys.

Now, I'm trying to create code that builds one of these things based on a set of BudgetBuildParameters that the user will create and supply. There are going to be multiple types of these parameters. For example, on the sites homepage, there will be an example section where you can quickly see what your numbers look like, so they would be a much simpler set of SampleBudgetBuildParameters then say after a user registers and wants to create a fully filled out Budget using much more information in the DebtBudgetBuildParameters.

Now a lot of these builds are going to be using similar code for certain tasks, but might want to also check the status of a users DebtCollection when formulating a monthly spending report, where as a Budget that only focuses on savings might not want to.

I'd like to reduce code duplication (obviously) as much as possible, but in my head, every way I can think to do this would require using a base BudgetBuilderFactory to return the correct builder to the caller, and then creating say a SimpleBudgetBuilder that inherits from a BudgetBuilder, and put all duplicate code in the BudgetBuilder, and let the SimpleBudgetBuilder handle it's own cases. Problem is, a lot of the unique cases are unique to 2/4 builders, so there will be duplicate code somewhere in there obviously if I did that.

Can anyone think of a better way to either explain a solution to this that may or may not be similar to mine, or a completely different pattern or way of thinking here?

I really appreciate it.

share|improve this question
2  
consider studying about SRP, your design appear to deviate from this principle quite a bit. For the start, I'd probably extract AutoSave related stuff into separate object. Savings and Debt might be worth extracting, too –  gnat Jun 30 '12 at 20:15
1  
Yeah I think I am going to extract related properties into their own objects, however, as far as my design and SRP goes, it definitely does violate it but, I can't seem to think of anything too much better. Any tips that can point me in the right direction? –  Scott Jun 30 '12 at 20:24
1  
well in cases like this I consider extracting the right direction. Until it is done, it is hard to me to see what else could be improved; after it is done I typically find out something else to improve. And so on, and so on, step by step. As you can see, I prefer iterative approach over BDUF –  gnat Jun 30 '12 at 20:30
    
I see, well I will go ahead and get started on re-factoring and maybe I will see certain things pop out at me. In the meantime, if anyone else responds, go for it! Also, why all the down votes? –  Scott Jun 30 '12 at 20:33
add comment

1 Answer

up vote 3 down vote accepted

Just looking at that giganto-class, I think you need to look at Strategy pattern.

software design pattern, whereby an algorithm's behaviour can be selected at runtime. Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it...

For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, and/or other discriminating factors. These factors are not known for each case until run-time, and may require radically different validation to be performed. The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication.

The essential requirement in the programming language is the ability to store a reference to some code in a data structure and retrieve it. This can be achieved by mechanisms such as the native function pointer, the first-class function, classes or class instances in object-oriented programming languages, or accessing the language implementation's internal storage of code via reflection...

share|improve this answer
    
Seems like it would work. I could probably encapsulate similar sub executions together. I like the simplicity of it though. –  Scott Jul 2 '12 at 0:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.