Last active
April 16, 2018 14:45
-
-
Save Zabaa/585577b4987a9feace8a08f451126e2b to your computer and use it in GitHub Desktop.
Fluent Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace FunWithPatterns.Builder | |
{ | |
public class ApplicationBuilder | |
{ | |
private string _name; | |
private string _sureName; | |
private DateTime _birthDate; | |
private string _scholarship; | |
private IList<RepetentClass> _repetentClasses = new List<RepetentClass>(); | |
private decimal _repetentCost; | |
private bool _dormitoryAssignment; | |
private string _dormitoryName; | |
private string _dormitoryAddress; | |
public ApplicationBuilder CreateApplication(string name, string sureName, DateTime birthDate) | |
{ | |
_name = name; | |
_sureName = sureName; | |
_birthDate = birthDate; | |
return this; | |
} | |
public ApplicationBuilder WithScholarship(string scholarship) | |
{ | |
_scholarship = scholarship; | |
return this; | |
} | |
public ApplicationBuilder WithRepetition(string className, int theoryHours, int practicalHours, decimal repetentCost) | |
{ | |
if (string.IsNullOrEmpty(className)) | |
throw new ArgumentNullException(nameof(className)); | |
_repetentClasses.Add(new RepetentClass | |
{ | |
ClassName = className, | |
TheoryHours = theoryHours, | |
PracticalHours = practicalHours | |
}); | |
_repetentCost += repetentCost; | |
return this; | |
} | |
public ApplicationBuilder WithDormitoryAssignment(string dormitoryName, string dormitoryAddress) | |
{ | |
_dormitoryAssignment = true; | |
_dormitoryName = dormitoryName; | |
_dormitoryAddress = dormitoryAddress; | |
return this; | |
} | |
public RecruitmentApplication Build() | |
{ | |
var application = new RecruitmentApplication(); | |
application.Name = _name; | |
application.SureName = _sureName; | |
application.BirthDate = _birthDate; | |
application.Scholarship = _scholarship; | |
application.RepetentClasses = _repetentClasses; | |
application.RepetentCost = _repetentCost; | |
application.DormitoryAssignment = _dormitoryAssignment; | |
application.DormitoryName = _dormitoryName; | |
application.DormitoryAddress = _dormitoryAddress; | |
return application; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace FunWithPatterns.Builder | |
{ | |
class FirstApplicationBuilder | |
{ | |
private RecruitmentApplication _application; | |
public FirstApplicationBuilder CreateApplication(string name, string sureName, DateTime birthDate) | |
{ | |
_application = new RecruitmentApplication | |
{ | |
Name = name, | |
SureName = sureName, | |
BirthDate = birthDate, | |
RepetentClasses = new List<RepetentClass>() | |
}; | |
return this; | |
} | |
public FirstApplicationBuilder WithScholarship(string scholarship) | |
{ | |
_application.Scholarship = scholarship; | |
return this; | |
} | |
public FirstApplicationBuilder WithRepetition(string className, int theoryHours, int practicalHours, decimal repetentCost) | |
{ | |
if (string.IsNullOrEmpty(className)) | |
throw new ArgumentNullException(nameof(className)); | |
_application.RepetentClasses.Add(new RepetentClass | |
{ | |
ClassName = className, | |
TheoryHours = theoryHours, | |
PracticalHours = practicalHours | |
}); | |
_application.RepetentCost += repetentCost; | |
return this; | |
} | |
public FirstApplicationBuilder WithDormitoryAssignment(string dormitoryName, string dormitoryAddress) | |
{ | |
_application.DormitoryAssignment = true; | |
_application.DormitoryName = dormitoryName; | |
_application.DormitoryAddress = dormitoryAddress; | |
return this; | |
} | |
public RecruitmentApplication Build() | |
{ | |
return _application; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
var builder = new ApplicationBuilder(); | |
var application = builder | |
.CreateApplication("Zaba", "Zul", new DateTime(1990, 9, 2)) | |
.WithDormitoryAssignment("Filon", "Kilinskiego 11") | |
.WithRepetition("Electronics", 10, 10, 550) | |
.WithRepetition("Object Oriented Programming", 20, 15, 850) | |
.Build(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace FunWithPatterns.Builder | |
{ | |
public enum StudyType | |
{ | |
FullTimeStudies, | |
ExtramuralStudies | |
} | |
public class RecruitmentApplication | |
{ | |
public string Name { get; set; } | |
public string SureName { get; set; } | |
public DateTime BirthDate { get; set; } | |
public string FieldOfStudy { get; set; } | |
public StudyType StudyType { get; set; } | |
#region Freshman | |
public string Scholarship { get; set; } | |
#endregion | |
#region Repetition | |
public string IndexNumber { get; set; } | |
public IList<RepetentClass> RepetentClasses { get; set; } | |
public decimal RepetentCost { get; set; } | |
#endregion | |
#region Dormitory assignment | |
public bool DormitoryAssignment { get; set; } | |
public string DormitoryName { get; set; } | |
public string DormitoryAddress { get; set; } | |
#endregion | |
} | |
public class RepetentClass | |
{ | |
public string ClassName { get; set; } | |
public int TheoryHours { get; set; } | |
public int PracticalHours { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment