Preface
This is an introductory post to the series, where I’ll cover all design patterns, their usage, diagrams. Besides patterns, I am going to guide you through the principles of the high-quality application design. We’ll start from these principles (GRASP), and then advance to patterns, see how they use these principles and develop several systems (architecture only). The whole series will take 10-15 posts. However, I don’t have exact plan yet
So, let’s go!
So what we’re talking about?
Actually, design pattern is no more, than a proven solution to some common problem. When saying “problem” I mean things you face with when developing the application architecture. These solutions are intended to make you application better – more extensible, flexible, simple.
Common examples of design problems (Let’s agree that in this series “design” means application, not graphics or DB design) are:
- Organizing access to the shared resource. This can be application object, DB abstraction object, Session object or other similar things. Main problem is that they should be created only once and accessed in every method easily. Singleton pattern solves this problem.
- Creating the easily extensible application. For example, code shopping cart in the way, that allows to add new payment or taxation modules easily. Adapter pattern saves us here.
- Providing the simple interface for accessing features of the application or application module, hiding all the underlying classes/functions from the user. Façade pattern does this.
- Separating data from data processing and from presentation. You’ve definitely heard about MVC (Model-View-Controller) – that’s also a pattern!
So patterns are everywhere in OOP and knowing them is essential for high-quality and extensible application design.
The basics
However, patterns themselves are implementation of the 9 basic principles (GPASP – General Responsibility Assignment Software Principles).
All OOP design is about creating real-world abstractions (classes), design methods (responsibilities) and their interaction. The key thing to good architecture is assigning responsibilities to the classes correctly. GRASP deals exclusively with this problem. We’ll go through all of them:
- Creator
- Information Expert
- Low Coupling
- High Cohesion
- Controller
- Polymorphysm
- Pure Fabrication
- Indirection
- Protected Variations
Today we’ll start from the first principle.
Creator
The most common problem in OO design is “Who should create object X?” This principle states, that object A should create object B if:
- A contains or aggregates B (e.g. Object User should initialize object Address)
- A records B (saves to DB or file)
- A closely uses B
- A has the initialization data that should be passed to B when object is created
If any of these conditions are true, then A should create B. Now let’s demonstrate how these principles work in real examples.
A contains or aggregates B
Consider we have table. Common table consists of desk and legs.
If we use object decomposition to program the table, we’ll get 3 classes:
-
Table (blue box on image):
- Desk (light grey)
- Leg (wooden)
Object decomposition basically means breaking some entity into parts and representing each part with a object. These parts can be decomposed further. For example, we can break Table into Desk and Leg. And then break Leg into Screw, Plank. In complex system there is a number of such levels.
If we start programming this, we can go in 2 ways. For example, we don’t want to use the Creator principle and will create Table class as follows:
-
class Table
-
{
-
protected $desk;//desk object
-
protected $legs;//array of legs
-
-
public function __construct(Desk $d, $l)
-
{
-
$this->desk = $dd;
-
$this->legs = $l;
-
}
-
}
Then Desk and Leg classes:
-
class Desk
-
{
-
protected $width;
-
protected $length;
-
protected $height;
-
-
public function __construct($w, $l, $h)
-
{
-
$this->width = $w;
-
$this->length = $l;
-
$this->height = $h;
-
}
-
}
-
class Leg
-
{
-
protected $width;
-
protected $length;
-
protected $height;
-
-
public function __construct($w, $l, $h)
-
{
-
$this->width = $w;
-
$this->length = $l;
-
$this->height = $h;
-
}
-
}
Yes, Desk and Leg classes are really the same cause they both are parallelepipeds. So we can create Parallelepiped class and extend both of them from it. We’ll do it later, now we’re talking about object creation. The above classes can be used in the following way:
-
$desk = new Desk(900,900,20);//90 cm width, 90 length (square) and 2cm height. A normal table for kitchen
-
new Leg(40,40, 880),//4×4 cm square and 88cm height. 88cm leg + 2 cm desk = 90cm full height
-
new Leg(40,40, 880),
-
new Leg(40,40, 880),
-
new Leg(40,40, 880),
-
);
-
$table = new Table($desk, $legs);
Yes, the object is created, but large part of the creation logic is outside of the object. The class user (the programmer who is using this class) should know the internal structure of objects to create the whole object. This approach is inefficient when we deal with large application; on the other side, such solution can’t be reused without changing the code. And it violates the first condition of using the Creator principle. Either Desk and Leg are parts of the Table object, so it should take care of their instantiation. So let’s learn how to solve such task correctly:
We’ll create constructor for Table class with parameters:
- Width – width of the table
- Length – length of the table
- Height – height of the table
- DeskHeight – height of the desk
- LegSection – (assume we want square legs)
So the end-user of the class should specify only these 5 parameters, we don’t ask him to create parts of our object. OK, let’s code it:
-
class Table
-
{
-
protected $desk;
-
protected $legs;
-
-
public function __construct($width, $length, $height, $deskHeight, $legSection)
-
{
-
$this->desk = new Desk($width, $length, $deskHeight);
-
for ($i = 0; $i < 4; $i++)
-
{
-
$this->legs[$i] = new Leg($legSection, $legSection, $height – $deskHeight);
-
}
-
}
-
}
Desk and Leg classes remain the same. So here we followed the first condition (actually, the most important) and got nice architecture.
A records B
After detailed discussion of the first condition, this should be quite evident. Usually if some object saves another one, it is his part. For example, we have some system with users. Each user may have several addresses. So Address will be the part of User (it is quite logical to design so), and User object will save all addresses in one field (Using serialization). And when loading, it will load the address info from DB and create Address objects. This is example of both 2, 3 and 4 item. The User object saves Address. User object has initialization info for Address and user object closely uses Address. By the way, first item is also here, Address is part of User object.
As you see, all these conditions often go together.
I think, that the final 2 items should be evident.
Further development
Tasks for improvement your skills:
- Extend both Leg and Desk from the Parallelipiped class
- Implement getHeight() method for Table, Desk and Leg. Hint: In Leg and Desk classes it should simply return height. In Table class it should return sum of leg height and Desk height
- Implement setHeight method for all classes. Hint: when setting height for table, you should actually increment Leg’s height
- Implement Saving and loading from file or DB. Hint: actually you have to save only 5 parameters – those passed to table constructor. So when saving and loading Table class with interact with Desk and Leg classes. And when loading – will create these classes.
Information Expert
The last condition of applying the Creator principle shows, that object A should have all info about B to create it. In other words, A should be Expert in info about B. Information Expert principle helps us assigning all responsibilities in the project. Real-world application usually holds a number of classes, that perform hundreds and thousands of actions, so it is essential to assign responsibilities correctly. The principle itself is fairly simple – you should give the responsibilities to the object it has information for. For example, assigning Table class responsibility to create Desk is correct because it has info about it. Assigning User responsibility to save data to DB is also correct, because User holds data about user and can perform this task without any other objects (assuming it deals with DB without DB abstraction layer). And assigning User to save information about Group is incorrect, because User will have to “ask” Group about it’s properties – ID, name, access rights etc. in order to save it correctly. In this case it’s much better to assign Group responsibility to save itself. And Use can call save() method on Group object.
In the next post I’ll cover Low Coupling and High Cohesion principles and illustrate it with practical examples. Subscribe to RSS not to miss it!
Feel free to ask any questions in comments or by email: konstantin.mirin[at]programmersnotes.info
Further reading
- GRASP
- Larman, Craig (2005). Applying UML and Patterns – An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd ed.). Prentice Hall PTR. ISBN 0-13-148906-2.
Related posts:
- Low Coupling and High Cohesion – GRASP (Design patterns series) Low coupling When designing some architecture, you face with the...
Related posts brought to you by Yet Another Related Posts Plugin.
Share this post with a friend













