who killed object oriented design?

Post on 02-Jul-2015

1.011 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation about Object Oriented Design common anti-patterns, bad habits and how to avoid them.

TRANSCRIPT

Who killedObject Oriented

Design?

Colonel PHPwith a lead pipe in

the study?

Just look around...

Is OOD making programming more

enjoyable?

How many abstract classes have you written lately?

When was the last time that you had a

design meeting?

Which one is your favorite LINQ expression?

Why would you use yield or yield

return?

Primitive Addiction

Be lazy

Make your life easier

Avoid repetition

Use the source Luke

Creating dates is painful?

DateTime.Parse("...")

new DateTime(y, m, d)

On.Jan(1)

On.Aug(3, 2013)

Date arithmetic makes you crazy?

DateTime.Today.AddDays(-3)

3.Days().Ago

5.Days().Span

1.Month().Ago + 5.Days().Span

On.Dec(10) - 2.Months().Span

2.Months().Before(On.Dec(10))

Date from and to in every method?

What’s missing?

Use a DateRange

new DateRange(!!!!!!On.Jul(1),!!!!!!1.Month().Span)

public class DateRange:!!!!!IEnumerable<DateTime>

var r = new DateRange(!!!!!!!!!!!!!!On.Jul(1), !!!!!!!!!!!!!!On.Sep(3)) r.Select(d => ....)

Generates a sequence of dates

public IEnumerator<DateTime> GetEnumerator() { var counter = this.StartDate;

while (counter <= this.EndDate) { yield return counter; counter = counter.AddDays(1); } }

Anemic models

OO mixes behavior and data

Models usually represent data

...and you realize that there is hardly any behavior on these objects, making them little more than bags of getters and setters...

Martin Fowler

Rich vs Anemic

What about SOLID?

Can we justify more classes?

ValidatorsProcessorsRetrieversTranslators

ServicesBuilders

class WorkingShift { public Code { get; set; } public Desc { get; set; }}

Night, day or evening...Who’s responsible?

shift.Code == "NIGHT"

class WorkingShift { public Code { get; set; } public Desc { get; set; } public bool IsNight() { ... }}

class Customer { public IsOfDrivingAge { get; } }

How to decide?

Functionality is Complex

Functionality uses multiple models

Interacting with external service

Is not a core concern

Multiple ways of implementation

Some options

Use extensions

public static class CustomerExtensions{ public static bool ! ! ! ! IsOfDrivingAge(this Customer ...) }

Decorator

View Model

Service Objects

Form Objects

Repository overpopulation

Media tes be tween the domain and data mapping layers using a collection-like interface for accessing domain objects

Martin Fowler

public interface IRepository<T>{ IEnumerable<T> All(); void Insert(T entity); void Delete(T entity);}

Custom queries

class CustomerRepository:! ! ! IRepository<Customer>{ // Implementation interface IEnumerable<Customer> FindByAddr(Address ) }

CustomerRepositoryAddressRepositoryChairRepository

...

IQueryable<T>

public interface IRepository<T>{ IQueryable<T> All(); void Insert(T entity); void Delete(T entity);}

class CustomerExtensions{ public IQueryable<Customer> FindByAddress( this IQueryable<Customer> query, Address address) { query.Where(c => c.Address == address) } }

How many repositories?

Only one generic implementation on top of your favorite ORM

IOC container can instantiate the right one

UnitOfWork can be a factory of

repositories

Having an ORM do we really need a repository?

Testing the testable test is

tested

A test is about behavior

Not implementation

Writing expectations about implementation

makes it brittle

public int ImportantMethod(){ var c1 = _dependency1.Calculate(); var c2 = _dependency2.Adjust(c1); return c2 + 10;}

public void ImportantTest() { // arrange .... _dependency1! ! ! .Expect(d => d.Calculate())! ! ! .Once()! ! ! .AndReturn(50) _dependency2! ! ! .Expect(d => d.Adjust(50))! ! ! .Once()! ! ! .AndReturn(100) // act var actual = sut.ImportantMethod() // assert Assert.That(actual, Is.EqualTo(110))! VerifyAllExpectations()}

public int ImportantMethod(){ var c1 = _dependency1.Calculate(); var c2 = _dependency2.Adjust(c1); return _dependency2.Adjust(c1) + 10;}

public void ImportantTest() { // arrange .... _dependency1! ! ! ! .Stub(d => d.Calculate())! ! ! ! .AndReturn(50) _dependency2! ! ! ! .Stub(d => d.Adjust(50))! ! ! ! .AndReturn(100) // act var actual = sut.ImportantMethod() // assert Assert.That(actual, Is.EqualTo(110))}

Abstraction subtraction

Exercise in groups

Hospital Domain Model

Versions

What the domain looked like

X months ago

DB Driven Development

Model may or may not match the database

Active Record

Transaction Script

Data Mapper

Coding is likeTelling a Story

Leave the code cleaner than you found it

Meaning is KING

Don’t code what you don’t need

Know your collections

Let LINQ do the work for you

Use extension methods to mimic a DSL

Discuss design options

Explore F#

Thank you!

The Smartest tool for lean project management

http://smartviewapp.com

amir@barylko.com

@abarylko

http://bit.ly/abarylkoslides

Photo Credit

• Under http://creativecommons.org/licenses/by/2.5/

• Joe Cheng, DSC_7820-01, http://flic.kr/p/2Zt2u

• Bill Ward, Derek Schin's Trucks 1, http://flic.kr/p/m5L5S

• Jeremy Keith, Roast beef, http://flic.kr/p/TKUz

• Rob Campbell, Field of daisies, http://flic.kr/p/6QJjU4

• Karin Dalziel, The Thinker, http://flic.kr/p/4UYArc

Photo Credit 2• Under https://creativecommons.org/licenses/by-sa/2.0/

• Don LaVange, To Irene, https://flic.kr/p/49ihZX

• Under http://creativecommons.org/licenses/by-sa/3.0/us/

• Derick Bailey, SOLID Motivational Posters, http://bit.ly/17aVaHg

• MGA Roadster 1600 in Chariot Red: http://www.carandclassic.co.uk/car/C414652

• Cone of Uncertainty, http://www.agilenutshell.com/cone_of_uncertainty

• Burning Money, http://en.wikipedia.org/wiki/Money_burning#mediaviewer/File:Burning-money-and-yuanbao-at-the-cemetery-3249.JPG

top related