Dependencies

Where to cut?

Classes should either have state or have dependencies on IO classes, but not both.

Using three class types:

  • Domain entity (point, user)
  • Resource (database, file io)
  • Command object

In pseudo-code:

class Point implements Drawable
    private x, y
    construct(x, y)
    draw(SurfaceInterface surface)

In a worse design:

class Point implements Drawable
    private x, y, surface
    construct(x, y, surface)
    draw()

Or possibly:

class Point implements Drawable
    private x, y, surface
    construct(x, y)
    setSurface(surface)
    draw()

Now Point has three properties of widely different character. Two related to state, one releated to behaviour.

class Rectangle implements Drawable
    internal bottomLeft, topRight
    construct(Point bottomLeft, Point topRight)
    draw(SurfaceInterface surface)

main()
    shapes = [
        new Point(1, 2),
        new Rectangle(new Point(2, 3), new Point(3, 4))
    ]
    surface = new Surface()
    forall shapes as shape
        shape.draw(surface)
class AppConfig
    internal username, password
    construct(username, password)

class App
    internal id, config
    construct(id, config)

class InstallApp
    private db, io
    construct(db, io)
    execute(App app)

But not:

class App
    internal id, config
    construct(id, config)
    install(DatabaseConnection db, FileIO io)
    
main()
    config = new AppConfig("username", "password")
    app = new App(1, config)
    installApp = new InstallApp(
        new DatabaseConnection(),
        new FileIO()
    )
    installApp.execute(app)

Notes

class PointPlotter implements PlotterInterface
    private surface
    construct(SurfaceInterface surface)
    plot(Point point)

class PointPersistance implements PersistanceInterface
    private pers
    construct(PersistanceInterface pers)
    save(Point point)

Point.toXML, .toJSON, .toSQL (column, rows)