Click here to Skip to main content
15,898,134 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
GeneralRe: Design Pattern Seminar in Europe Pin
Pete O'Hanlon14-Aug-07 9:43
mvePete O'Hanlon14-Aug-07 9:43 
GeneralRe: Design Pattern Seminar in Europe Pin
Spithas17-Aug-07 21:47
Spithas17-Aug-07 21:47 
GeneralRe: Design Pattern Seminar in Europe Pin
martin_hughes20-Aug-07 5:50
martin_hughes20-Aug-07 5:50 
AnswerRe: Design Pattern Seminar in Europe Pin
t!bast30-Aug-07 3:22
t!bast30-Aug-07 3:22 
GeneralKeyboard Wedge Barcode Scanner Pin
Brady Kelly7-Aug-07 23:32
Brady Kelly7-Aug-07 23:32 
QuestionChange CSS according to Browser? Pin
chand107-Aug-07 21:30
chand107-Aug-07 21:30 
AnswerRe: Change CSS according to Browser? Pin
Pete O'Hanlon7-Aug-07 21:54
mvePete O'Hanlon7-Aug-07 21:54 
QuestionRefactoring Properties into Classes Pin
Leslie Sanford7-Aug-07 6:52
Leslie Sanford7-Aug-07 6:52 
Say you have an interface:

public interface IWidget
{
    // Methods...
 
    // Properties...
 
    int Count
    {
        get;
        set;
    }
}


Let's pretend that a lot of classes will implement this interface. As a result, each class will have to implement the Count property. The implementation code in each class will be the same, more or less. So it's tempting to do this:

public abstract Widget : IWidget
{
    private int count = 0;
 
    public int Count
    {
        get
        {
            return count;
        }
        set
        {
            count = value;
        }
    }
}


Now instead of implementing the IWidget interface directly, classes derive from the abstract Widget base class.

Let's say, however, that derived classes need to know when Count changes; they may need to do some house keeping. We could make Count virtual so that derived classes could do this:

public class MyWidget : Widget
{
    public override int Count
    {
        get
        {
            return base.Count;
        }
        set
        {
            base.Count = value;
 
            // Do stuff based on new count value...
        }
    }
}


I've posted about this in the past. I don't like this approach because it seems to introduces an ambiguity. When overriding a virtual method or property, when should you call the base class version? And should the call be at the beginning or at the end of the virtual method or property?

But I'm not really interested in delving into this issue again, though feel free to comment.

The next step is to factor out the Count property into its own class:

public class Counter
{
    private int count = 0;
 
    public event EventHandler CountChanged;
 
    // ...
 
    public int Count
    {
        get
        {
            return count;
        }
        set
        {
            if(count < 0)
            {
                throw new ArgumentOutOfRange("Count");
            }
 
            count = value;
 
            OnCountChanged(EventArgs.Empty);
        }
    }
}


Instead of Count being a property of the IWidget interface, it becomes an object that is passed to instances of IWidget derived classes. Derived classes hook up to the CountChanged event and do whatever house keeping is necessary when the event is raised.

Counter myCounter = new Counter();
 
IWidget widget1 = new MyFirstWidget(myCounter);
IWidget widget2 = new MySecondWidget(myCounter);


I need to point out that in the scenario I've described above, instances of classes derived from IWidget will share the same count value and will be treated as a group of objects. So instead of iterating through a group of objects changing the property value on each one, you simply change the value of one object which in turn raises an event to notify its observers.

Instead of:

foreach(IWidget widget in widgets)
{
    widget.Count++;
}


You have:

myCounter.Count++;


This post has gotten rather long, but it's been a kind of chronicle of the process I went through. I finally arrived at the point where I factored out properties whose values are shared across several objects into a class of its own. This allows several objects to share the same property. I'm posting this more to get feedback and as a general sanity check.
AnswerRe: Refactoring Properties into Classes Pin
Luc Pattyn7-Aug-07 7:43
sitebuilderLuc Pattyn7-Aug-07 7:43 
GeneralRe: Refactoring Properties into Classes Pin
Leslie Sanford7-Aug-07 8:00
Leslie Sanford7-Aug-07 8:00 
GeneralRe: Refactoring Properties into Classes Pin
Luc Pattyn7-Aug-07 14:34
sitebuilderLuc Pattyn7-Aug-07 14:34 
AnswerRe: Refactoring Properties into Classes Pin
Pete O'Hanlon7-Aug-07 22:00
mvePete O'Hanlon7-Aug-07 22:00 
GeneralRe: Refactoring Properties into Classes Pin
Heavy Storm15-Aug-07 8:11
Heavy Storm15-Aug-07 8:11 
AnswerRe: Refactoring Properties into Classes Pin
originSH7-Aug-07 23:05
originSH7-Aug-07 23:05 
GeneralOff-site Backup Service [modified] Pin
Brady Kelly7-Aug-07 4:52
Brady Kelly7-Aug-07 4:52 
GeneralRe: Off-site Backup Service Pin
led mike7-Aug-07 5:37
led mike7-Aug-07 5:37 
GeneralRe: Off-site Backup Service Pin
Luc Pattyn7-Aug-07 5:41
sitebuilderLuc Pattyn7-Aug-07 5:41 
GeneralRe: Off-site Backup Service Pin
Brady Kelly7-Aug-07 5:45
Brady Kelly7-Aug-07 5:45 
GeneralRe: Off-site Backup Service Pin
led mike7-Aug-07 6:02
led mike7-Aug-07 6:02 
GeneralRe: Off-site Backup Service Pin
Brady Kelly7-Aug-07 6:14
Brady Kelly7-Aug-07 6:14 
GeneralRe: Off-site Backup Service Pin
led mike7-Aug-07 6:34
led mike7-Aug-07 6:34 
GeneralRe: Off-site Backup Service Pin
Brady Kelly7-Aug-07 22:18
Brady Kelly7-Aug-07 22:18 
QuestionPOS discount headache Pin
dazfuller6-Aug-07 4:29
dazfuller6-Aug-07 4:29 
AnswerRe: POS discount headache Pin
led mike6-Aug-07 4:57
led mike6-Aug-07 4:57 
GeneralRe: POS discount headache Pin
dazfuller6-Aug-07 5:23
dazfuller6-Aug-07 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.