1

I have a class I want to implement, but there is a lot of conditional logic so I decided to implement a builder(like) pattern. These setters will pretty much only be flags to set

private class MockBuilder
{
  private bool _flag1 = false;
  private bool _flag2 = false;
  // arbitrarily long amount of flags here

  public MockBuilder()
  {
  }
  public void DoFlag1(bool value = true)
  {
      _flag1 = value;
  }
  public void DoFlag2(bool value = true)
  {
      _flag2 = value;
  }
  public builtObject Build()
  {
    // Build with current flags
  }
}

Long story short, I looked into default params for setters and some questions like: c#: getter/setter and wondering if there was a more "C#" way to do this, something along the lines of

public bool flag1 { get; set(value = true); }

The end goal being able to call the builder like so

var object = new MockBuilder();
builder
  .flag1()
  .flag3()
// etc...
  .generate();

Does C# implement some sort of default value for setters? Is this a good use of the builder pattern? Should I be using a different pattern all together?

Community
  • 1
  • 1
  • Take a look at this question: http://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value .. – txtechhelp Mar 09 '16 at 01:38
  • By default, I want the flag set to false originally. Then when triggered by calling the "setter" I want it to set it to true unless specifically noted not to be. If that makes sense. So tl;dr I want the setter to have a default param in a more C# way if possible. If not, is this the best way? – Dylan Walseth Mar 09 '16 at 01:44
  • I have to wonder if this is one of those questions where what is asked is "how to solve x" where x is perhaps not the best way to solve the Problem. Dylan, what is the real problem you are trying to solve? How many flags (32/64/more)? There are many ways to combine flags/objects/attributes to describe a state. More information would help provide a solution for the problem, and perhaps not focus on the algorithm you might be proposing (which could very well be interesting). – Kory Gill Mar 09 '16 at 01:45
  • Are you sure a bitmask is not want you want? Bits you don't set are 0. Those you do, become 1. – Kory Gill Mar 09 '16 at 01:46
  • This is a pattern I would more or less like to implement at my work's code base, we have more than a few functions which have 8+ params used to define the type of behavior a certain class should behave. I.e. doThisThing(bool doThisToo, bool andThis, bool whileYoureAtIt ... ) The logic is very conditional and unfortunately I don't think there will be an opportunity to implement a better overall design as this is quite a large application and is not expected to change for some time. – Dylan Walseth Mar 09 '16 at 01:49
  • Hmm... bitmasks... that's actually a really good idea (forgot about those)... I think you just made my issue a non-issue :) Though the original first question still stands, is it possible to do what I'm asking in a more "C#" way... as generic as that sounds lol – Dylan Walseth Mar 09 '16 at 01:52
  • I'm not sure this helps with the question, but C# 6 did add a way to specify default values for auto properties... see http://stackoverflow.com/questions/40730/how-do-you-give-a-c-sharp-auto-property-a-default-value?lq=1 for an example. – Shawn Rakowski Mar 09 '16 at 01:58
  • @ShawnRakowski That isn't the behavior I was looking for :/ . I wanted to have a default param for the setter so I could just call the flag... as weird as that sounds and as I look back, I realize it was kind of silly of me to think of doing something like that as it really doesn't make a lot of sense... As Kory put it though, I should just be using bitmasks aka. flag enums – Dylan Walseth Mar 09 '16 at 02:03
  • @DylanWalseth ahh, that makes much more sense now :) – Shawn Rakowski Mar 09 '16 at 02:39

1 Answers1

1

Use a bitmask like so:

public enum ThingsToDo
{
    // use powers of 2!
    DoThis          = 1 << 0,
    DoThat          = 1 << 1,
    WhileYouAreAtIt = 1 << 2,
    AndFinally      = 1 << 3
}
class Program
{
    static void Main(string[] args)
    {
        var First = ThingsToDo.DoThat | ThingsToDo.DoThis;
        var Next = ThingsToDo.WhileYouAreAtIt | ThingsToDo.AndFinally;
        // ...
        if ((First & ThingsToDo.DoThis) == ThingsToDo.DoThis) {
            // do this 
        }

        if ((Next & ThingsToDo.WhileYouAreAtIt) == ThingsToDo.WhileYouAreAtIt)
        {
            // do while you are at it
        }
    }
}
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • I've actually been on SO/SE for over 2 years, make an account a year ago and mostly just lurk, this is only my 2nd question ever asked. Thanks for the help though :) – Dylan Walseth Mar 09 '16 at 02:32