solid mvc - thephp.cc fileprobably the widest quoted pattern in ui development is model view...

56
SOLID MVC Stefan Priebsch | Bulgaria PHP Conference 2015

Upload: truongmien

Post on 27-Aug-2019

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

SOLID MVCStefan Priebsch | Bulgaria PHP Conference 2015

Page 2: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Stefan PriebschConsultant and coach. Helps teams to successfully develop software.

Page 3: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

sharing experience

Page 4: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

MVC ?

Page 5: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Controller

ModelView

Page 6: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen
Page 7: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Probably the widest quoted pattern in UI development is Model View Controller

(MVC) - it's also the most misquoted. I've lost count of the times I've seen

something described as MVC which turned out to be nothing like it.

http://martinfowler.com/eaaDev/uiArchs.html

Page 8: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

"A Framework is no architecture"Ask your favourite search engine for more information

Page 9: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

SOLID ?

Page 10: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

[SOLID] stands for five basic principles of object-oriented programming and

design.

[...]

The principles, when applied together, intend to make it more likely that a

programmer will create a system that is easy to maintain and extend over time.

https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

Page 11: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen
Page 12: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

SOLID• Single Responsibility Principle• Open/Closed Principle• Liskov Substitution Principle• Interface Segregation Principle• Dependency Inversion Principle

Page 13: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Single ResponsibilityThere should never be more than one reason for a class to change

Page 14: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Open/Closed PrincipleCode should be open for extensions, but closed for modifications

Page 15: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Liskov Substitution PrincipleIn short: inheritance sucks

Page 16: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Interface SegregationClients should not be forced to depend upon interfaces that they do not use

Page 17: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Dependency InversionA. High-level modules should not depend on low-level modules.

Both should depend on abstractions.

Page 18: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Dependency InversionB. Abstractions should not depend on details.

Details should depend on abstractions.

Page 19: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Infrastructure

Domain

Page 20: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Infrastructure

Domain

A

I

Page 21: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class FooController extends FrameworkController

{

public function indexAction()

{

...

}

public function createAction()

{

...

}

public function deleteAction()

{

...

}

...

}

Page 22: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class IndexController extends FrameworkController

{

public function indexAction()

{

...

}

}

class CreateController extends FrameworkController

{

public function createAction()

{

...

}

}

...

Page 23: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class IndexController extends FrameworkController

{

public function run()

{

...

}

}

class CreateController extends FrameworkController

{

public function run()

{

...

}

}

...

Page 24: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

interface Controller

{

public function run();

}

Page 25: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class IndexController ...

{

public function process(HttpRequest $request)

{

...

}

}

class CreateController ...

{

public function process(HttpRequest $request)

{

...

}

}

...

Page 26: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

interface Controller

{

public function process(HttpRequest $request);

}

Page 27: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class IndexController ...

{

public function process(HttpGetRequest $request)

{

...

}

}

class CreateController ...

{

public function process(HttpPostRequest $request)

{

...

}

}

...

Page 28: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

interface Controller

{

public function process(HttpRequest $request);

}

Page 29: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class FrameworkController

{

protected function getRequest() {...}

protected function getFactory() {...}

protected function getServiceContainer() {...}

...

}

Page 30: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen
Page 31: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class MyController extends Framework_Controller

{

public function someAction()

{

global $templating;

global $router;

global $mailer;

// ...

}

}

Page 32: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class YourController extends FrameworkController

{

public function someAction()

{

// ...

... $this->retriveFromDiContainer('something') ...

// ...

}

}

Page 33: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class YourController extends FrameworkController

{

public function someAction()

{

// ...

... $this->retrieveSomethingFromDiContainer() ...

// ...

}

}

Page 34: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class YourController extends FrameworkController

{

private $something;

public function __construct(Something $something)

{

$this->something = $something;

}

public function someAction()

{

// ...

... $this->something->doStuff() ...

// ...

}

}

Page 35: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Dependency Disguise is an Antipattern

Page 36: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class YourCode

{

private $dependency;

public function __construct(

SomePartOfTheFramework $externalDependency

)

{

$this->dependency = $externalDependency;

}

// ...

}

Page 37: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class YourCode

{

private $dependency;

public function __construct(

SomePartOfTheFrameworkButItIsAnInterface $externalDependency

)

{

$this->dependency = $externalDependency;

}

// ...

}

Page 38: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class YourCode

{

private $dependency;

public function __construct(

InterfaceDefinedByFramework $externalDependency

)

{

$this->dependency = $externalDependency;

}

// ...

}

Page 39: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class YourCode

{

private $dependency;

public function __construct(

InterfaceDefinedByYou $internalDependency

)

{

$this->dependency = $internalDependency;

}

// ...

}

Page 40: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

interface YourSomethingLoaderInterface

{

public function loadAllSomethings();

}

Page 41: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class YourLoader implements YourSomethingLoaderInterface

{

private $thing;

public function __construct(FrameworkLoaderMagicComponentThing $thing)

{

$this->thing = $thing;

}

public function loadAllSomethings()

{

... work with $this->thing ...

... and convert the result back to a SomethingCollection ...

}

}

Page 42: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Infrastructure

Domain

A

I

Page 43: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class SomethingModel

{

public function __construct(Session $session)

{

...

}

}

Page 44: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class SomethingModel

{

public function __construct(ApplicationState $applicationState)

{

...

}

}

Page 45: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class SomeController extends FrameworkController

{

public function indexAction()

{

...

$this->redirect(...);

}

}

Page 46: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

<?php

class FooController extends FrameworkController

{

public function indexAction()

{

...

$view->setData(...);

}

}

Page 47: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Summary:MVC usually does not apply to your problem,

but makes it super-simple to write non-SOLID code.

Page 48: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Summary:You are responsible for the quality of your code.

Page 49: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Warning Signs:First, you decide which framework to use

Page 50: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Warning Signs:You put code into the framework

Page 51: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Warning Signs:You consider migrating from one framework to another

Page 52: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Warning Signs:Your methods do not fit on one screen

Page 53: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Warning Signs:You are unsure whether to put something into a controller, or into a model

Page 54: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

Warning Signs:You have a hard time naming classes and methods

Page 56: SOLID MVC - thephp.cc fileProbably the widest quoted pattern in UI development is Model View Controller (MVC) - it's also the most misquoted. I've lost count of the times I've seen

sharing experience