Skip to main content

C# Builder Pattern

So I have been learning C# lately and trying to apply some concepts from Joshua Bloch's Effective Java 2.

The builder pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PaymentLibrary
{
    public sealed class CardType
    {
        public string Id { get; private set; }
        public string Name { get; private set; }
        public string IssueFlag { get; private set; }
        public IList<int> Lengths { get; private set; }
        public IList<string> Prefixes { get; private set; }

        private CardType()
        {
        }

        public sealed class Builder
        {
            private string id;
            private string name;
            private string issueFlag;
            private IList<int> lengths = new List<int>();
            private IList<string> prefixes = new List<string>();

            public Builder WithId(string id)
            {
                this.id = id;
                return this;
            }

            public Builder WithName(string name)
            {
                this.name = name;
                return this;
            }

            public Builder WithLength(int length)
            {
                this.lengths.Add(length);
                return this;
            }

            public Builder WithIssueFlag(string issueFlag)
            {
                this.issueFlag = issueFlag;
                return this;
            }

            public Builder WithPrefix(string prefix)
            {
                this.prefixes.Add(prefix);
                return this;
            }

            public CardType Build()
            {
                CardType cardType = new CardType();
                cardType.Id = id;
                cardType.Name = name;
                cardType.IssueFlag = issueFlag;
                cardType.Lengths = lengths;
                cardType.Prefixes = prefixes;
                return cardType;
            }
        }
    }
}

Usage:
CardType cardType = new CardType.Builder()
                                    .WithId("MC")
                                    .WithName("Mastercard")
                                    .WithIssueFlag("N")
                                    .WithPrefix("49")
                                    .WithPrefix("56")
                                    .WithLength(16)
                                    .WithLength(18)
                                    .WithLength(19)
                                    .Build();

Comments

Popular posts from this blog

Architecture Complexity

Here are the items to consider: Coding to an interface Service Oriented Architecture Automated Testing Domain Driven Design Custom Data Access Layer Layered architecture Complexity is relatively equal the number of lines of code. Note that complexity is not bad. It must be justified.

Importance of daily meetings

Why are daily meetings important? Yes I know what you are thinking, to check if the schedule is on target, to spot showstoppers, roadblocks and whatnots. But aside from those, there are things that make daily meetings rewarding. Let’s face it, some people (including me) find it hard oftentimes to achieve a certain level of focus at work especially when multitasking is inevitable. Meetings will remind us of our commitments infront of our peers. Since most of us will be ashamed of not making any progress, it will trigger ourselves to do something about it. In my opinion, having daily meetings typically before the shift starts will help a lot of peers suffering from bad habits - procrastination. It is one way of invigorating those who have not found their way out yet. “Keep each other in the zone, what are teammates for?”