Download - Modern programozás

Transcript
Page 1: Modern programozás

Modern Programozás

Jenei Attila 2013. !

www.attilajenei.com

Page 2: Modern programozás

Strukturált programozás

Page 3: Modern programozás

Strukturált programozás

• Eljárásokra bontás

• Kisebb feladatok

• Procedurális programozás

Page 4: Modern programozás

Mikor használjuk

• Objektum orientált programozás

• Közös részek

• Alrészek

• Ismétlődően futó részek

Page 5: Modern programozás

Űrlap megjelenítése

LINEÁRIS STRUKTURÁLT

ŰrlapMező #1Mező #2Gomb #1Gomb #2

Űrlap

Mező #1

Mező #2

Gomb #1

Gomb #2

Page 6: Modern programozás

Példaecho $form->render();…function render(){ $result = ‘<form name=“‘ . $this->_escape($this->name) . ”>’;

foreach ($this->elements as $element) { $result .= $element->render(); } return $result . “</form>”; }

Page 7: Modern programozás

Előnyök

• Áttekinthetőbb forrás

• Kisebb fókusz

• Saját névtér

Page 8: Modern programozás

Modularitás

Page 9: Modern programozás

Modularitás

• Illeszthető komponensek

• Újrafelhasználhatóság

• Cserélhető komponensek

Page 10: Modern programozás

Modularitás

• Illeszthető komponensek

• Újrafelhasználhatóság

• Cserélhető komponensek

Page 11: Modern programozás

Teljes modularitás

• Osztályhivatkozások elhagyása

• Singletonok mellőzése

• Dinamikus táblanevek (query-k)

• Kulcsok

Page 12: Modern programozás

Kulcsok

• Gyártók

• Osztálynevek

• Szükség esetén

• Megosztott vagy új példány

• Kezelő(k)

Page 13: Modern programozás

Példa

$user = new User; !

$path = Config::get(‘ViewPath’);

Page 14: Modern programozás

Példa

$user = clone $services->get(‘Model\User’); !

$path = $services->get(‘MyConfig’)->get(‘ViewPath’);

Page 15: Modern programozás

Beállítás példaarray( ‘invokables’ => array( ‘Model\User’ => ‘Project\Model\User’ ), ‘factories’ => array( ‘Model\User\Table’ => function ($sm) { return \Project\Model\User\Table( $sm->get(‘Model\User\TableGateway’)); }, ‘Model\User\TableGateway’ => function ($sm) { return \Project\Model\User\TableGateway( $sm->get(‘Zend\Db\Adapter\Adapter’), ‘user’, $sm->get(‘Model\User’)); }, ‘Zend\Db\Adapter\Adapter’ => ‘Zend\Db\Adapter\AdapterServiceFactory’, ),);

Page 16: Modern programozás

