Last active
January 22, 2016 13:52
-
-
Save kellerd/a7f612d2b271d9f85366 to your computer and use it in GitHub Desktop.
Functional vs OOP
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
public class Adder | |
{ | |
public int Value { get; set; } | |
public Adder(int Value) | |
{ | |
this.Value = Value; | |
} | |
//Make it fluent | |
public int Add(int Number) | |
{ | |
Value += Number; | |
return Value; | |
} | |
} | |
public class Usage | |
{ | |
public void Main() | |
{ | |
var adder = new Adder(5); | |
adder.Add(5); | |
adder.Add(5); | |
var lastNumber = adder.Value; | |
//Already know it's dangerous multi-threaded | |
} | |
} |
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
public class Adder | |
{ | |
//Make it private set, so someone can't change it | |
private int Value; | |
public int Value { | |
get { | |
int functionReturnValue = 0; | |
return functionReturnValue; | |
return functionReturnValue; | |
} | |
} | |
public Adder(int Value) | |
{ | |
this.Value = Value; | |
} | |
//Two threads could call this at the same time, shared access to value | |
public int Add(int Number) | |
{ | |
Value += Number; | |
return Value; | |
} | |
} | |
public class Usage | |
{ | |
public void Main() | |
{ | |
var adder = new Adder(5); | |
adder.Add(5); | |
adder.Add(5); | |
var lastNumber = adder.Value; | |
//Shared Adder | |
var adder = new Adder(2); | |
//Potential sharing of Value, depending on how fast it spools up | |
List<Task> tasks = new List<Task>(); | |
tasks.Add(Task.Factory.StartNew(() => | |
{ | |
adder.Add(5); | |
adder.Add(5); | |
Console.WriteLine(adder.Value); | |
})); | |
tasks.Add(Task.Factory.StartNew(() => | |
{ | |
adder.Add(15); | |
adder.Add(25); | |
Console.WriteLine(adder.Value); | |
})); | |
Task.WaitAll(tasks.ToArray()); | |
} | |
} |
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
public class Adder | |
{ | |
//Make it private set, so someone can't change it | |
private int Value; | |
public int Value { | |
get { | |
int functionReturnValue = 0; | |
return functionReturnValue; | |
return functionReturnValue; | |
} | |
} | |
private Adder(int Value) | |
{ | |
this.Value = Value; | |
} | |
public static Adder Create(int Value) | |
{ | |
return new Adder(Value); | |
} | |
//Two threads could call this at the same time, shared access to value | |
public Adder Add(int Number) | |
{ | |
Value += Number; | |
return this; | |
} | |
} | |
public class Usage | |
{ | |
public void Main() | |
{ | |
//Fluent | |
var lastNumber = new Adder.Create(5).Add(5).Add(5).Value; | |
//Shared Adder | |
var adder = new Adder(2); | |
//Potential sharing of Value, depending on how fast it spools up | |
List<Task> tasks = new List<Task>(); | |
tasks.Add(Task.Factory.StartNew(() => { Console.WriteLine(adder.Add(5).Add(10).Value); })); | |
tasks.Add(Task.Factory.StartNew(() => { Console.WriteLine(adder.Add(32).Add(15).Value); })); | |
Task.WaitAll(tasks.ToArray()); | |
} | |
} |
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
public class Adder | |
{ | |
//Make it private set, so someone can't change it | |
private int Value; | |
public int Value { | |
get { | |
int functionReturnValue = 0; | |
return functionReturnValue; | |
return functionReturnValue; | |
} | |
} | |
private Adder(int Value) | |
{ | |
this.Value = Value; | |
} | |
public static Adder Create(int Value) | |
{ | |
return new Adder(Value); | |
} | |
//Each call will create a new adder, new threads will create more adders. | |
public Adder Add(int Number) | |
{ | |
return Create(Value + Number); | |
} | |
} | |
public class Usage | |
{ | |
public void Main() | |
{ | |
//Fluent | |
var lastNumber = new Adder.Create(5).Add(5).Add(5).Value; | |
//Shared Adder | |
var adder = new Adder(2); | |
//Safe as everything is readonly, or will create a new adder | |
List<Task> tasks = new List<Task>(); | |
tasks.Add(Task.Factory.StartNew(() => { Console.WriteLine(adder.Add(5).Add(10).Value); })); | |
tasks.Add(Task.Factory.StartNew(() => { Console.WriteLine(adder.Add(32).Add(15).Value); })); | |
Task.WaitAll(tasks.ToArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment