Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Dec 30 2008

Fat Model and Skinny Controller

Published by under MVC,Patterns,PHP

Few days ago, I read a post (the post is a bit old) about the the design concept of fat model, skinny controller. After going through that post and this one, for few days I have been thinking and trying to analyze the implementation impact of the concept. I guess I have been petty much impressed after doing some research on the web and going through blogs of experts in this subject.

Fat + Skinny
It won’t be wrong if MVC is called the de-facto pattern of developing web applications. But still, the implementation mechanism of MVC and understanding the overall logic of MVC can be different from developer to developer.
MVC, being mostly implemented in the presentation layer provide powerful mechanism to developers to reuse code (reusing could have been easy if there were only one language or platform to deal with but while developing web applications developers need to deal with many languages and technologies that may or may not integrate well with each other) and significantly apply the concept of object oriented principles and agile methodologies.
Most of the developers and their code I have encountered especially in the PHP seem to embrace the idea of putting business logic in the controller rather than in the model. They regard model as the way only to deal with CRUD operations. It is true that MVC frameworks like Cake and Symphony provide quite powerful ORM capabilities without the need of writing a single SQL query embedding the principles popularized by Rails i.e. convention over configuration, DRY and YAGNI.
But in my opinion, models should not be treated as the the component for dealing with CRUD operations. Model should be responsible for more than that. It’s also true that applying business logic only through models is almost kinda impossible. It’s something like coupling which needs to be reduced but can’t be eliminated completely.
I view controllers as a tunnel that should be responsible for accepting input from the view and passing to the model and accepting raw output from the model and passing it to the view. Making the controller petty fat could make the code difficult to re-use. In reality all these things are easier to say but making the best use of the MVC by making model responsible for most of the business logic and let controllers and views perform their native task can be quite difficult because of the complex nature of the web. Complexity in the sense that there are other languages and technologies as well in the same layer that needs to be integrated and be dealt with. For eg; Server side Flash / SilverLight components, JavaScripts and so on.
So I have been trying for few days to apply the concept of fat model and skinny controller and as expected facing some difficulties especially to make my model more responsible and controller more light or in other word more skinny. Since I was involved in that project from middle so, attaching the business logic only to the model seems quite impossible. So, I am looking forward to do some small project in MVC applying the concept of Fat Model and Skinny Controller.
Until then, I can not claim that this concept must be followed though I still believe this concept should be followed to write re-usable and loosely coupled code.
As usual it would be great if you provide comments and criticisms and I can also know if I am wrong or right.


Comments Off

Dec 27 2008

Method Chaining in PHP

Published by under PHP

Make modifier methods return the host object so that multiple modifiers can be invoked in a single expression. – Martin Fowler [article]

In simple terms method chaining is the way of calling a sequence of class methods where each object returns the same host object for further modification. This approach is more used by JAVA and .NET developers (as I have seen). After having a quick look at various PHP frameworks like Zend, CodeIgniter and Cake, it seems like they do support method chaining and I really love to use method chaining when possible. Enabling method chaining is quite similar in PHP as it is in C# and JAVA. By the use of $this, method chaining can be enabled for any class.

<?php
	class HardDrive
	{
		private $_capacity;
		private $_isExternal;
		private $_speed;

		//Getters and setters for _capacity
		function getCapacity()	{
			return $this->_capacity;
		}

		function setCapacity($capacity)	{
			$this->_capacity = $capacity;
			return $this;
		}

		//Getters and Setters  for _isExternal
		function getIsExternal()	{
			return $this->_isExternal;
		}

		function setIsExternal($isExternal)	{
			$this->_isExternal = $isExternal;
			return $this;
		}

		//Gettter and setter for speed
		function getSpeed()	{
			return $this->_speed;
		}

		function setSpeed($speed)	{
			$this->_speed = $speed;
			return $this;
		}
	}
?>

Using Method Chaining for above Class

<?php
	$hardDrive = new HardDrive();
	$hardDrive
		->setCapacity(200)
		->setIsExternal(false)
		->setSpeed(7200);
?>

 

…or using traditional way

<?php
	$hardDrive = new HardDrive();
	$hardDrive->setCapacity(200);
	$hardDrive->setIsExternal(false);
	$hardDrive->setSpeed(7200);
?>

I have seen people who really hates method chaining and also seen people that use this approach a lot. And luckily or unluckily I am also kinda supporter of method chaining. So, if you are like me I hope you want to enable method chaining in your class as well and as you saw it’s very very simple.

And finally, some considerations you need to know. I don’t think method chaining is possible in PHP 4 (but who cares about PHP 4, coz it’s kinda RIP :) ). The biggest problem with PHP method chaining I have felt is that IDE like Eclipse really don’t provide auto-complete feature :(


Comments Off

Sep 27 2008

Polymorphism in PHP

Published by under Patterns,PHP

In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B. In non-strongly typed languages (dynamically typed languages) types are implicitly polymorphic to the extent they have similar features (fields, methods, operators). In fact, this is one of the principal benefits of dynamic typing. – Wikipedia
Polymorphism is tightly coupled to the inheritance and is often considered to be one of the most powerful feature of the object oriented programming. It can be defined as a term according to which a name (variable declaration) may denote objects of many different classes that are related by some common suprclass; thus, any object denoted by this name is able to respond to some common set of operations in different ways. – Grady Booch

Lets consider an example:
There are two array of shapes called Rectangle and Circle. Even though we treat both these as a Shape but their implementation is quite different. In nutshell, each class is able to respond differently to the same method getArea(). This is called Polymorphism.

UML Diagram

PHP Code


Comments Off

Sep 26 2008

Going thru Patterns – Factory Method Pattern

Published by under Patterns,PHP

This is my first attempt to try to understand and implement patterns and practices. It is necessary to understand and implement these design patterns in order to make code flexible, re-usable, highly cohesive and less coupled.

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems. – Wikipedia

http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

As this is my first post regarding patterns and practices, I want to express something that I have learnt about Factory Method Pattern. It is a part of Gang-of-Four (GoF) design patterns.

The Factory Method pattern is a way of creating objects, but letting subclasses decide exactly which class to instantiate. Various subclasses might implement the interface; the Factory Method instantiates the appropriate subclass based on information supplied by the client or extracted from the current state. This pattern enables to make decision about which concrete class to be instantiatedin the run-time.

Lets go thru an example:

There is an interface called IPerson and two implemented class named Student and Programmer. The class called PersonFactory is created to make run-time decision about the selection of Student or Programmer thru a static method named GetWorkDone.

UML Diagram


PHP Code


Comments Off

« Prev

RSS Feed
Subscribe by email
Follow me @ Twitter