Egyszerű Service Managerpublic function get($name){ if (isset($this->instances[$name]) { return $this->instances[$name]; }

if (isset($this->invokables[$name])) { $className = $this->invokables[$name]; if (class_exists($className)) { $instance = new $className; } else { throw new \Exception(‘Ismeretlen osztály: ’ . $className); } }

Page 17: Modern programozás

Egyszerű Service Manager else if (isset($this->factories[$name]) { $factory = $this->factories[$name]; if ($factory instanceof FactoryInterface) { $factory = array($factory, ‘createService’); }

if (is_callable($factory) { $instance = call_user_func($factory, $this, $name); } else { throw new \Exception(‘Hibás gyártó: ’ . $name); } }

Page 18: Modern programozás

Egyszerű Service Manager else { throw new \Exception(‘Ismeretlen kulcs: ’ . $name); }

if ($instance) { $this->instances[$name] = $instance; return $instance; }

throw new \Exception(‘Nincs példány: ’ . $name); }

Page 19: Modern programozás

Objektum Orientáltság

Page 20: Modern programozás

Maximalizálás

• Adatbázis független

• Ismeretlen “külvilág”

• Saját feladat

• Kiszervezés

Page 21: Modern programozás

Entitás

• Információ

• ≠ adatbázis bejegyzés

• Futtatás helye is

Page 22: Modern programozás

Különbség

Adatbázis bejegyzés képviselete objektumként.

Objektumok tárolása adatbázisban.

Page 23: Modern programozás

Példa: Tartalomkezelők

• Oldal, mint entitás

• Fővezérlés szála

!

• Hasáb, mint entitás

• Megjelenítési szál

Page 24: Modern programozás

Adatmodell

Page 25: Modern programozás

Rétegek

ENTITY Információ

TABLE Entitások

TABLE GATEWAY Adatbázis illesztés

HYDRATOR Objektum - Adat

Page 26: Modern programozás

Entityabstract class Entity{ protected $serviceLocator; protected $storedPrimaryKey; protected $table;

Page 27: Modern programozás

Entity final public function getServiceLocator() { return $this->serviceLocator; }

final public function getStoredPrimaryKey() { return $this->storedPrimaryKey; }

final public function getTable() { return $this->table; }

Page 28: Modern programozás

Entity final public function setServiceLocator(Ser…ace $serviceLocator) { $this->serviceLocator = $serviceLocator; return $this; }

final public function setStoredPrimaryKey(array $storedPrimaryKey) { $this->storedPrimaryKey = $storedPrimaryKey; return $this; }

final public function setTable(Table $table) { $this->table = $table; return $this; }

Page 29: Modern programozás

Entity public function delete() { if (!$this->storedPrimaryKey) { throw new \Exception(‘Nincs tárolva’); } $this->table->delete($this->storedPrimaryKey); $this->storedPrimaryKey = array(); return $this; }

abstract public function exchangeArray(array $data);

Page 30: Modern programozás

Entity public function save() { $this->table->save($this); $reloaded = $this->table ->fetchAll($this->storedPrimaryKey)->current(); if ($reloaded) { $this->exchangeEntity($reloaded); } else { throw new \Exception(‘Hiba történt visszaolvasás közben’); } }}

Page 31: Modern programozás

User Entityclass User extends Entity { protected $name; protected $userID; public function getName() {…} public function getUserID() {…} public function setName($name) {…} public function setUserID($userID) {…}

Page 32: Modern programozás

User Entity public function exchangeArray(array $data) { $this->name = isset($data[‘name’]) ? $data[‘name’] : null; $this->userID = isset($data[‘userID’]) ? $data[‘userID’] : null; $this->storedPrimaryKey = array(‘userID’ => $this->userID); return $this; }

public function exchangeEntity(User $entity) { $this->name = $entity->name; $this->userID = $entity->userID; $this->storedPrimaryKey = $entity->storedPrimaryKey; return $this; }

Page 33: Modern programozás

User Entity public function exchangeArray(array $data) { $this->name = isset($data[‘name’]) ? $data[‘name’] : null; $this->userID = isset($data[‘userID’]) ? $data[‘userID’] : null; $this->storedPrimaryKey = array(‘userID’ => $this->userID); return $this; }

public function exchangeEntity(User $entity) { $this->name = $entity->name; $this->userID = $entity->userID; $this->storedPrimaryKey = $entity->storedPrimaryKey; return $this; }

Hydrator

Page 34: Modern programozás

Tableabstract class Table { protected $serviceLocator; protected $tableGateway;

public function __construct(TableGateway $tableGateway) { $this->tableGateway = $tableGateway; }

final public function getServiceLocator() {…} final public function getTableGateway() {…} final public function setServiceLocator(…tor) {…}

Page 35: Modern programozás

Table public function delete($where) { $this->tableGateway->delete($where); return $this; }

final public function fetchAll($where = null) { return $this->tableGateway->select($where); } }

Page 36: Modern programozás

User Tableclass User\Table extends Table { public function save(User $entity) { $data = array(‘userID’ => $entity->getUserID(), ‘name’ => $entity->getName());

if ($entity->getStoredPrimaryKey()) { $this->tableGateway->update($data, $entity->getStoredPrimaryKey()); } else { $this->tableGateway->insert($data); $data[‘userID’] = $this->tableGateway->getLastInsertValue(); }

$entity->setStoredPrimaryKey(array(‘userID’ => $data[‘userID’])); }

Page 37: Modern programozás

User Tableclass User\Table extends Table { public function save(User $entity) { $data = array(‘userID’ => $entity->getUserID(), ‘name’ => $entity->getName());

if ($entity->getStoredPrimaryKey()) { $this->tableGateway->update($data, $entity->getStoredPrimaryKey()); } else { $this->tableGateway->insert($data); $data[‘userID’] = $this->tableGateway->getLastInsertValue(); }

$entity->setStoredPrimaryKey(array(‘userID’ => $data[‘userID’])); }

Hydrator

Page 38: Modern programozás

Table Gatewayabstract class TableGateway extends AbstractTableGateway{ protected $entityPrototype; protected $serviceLocator;

public function __construct(Adapter $adapter, $table, $entityPrototype) { $this->adapter = $adapter; $this->table = $table; $this->entityPrototype = $entityPrototype; $this->resultSetPrototype = new ResultSet; $this->resultSetPrototype->setArrayObjectPrototype($entityPrototype); $this->sql = new Sql($adapter, $table); }

Page 39: Modern programozás

Table Gateway final public function getServiceLocator() {…} final public function setServiceLocator(…tor) {…}

public function create() { return clone $this->entityPrototype; } }

Page 40: Modern programozás

Abstract Table Gateway

• select()

• insert()

• update()

• delete()

Page 41: Modern programozás

User - Groupclass User extends Entity{ … protected $group;

public function getGroup() { if (!is_object($this->group) && !empty($this->group)) { $this->group = $this->serviceLocator->get(‘Group\Table’) ->fetchAll(array(‘groupID’ => $this->group))->current(); } return $this->group; }

Page 42: Modern programozás

User - Group public function getGroupID() { return is_object($this->group) ? $this->group->getGroupID() : $this->group; }

public function setGroup($group) {…}}

Page 43: Modern programozás

Összefoglalás

• Entitás

• Egyszerű modulok

• Bonyolult hálózat

• Rétegelt felépítés


Top Related