Click here to Skip to main content
16,007,610 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
GeneralRe: Design Patterns References Pin
geekfromindia18-Jan-08 10:51
geekfromindia18-Jan-08 10:51 
GeneralRe: Design Patterns References Pin
Member 74129825-Jan-08 2:33
Member 74129825-Jan-08 2:33 
GeneralDatabase App Deployment - (Newbie) [modified] Pin
wjtaylor10-Jan-08 13:19
wjtaylor10-Jan-08 13:19 
GeneralRe: Database App Deployment - (Newbie) Pin
VarChar25510-Jan-08 15:42
VarChar25510-Jan-08 15:42 
GeneralRe: Database App Deployment - (Newbie) Pin
wjtaylor11-Jan-08 3:53
wjtaylor11-Jan-08 3:53 
GeneralRe: Database App Deployment - (Newbie) Pin
led mike11-Jan-08 4:56
led mike11-Jan-08 4:56 
GeneralLocalization design question Pin
gnjunge8-Jan-08 22:12
gnjunge8-Jan-08 22:12 
QuestionMutually exclusive property across instances? Pin
Edmundisme8-Jan-08 16:12
Edmundisme8-Jan-08 16:12 
So I have this class, Apple. There can be many instances of Apple, but only one can be my favorite at any given time. So the question is how best to manage a mutually exclusive property on a collection of objects?

Here’s a potential solution but I don’t know if it’s bad form or not (the meat is in the property-set and uses the static FavoriteApple field).

<br />
public class Apple<br />
{<br />
   private static Apple FavoriteApple;<br />
<br />
   private bool isFavorite;<br />
<br />
   public bool IsFavorite<br />
   {<br />
      get { return this.isFavorite; }<br />
<br />
      set<br />
      {<br />
         // Set this instance's isFavorite field.<br />
         this.isFavorite = value;<br />
<br />
         if (value == true)<br />
         {<br />
            // Set the current favorite apple's IsFavorite property to false.<br />
            Apple.FavoriteApple.IsFavorite = false;<br />
            // Set the static "FavoriteApple" reference to this instance.<br />
            Apple.FavoriteApple = this;<br />
         }<br />
         else<br />
         {<br />
            // If the static "FavoriteApple" reference is pointing to this instance, null it out.<br />
            if (this == Apple.FavoriteApple) Apple.FavoriteApple = null;<br />
         }<br />
      }<br />
   }<br />
}<br />



The question is about the proper use of properties. Is it reasonable to set a property on an instance of a class and have it affect the state of another instance? Intuitively this seems wrong. But how else can you handle the setting of an object property that is by design mutually exclusive amongst all instances of the class?

The most obvious alternative is to leave it to the programmer to somehow enforce the mutual exclusivity. This puts the burden on the programmer and opens up the potential for some serious bugs, but doesn’t do any fishy stuff behind the scenes. Everything is explicit.

Another solution is to remove the "IsFavorite" property from the Apple class all together and us a reference external to the Apple class to keep track of the "favorite" apple. The only problem with this is that if Apple instances are treated differently based on whether or not they are the "Favorite", then the reference to the "Favorite" apple has to be passed around with the Apple instances.

At any rate, the method I've illustrated here works, the only question is does it violate a best practice or coding principle by changing the state of one instance implicitly when changing the state of another instance explicitly.

Thoughts?
AnswerRe: Mutually exclusive property across instances? Pin
Mark Churchill8-Jan-08 18:53
Mark Churchill8-Jan-08 18:53 
GeneralRe: Mutually exclusive property across instances? Pin
Edmundisme9-Jan-08 8:49
Edmundisme9-Jan-08 8:49 
GeneralRe: Mutually exclusive property across instances? Pin
Mark Churchill9-Jan-08 21:15
Mark Churchill9-Jan-08 21:15 
GeneralService Oriented Architecture Pin
ppayal3-Jan-08 7:05
ppayal3-Jan-08 7:05 
GeneralRe: Service Oriented Architecture Pin
Ray Cassick3-Jan-08 7:42
Ray Cassick3-Jan-08 7:42 
GeneralRe: Service Oriented Architecture Pin
Paul Conrad4-Jan-08 11:29
professionalPaul Conrad4-Jan-08 11:29 
GeneralRe: Service Oriented Architecture Pin
VarChar2559-Jan-08 9:04
VarChar2559-Jan-08 9:04 
GeneralSomehow.. Pin
Eytukan18-Jan-08 6:46
Eytukan18-Jan-08 6:46 
Questionhow to make a crawler to fetch a web page in php Pin
Arun.N2-Jan-08 1:41
Arun.N2-Jan-08 1:41 
AnswerRe: how to make a crawler to fetch a web page in php Pin
Pete O'Hanlon2-Jan-08 9:31
mvePete O'Hanlon2-Jan-08 9:31 
AnswerRe: how to make a crawler to fetch a web page in php Pin
Paul Conrad4-Jan-08 11:29
professionalPaul Conrad4-Jan-08 11:29 
AnswerRe: how to make a crawler to fetch a web page in php Pin
Eytukan18-Jan-08 6:47
Eytukan18-Jan-08 6:47 
Questionhow to make a crawler that crawls the webpage and retrieves the stock information from google in php. Pin
ramesh_imayam2-Jan-08 1:12
ramesh_imayam2-Jan-08 1:12 
AnswerRe: how to make a crawler that crawls the webpage and retrieves the stock information from google in php. Pin
Paul Conrad4-Jan-08 11:30
professionalPaul Conrad4-Jan-08 11:30 
Questionhow to separate Pin
netJP12L1-Jan-08 18:13
netJP12L1-Jan-08 18:13 
AnswerRe: how to separate Pin
Matthew Faithfull1-Jan-08 23:38
Matthew Faithfull1-Jan-08 23:38 
QuestionVS 2008 and SQL Server 05 or 08? Pin
camby7528-Dec-07 5:47
camby7528-Dec-07 5:47 

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.