Design patterns are ways of thinking about programming.
They provide a collection of advice, ready-made practices, best-practices and insights into development. For each programming paradigm and task type, there are certain design patterns that are best suited.
In programming, solving certain types of problems is repetitive, so it makes sense to choose one method to solve these problems and keep repeating that method.
The main benefit arises especially during team development, when everyone knows how the application will be developed (according to which design pattern) and they just apply it. This then eliminates the unnecessary tens of hours of debugging strangely written code and trying to understand the principles the author intended.
My favourite design pattern is MVC
(from Model View Controller
), which says that the application is divided into 3 independent layers that call each other sequentially and pass data to each other.
For example, when rendering a page, it might look like it first decides what type of page it is (for example, a category detail), so it calls CategoryController
with the detail
method.
A concrete example (I'm simplifying a lot):
class CategoryController{public CategoryManager $categoryManager;public function actionDetail(string $id): void{$this->template->id = $id;$this->template->category = $this->categoryManager->getById($id);}}
Note:
This is just a sample code that explains the principle of the
MVC
design pattern.In a real implementation, we would have to further figure out how to, for example, get an instance of
CategoryManager
and how to pass it to property. Typically,Dependency injection
is used for this type of task.
Before rendering the page for the category detail, the CategoryController
is first called to receive the actual request (i.e., we are rendering the category detail with a certain ID, which is obtained by the router, for example, in the URL), get the data (querying the corresponding Model
) and pass the final data to the template for rendering.
The huge advantage of this principle is that we can write many models (application logic) that is independent of the way the data is presented (the template), resulting in reusable code. In fact, if we want to use the CategoryManager
in another project, we simply pass the data through the Controller
in a specific way, which will be rendered according to the template defined by the project itself, while the application logic remains the same and nobody cares because the software layer has fulfilled its agreed interface and responsibilities.
Practical note:
The
MVC
design pattern is used by most modern frameworks such as Nette, Symfony, Laravel and others.We can also encounter
MVC
in mobile app development and other types of software where we need to get data and render it in a template according to the page or view type.
Generally in programming there are many design patterns which are not suitable for web development. This list describes the most important patterns that I use myself and you should be familiar with.
A complete list of all design patterns, examples of their use and detailed explanations can be found on a separate page.
Model
(application logic and data), View
(template and data view) and Controller
(linking Model
and View
).Dependency injection
, where each service has only one instance, which is passed across the application).Adapter
converts data from one type to the other (typically converting native PHP data types to database data types and back again).There are many more design patterns, these were the most important ones you should know.
Some programming development techniques are considered anti-pattern
, which is the exact opposite of a design pattern. It is usually a technique that produces strange code that cannot be easily debugged, maintained, and behaves magically
.
A typical example is the use of global variables.
Jan Barášek Více o autorovi
Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.
Rád vám pomůžu:
Články píše Jan Barášek © 2009-2024 | Kontakt | Mapa webu
Status | Aktualizováno: ... | en