Created by Ed Blackburn / @ejblackburn
public class Horse {
public string Leg1 { get; set; }
public string Leg2 { get; set; }
public string Leg3 { get; set; }
public string Leg4 { get; set; }
}
public class MoveHorse {
public Gallop(Horse horse, int steps) {
for(step = 0;step > steps;step++) {
horse.Leg1 = "Move Fast";
horse.Leg2 = "Move Fast";
horse.Leg1 = "Put on ground";
horse.Leg2 = "Put on Ground";
horse.Leg3 = "Move Fast";
horse.Leg4 = "Move Fast";
}
}
public Cantor(Horse horse, int steps) {
for(step = 0;step > steps;step++) {
horse.Leg1 = "Move slow";
horse.Leg1 = "Put on ground";
horse.Leg2 = "Move slow";
horse.Leg2 = "Put on ground";
horse.Leg3 = "Move slow";
horse.Leg3 = "Put on ground";
horse.Leg4 = "Move slow";
horse.Leg4 = "Put on ground";
}
}
.
.
}
The algorithm is wrong. This can get complicated quickly and is not intuitive.
class Horse {
private Leg leg1;
private Leg leg2;
private Leg leg3;
private Leg leg4;
.
.
void Gallop() {..}
void Cantor() {..}
}
Immutable types. Enforce invariants: a name is not a string. A name can not be null or empty, can not begin or end with white space.
class Name {
ctor(string name) {
throw if null
name = name.trim()
throw if empty
}
Equals(Name) {..}
}
class Money {
ctor(double value, Currency currency){..}
Equals(Money other){
return other.value == this.value && other.currency == this.currency;
}
}
var five_pounds = new Money(5, new GBP());
var ten_pounds = new Money(10, new GBP());
class Note {
ctor(SerialNumber serial_number){..}
Equals(Note other){
return other.serial_number == this.serial_number;
}
}
Represent two different things. Both use Guids..
class AccountId {
ctor(string id){..}
Equals(AccountId){..}
}
class AuthToken {
ctor(string id){..}
Equals(AuthToken){..}
}
..a category of objects which seem to have an identity, which remains the same throughout the states of the software. For these objects it is not the attributes which matter, but a thread of continuity and identity, which spans the life of a system and can extend beyond it. Such objects are called Entities
- Eric Evans
class Product {
private ProductId;
Publish(){..}
AddOffer(Offer){..}
.
.
Equals(Product){
return Product.ProductId == this.ProductId;
}
}