@rob_rich
by Rob Richardson
December 9, 2015
Rob Richardson is a software craftsman building web properties in ASP.NET, Node, and Angular. He's a Microsoft MVP, published author, frequent speaker at conferences, user groups, and community events, and a diligent teacher and student of high quality software development. You can find this and other talks on https://robrich.org/presentations and follow him on twitter at @rob_rich.
What's the purpose?
To tell others of your intentions
Agile: Element of least surprise
Programming: Discoverable
Easy (not simple)
"Legacy"
means I don't understand it
To provide the best information for those that follow us
A generic solution to a common problem
What is the problem we're trying to solve?
What is the technique?
What are the pros & cons
A single instance
Problem: expensive initialization
Problem: common state
Solution: private constructor,
public static get instance method
public class TheSingleton {
private static TheSingleton instance = null;
public static TheSingleton GetInstance() {
if (instance == null) {
instance = new TheSingleton();
}
return instance;
}
private TheSingleton() {
}
// ...
}
Pros | Cons |
|
|
A helper class instantiates and configures the real class
Problem: group common initialization
Problem: instantiate derived class, return interface
Solution: method that returns a new instance
public class TheFactory {
public static TheClass GetTheClass() {
TheClass theclass = new TheClass();
// initialize theclass
return theclass;
}
}
public class TheClass {
// ...
}
Pros | Cons |
|
|
A smarter class configures a simpler class
Problem: avoid overloaded constructors
Solution: method that configures the class's property and returns itself
public class TheBuilder {
private TheClass theclass;
public TheBuilder() {
this.theclass = new TheClass();
}
public TheBuilder SetColor(string Color) {
this.theclass.Color = Color;
return this;
}
public TheClass Build() {
return this.theclass;
}
}
public class TheClass {
// ...
}
public void Main() {
TheClass theclass = new TheBuilder().SetColor("green").SetSize(5).Build();
}
Pros | Cons |
|
|
Model - View - Controller
Problem: UI code is hard to test
Solution: Separate routing, computation, and presentation
source: http://www.tonymarston.net/php-mysql/model-view-controller-01.png
Pros | Cons |
|
|
Decouple creating and handling events
Problem: avoid tight coupling between disconnected pieces
Solution: publish events or messages from one class to another
public class ThePublisher {
public event InvoiceData InvoiceShipped;
private ShipInvoice(InvoiceData Data) {
InvoiceShipped(this, Data);
}
}
public class TheConsumer {
private ThePublisher publisher
public void Setup() {
publisher.InvoiceShipped += this.OnInvoiceShipped;
}
public void OnInvoiceShipped(object Sender, InvoiceData Data) {
// ...
}
}
Pros | Cons |
|
|
Generic interface implemented by concrete class
Problem: concrete dependencies make testing hard
Solution: swap in fake dependencies during tests
public interface ITheClass {
public int Add(int X, int Y);
}
public class TheClass : ITheClass {
public int Add(int X, int Y) {
return X + Y;
}
}
// ...
public class MockClass : ITheClass {
public int Add(int X, int Y) {
return 5;
}
}
Pros | Cons |
|
|
Thin veiner around expensive external systems
Problem: Need to contain change around external dependencies
Solution: A gateway class that separates the the external dependency from the rest of the application
source: http://fredwu.me/post/54009567748/datamappify-a-new-take-on-decoupling-domain
public class CustomerRepository {
public Customer GetById(int Id) {
using (var db = new DataContext()) {
return db.Find(Id);
}
}
public void Save(Customer Customer) {
Customer.Modified = DateTime.Now;
using (var db = new DataContext()) {
db.Attach(Customer);
db.SaveChanges();
}
}
}
Pros | Cons |
|
|
Problem: What was this doing?
Solution: Add notes for future developers about why these choices were made
public class ConfusingClass {
public void Iduno(int inData) {
int mutex = new Mutex(101,504,inData);
for(int i = 0; i < 200; i++) {
mutex.Next(i);
}
}
// Count the salamander's weight relative to its height
public void IKnow(int SalamanderSize) {
// 101 is the weight of the object
// 504 is the number of seconds it will run
int mutex = new Mutex(101,504,SalamanderSize);
// 200 is the max speed
for(int i = 0; i < 200; i++) {
mutex.Next(i);
}
}
}
Pros | Cons |
|
|
Gang of Four
wikipedia.org/wiki/Design_Patterns