What design pattern should I use?

I have a studentadministration application. Now I have to implement a new part of this application that gives advise (in the style of a text) depending on the grades of the student. So we have 1 "algorithm" and 4(for the moment) different texts(variations) as output).

What is the best pattern in this case? I personally think about using the bridge pattern so that I can put the algorithm in the abstraction class and put the different texts as concrete implementors.

-------------Problems Reply------------

I think you're over engineering this.

This is a pretty simple search algorithm problem. Your domain consists of Students (who want advice), Pieces of Advice (who contain knowledge), and a Provider of some kind, which will contain a search algorithm to help the student find advice.

Using the following, I can simply change the search algorithm at any time to suit my needs. I can either create an entirely new search, or modify the one I already have. In addition, I could create a search that works off of a database instead of a list of advice. I can add new criteria by updating my StudentAdvice model and searching for that advice within the provider (these are two distinct changes, so it does not violate the Open/Closed principle).

class Program
{
static void Main(string[] args)
{
var advices = new[]
{
new StudentAdvice{ Grade = 60, Advice = "Talk to your professor." },
new StudentAdvice{ Grade = 70, Advice = "Spend more time studing." },
new StudentAdvice{ Grade = 80, Advice = "Spend even more time studing." },
new StudentAdvice{ Grade = 90, Advice = "You're doing great, almost there!" },
new StudentAdvice{ Grade = 100, Advice = "Perfect!" },
};

IStudentAdviceProvider adviceProvider = new GradeBasedAdviceProvider(advices);

var student = new Student { Name = "Jim", Grade = 80 };

foreach(var advice in adviceProvider.GetAdvice(student))
{
Console.WriteLine(advice.Advice);
}
}
}

public interface IStudentAdviceProvider
{
IEnumerable<StudentAdvice> GetAdvice(Student student);
}

public class GradeBasedAdviceProvider : IStudentAdviceProvider
{
private readonly IEnumerable<StudentAdvice> advices;

public GradeBasedAdviceProvider(IEnumerable<StudentAdvice> advices)
{
this.advices = advices;
}

public IEnumerable<StudentAdvice> GetAdvice(Student student)
{
// Advice Selection Algorithm
return advices.Where(advice => student.Grade <= advice.Grade).Take(1).ToList();
}
}

public class Student
{
public string Name { get; set; }
public int Grade { get; set; }
}

public class StudentAdvice
{
public int Grade { get; set; }
public string Advice { get; set; }
}

At it's core, this could be used as a strategy pattern. However, I started with the domain, and the pattern emerged itself. Picking patterns first codes you into corners.

Learning design patterns gives us a bunch of tools to use. Learning SOLID design principles allows us to realize when those tools should be used. The following site has some great resources related to OOD.

http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

Seems to me that template method pattern would be the best choice. You have an algorithm in which one step (output) can vary. So I'll do something like this (PHP):

abstract class MyAlgo
{
function executeAlgo()
{
$this->step1();
$this->step2();
$this->output();
}

function step1()
{
...
}

function step2()
{
...
}

abstract function output();
}

class Variation1 extends MyAlgo
{
function output()
{
...
}
}

class Variation2 extends MyAlgo
{
function output()
{
...
}
}

Category:design patterns Views:0 Time:2011-11-08

