Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Patterns Used in CakePHP

My Question:
What are some good examples of design patterns used in CakePHP?

Why Use CakePHP As My Context
I've been using CakePHP for about a year so I think it's easier for me to think in that context. CakePHP is also rich in design pattern use (I'm confident of that)--I just don't know what patterns are being used other than a few obvious ones.

Example Books I've been Reading On the Subject:
I'm reading the following books which all cover design patterns to one extent or another; unfortunately they mostly use Java and C++ code examples which makes it harder for me to get a grip on the design patterns on a practical level (I'm a PHP developer so its hard for me to absorb it):
"Patterns of Enterprise Application Architecture", by Martin Fowler
"Head First Design Patterns", by Gang of Four (Eric Freeman, Elisabeth Freeman, Kathy Sierra & Bert Bates) (2004)
"Design Patterns: Elements of Resuable Object Oriented Software)", by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides)

Examples of Patterns I Can Observe in CakePHP
-I'm guessing the config file uses something akin to the factory pattern
-maybe $this->params is using something related to the observer pattern? I'm not sure on that...
-MVC (obvious! since Cake PHP uses the MVC file structure)
-ORM (another very obvious one)
-Maybe the HTML helper is using the decorator pattern?

Summary
I don't expect anyone to go down the line and identify all the patterns used in CakePHP--I'm just looking for a few examples of design patterns that should be obvious that I'm missing.

like image 379
Jason F Avatar asked Jan 11 '12 06:01

Jason F


People also ask

Which design pattern is used in asp net core?

ASP.NET Core API. The MVC design pattern is a popular design pattern for the user interface layer of a software application. In larger applications, you typically combine a model-view-controller UI layer with other design patterns in the application, like data access patterns and messaging patterns.

Which design pattern is commonly used?

Factory Design Pattern One of the most popular design patterns used by software developers is a factory method. It is a creational pattern that helps create an object without the user getting exposed to creational logic. The only problem with a factory method is it relies on the concrete component.

Is CakePHP an MVC framework?

CakePHP is an open-source web framework. It follows the model–view–controller (MVC) approach and is written in PHP, modeled after the concepts of Ruby on Rails, and distributed under the MIT License. Cake Software Foundation, Inc.


2 Answers

One that comes to mind is the concept of mixins. Not exactly a pattern but actually a language feature available in some languages (ie. modules in Ruby) but not in others (ie. Java). It will be coming to PHP when 5.4 goes stable and we get traits, but CakePHP's model behaviours are a good example of mimicking this kind of multiple inheritance where it normally isn't possible.

class Post extends AppModel { // we can only inherit from one class

    public $actsAs = array('This', 'That', 'Other'); // but we can do this instead

}
like image 128
deizel Avatar answered Oct 19 '22 23:10

deizel


Software design patterns (like a RoR):

  • Convention over configuration: all configuration files from Configure

  • Model-View-Controller: folders: Model, Controller, View, etc

  • ActiveRecord, Association Data Mapping: database mapping

  • Front Controller: main entry point (index.php)

Found in the comments:

Creational patterns:

  • Singleton -- find by "getInstance, singleton"

  • Factory -- find by "factory"

  • Builder -- find by "builder"

Structural patterns:

  • Adapter -- find by "adapter"

  • Front controller (.htaccess, include)

Behavioral patterns:

  • Strategy -- find by "strategy"

View:

  • Two-step-view pattern -- "two-step-view"
like image 35
okuznetsov Avatar answered Oct 20 '22 01:10

okuznetsov