repository Design Pattern

Vo Xuan Tien

High Level Application Design

Good design?

Object creation, persistence, delivery are outside of the core

Good Design

Dependencies point only toward the business logic.

Benefits

  • Swap the heavy MySQL database with a lightweight SQLite3 database.
  • Drop your current MVC framework and replacing it with another, without touching the business logic.
  • Delivering the results of your application through a third party API and not over HTTP.
  • Making all these changes and your tests would still pass.

repository

What?

Connect business logic, factories and persistence.

Repository

When?

Any application has to work with persistence and with some kind of list of items.

Factory

What?

It’s a class that acts as a factory of object instances.

Factory

What?

It will know how to take all the information needed to build an object.

Factory

What?


$buttons = array('image', 'input', 'flash');
foreach($buttons as $b) {
  echo ButtonFactory::createButton($b)->getHtml();
}

Gateways

What?

Offers the connection between the business logic and the database itself.

Gateways

What?

Do the queries on the database and get raw data.

Gateways

What?

Raw data must then be passed to the Factories.

Problems

Duplication by Data Handling

Problems

Duplication by Data Retrieval Logic

Problems

Duplication by Data Persistence Logic

Repository for Data Retrieval

Repository for Data Persistence

Practice

  • Blog application
  • Comments and Posts
  • Comments belong to Posts
  • Persist comments and posts
  • Retrieve comments and posts

Demo

Factory Design Pattern

By Tiến Võ Xuân

Factory Design Pattern

  • 863