Jonah says:
Good read! It’s healthy to take a large step back like this every once in a while and think about the way you are doing things from the “big picture” viewpoint.
March 30, 2009, 07:52Konstantin Mirin says:
Yes, reviewing such topics reveal new things you didn’t notice when studying it
That’s why I’m blogging and it’s one of the reasons to start this series.
March 30, 2009, 20:51fristi says:
this is really interesting to read!
May 21, 2009, 14:44Specially when you are not an expert like me. The table example really gives a clear view on how to design your architecture. Looking forward to your following posts in this series. (thanks for pointing this out to me on sitepoint.com
Konstantin Mirin says:
Thanks for the nice comments! I’ll definitely continue the series, I have the next post half-finished. I think, that will be on the weekends or next week. Subscribe to RSS not to miss this
May 21, 2009, 16:45KrisBelucci says:
I really liked this post. Can I copy it to my site? Thank you in advance.
June 3, 2009, 02:01Konstantin Mirin says:
No, but you may reference it. I’d prefer to keep info on my site only. If you need articles, we can make an agreement and I can write a few for you – for free if your blog has huge traffic, just for linking to mine. If it is just a start-up with PR<2 and <1000 unique visitors per month, I’m not interested.
June 4, 2009, 08:01Kelly Brown says:
Great post! I’ll subscribe right now wth my feedreader software!
June 12, 2009, 19:09KattyBlackyard says:
The article is usefull for me. I’ll be coming back to your blog.
June 15, 2009, 03:28GarykPatton says:
I have been looking looking around for this kind of information. Will you post some more in future? I’ll be grateful if you will.
June 16, 2009, 03:51Konstantin Mirin says:
Yes, I will. I’m finising my study this week and then I’ll have more time for the blog. I have some articles half-ready, they just need polishing and illustrations. Thank you for the positive feedback, I’ll definitely post about other patterns and principles.
June 18, 2009, 11:39KonstantinMiller says:
Hi. I like the way you write. Will you post some more articles?
July 6, 2009, 19:30Walkie talkies guide says:
…
I saw this really great post today. Here you can always look the cheap Kenwood walkie talkie!…
May 28, 2010, 16:11Glass coffee table guide says:
…
I thought that was extremeley fascinating. Many thanks for your unusual details. I’ll retain subsequent this….
June 4, 2010, 09:17Yahoo News says:
Yahoo News…
This is really good news today….
June 9, 2010, 18:46remortgage with bad credit diving into the bright side says:
badcreditremortgage…
I saw this really good post today……
June 15, 2010, 15:26Fleece vest says:
…
Hi there may perhaps I reference some from the insight below in this website if I reference you having a link back again for your web page?…
June 18, 2010, 15:44Corner Television stands guide says:
…
How’s it intending? I loved reading this write-up. My husband and I have been browsing for this type of write-up for that longest time and We realize that your information for the issue at hand is spot on. I will be sure to introduce this article to my…
June 22, 2010, 17:03best remortgage deals says:
adverse credit remortgage…
I saw this really great post today……
June 25, 2010, 13:29casio pathfinder says:
casio pathfinder…
My blog about casio pathfinder…
July 27, 2010, 22:06Coach handbag sale says:
…
I saw this really fantastic post today!…
July 29, 2010, 16:46Latex mattress says:
…
That is some inspirational stuff. In no way knew that ideas could be this varied. Thanks for all the enthusiasm to provide such helpful details here….
August 18, 2010, 12:03JackieBooth says:
Have no a lot of cash to buy a house? Worry no more, because this is achievable to get the business loans to solve all the problems. So get a commercial loan to buy everything you want.
August 18, 2010, 22:05Senior business guide says:
…
Thanks! it is useful to me!…
August 29, 2010, 19:35Bagless upright vacuum says:
…
Many thanks for that good write-up. I’ll take the notes you’ve written….
September 4, 2010, 14:08sealy mattresses says:
Hi just thought i would certainly tell you something.. It is twice now i have landed on the blog within the last 3 days searching for totally unrelated things. Odd or what?
September 6, 2010, 13:45White Gold Wedding Bands says:
…
Sure glad that I navigated for a page by accident. I’ll be subscribing for a feed in order that I can get the newest updates. Appreciate all of the information right here…
September 8, 2010, 11:04DOUGLAS says:
Buy:Arimidex.Petcam (Metacam) Oral Suspension.Prevacid.Mega Hoodia.Zyban.Valtrex.Zovirax.Actos.Synthroid.Human Growth Hormone.Accutane.Lumigan.Prednisolone.Nexium.100% Pure Okinawan Coral Calcium.Retin-A….
September 8, 2010, 16:41