Related post

  • How do I create a Class using the Singleton Design Pattern in Ruby? 2008-08-07

    The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object instance. Although I know how to code the singleton pattern in C++ and Java, I was wondering if anyone know how to implement it in Ruby? -------

  • Do you use design patterns? 2008-08-14

    What's the penetration of design patterns in the real world? Do you use them in your day to day job - discussing how and where to apply them with your coworkers - or do they remain more of an academic concept? Do they actually provide actual value to

  • Design pattern for parsing binary file data and storing in a database 2008-08-14

    Does anybody recommend a design pattern for taking a binary data file, parsing parts of it into objects and storing the resultant data into a database? I think a similar pattern could be used for taking an XML or tab-delimited file and parse it into

  • Can you really build a fast word processor with GoF Design Patterns? 2008-08-19

    The Gang of Four's Design Patterns uses a word processor as an example for at least a few of their patterns, particularly Composite and Flyweight. Other than by using C or C++, could you really use those patterns and the object-oriented overhead they

  • What Are Some Examples of Design Pattern Implementations Using JavaScript? 2008-08-23

    I'm a moderately skilled programmer using JavaScript but I am no guru. I know you can do some pretty powerful things with it, I just haven't seen much other than fairly basic DOM manipulation. I'm wondering if people could provide some examples of tr

  • Best book/resource for learning Java design patterns? 2008-08-24

    I know the MVC design pattern but would be a little unclear about other Java design patterns, so I was just wondering if there's any useful books or other resources you could recommend to get up to speed with these patterns? --------------Solutions--

  • Beyond design patterns? 2008-08-25

    For the past 10 years or so there have been a smattering of articles and papers referencing Christopher Alexander's newer work "The Nature of Order" and how it can be applied to software. Unfortunately, the only works I can find are from James Coplie

  • Abstract Factory Design Pattern 2008-08-26

    I'm working on an internal project for my company, and part of the project is to be able to parse various "Tasks" from an XML file into a collection of tasks to be ran later. Because each type of Task has a multitude of different associated fields, I

  • What is a good online resource for css 'design patterns'? 2008-08-30

    Can anyone out there recommend a good online resource for CSS 'design patterns'? I know design patterns in a software context usually refer to OO based design patterns, but I mean design patterns in the broader sense of the term: i.e. common, clean s

  • What are some real life examples of Design Patterns used in software 2008-08-30

    I'm reading through head first design patterns at the moment and while the book is excellent I also would like to see how these are actually used in the real world. If you know of a good example of design pattern usage (preferably in a OSS program so

  • Data Validation Design Patterns 2008-09-05

    If I have a collection of database tables (in an Access file, for example) and need to validate each table in this collection against a rule set that has both common rules across all tables as well as individual rules specific to one or a subset of t

  • MS-Access design pattern for last value for a grouping 2008-09-06

    It's common to have a table where for example the the fields are account, value, and time. What's the best design pattern for retrieving the last value for each account? Unfortunately the last keyword in a grouping gives you the last physical record

  • Design Pattern for Undo Engine 2008-09-08

    I'm writing a structural modeling tool for a civil enginering application. I have one huge model class representing the entire building, which include collections of nodes, line elements, loads, etc. which are also custom classes. I have already code

  • What is the design pattern for processing command line arguments 2008-09-10

    If you are writing a program that is executable from the command line, you often want to offer the user several options or flags, along with possibly more than one argument. I have stumbled my way through this many times, but is there some sort of de

  • Do you know any examples of a PAC design pattern? 2008-09-16

    Can anyone point to any websites or web applications that are using the Presentation-Abstraction-Control design pattern rather than MVC? Desktop applications are easy to find like this (e.g.; GIMP) but I'm looking for something on the web. ----------

  • design patterns or best practices for shell scripts 2008-09-17

    Does anyone know of any resources that talk about best practices or design patterns for shell scripts (sh, bash etc...)? --------------Solutions------------- I wrote quite complex shell scripts and my first suggestion is "don't". The reason is that i

  • How do you know when to use design patterns? 2008-09-17

    Anyone can read the GoF book to learn what design patterns are and how to use them, but what is the process for figuring out when a design pattern solves a problem? Does the knowledge of the pattern drive the design, or is there a way to figure out h

  • Appropriate design pattern for an event log parser? 2008-09-18

    Working on a project that parses a log of events, and then updates a model based on properties of those events. I've been pretty lazy about "getting it done" and more concerned about upfront optimization, lean code, and proper design patterns. Mostly

  • What is the most interesting design pattern you've ever met? 2008-09-19

    Most of us have already used the casual patterns such as MVC, strategy, etc. But there must be some unusual solutions to unusual problems, and I'd like to hear about it. --------------Solutions------------- Crash Only Software: http://www.usenix.org/

  • Which Design Pattern is best for Iterative development? 2008-09-23

    Is there such a thing as having the most prefered design pattern for building applications in TDD or the iterative mode? --------------Solutions------------- I think the question could be rewritten so it makes more sense in these words: "Which archit

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.713 (s). 11 q(s)