Builder pattern

The builder pattern is used when you want to create the same object different in certain circumstances.
A nice quote found on the internet:

The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects

A nice and useable example of the builder pattern

public class EmailBuilder
        private readonly List _attachments;
        private readonly List _toAddresses;
        private string _body;
        private string _subject;

        public EmailBuilder(params string[] toAddresses)
            _attachments = new List();
            _toAddresses = new List();

        public EmailBuilder WithAttachments(params FileInfo[] files)
            return this;

        public EmailBuilder WithSubject(string subject)
            _subject = subject;
            return this;

        public EmailBuilder WithBody(string body)
            _body = body;
            return this;

        public Email Build()
            return new Email
                Attachments = _attachments,
                Body = _body,
                Subject = _subject,
                To = _toAddresses

Example of usage of the builder class

            var email = new EmailBuilder(_toAdresses)




Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!