Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-17-2005, 07:57 AM   #1 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Tip (5 minute solution): The Facade Pattern

The Facade Design Pattern

Often when code is presented by slightly less seasoned coders, they tend to overdo the main() function, by adding too much code in it. It only gets harder for strangers to find out what's going on.

Here is an example. We will implement the most basic linked list we can imagine:
Code:
#include <iostream>

struct IntNode
{
  int num_;
  IntNode* next_;
};

int main()
{
  IntNode *start, *p;
  start = 0;
  
  //Add a node.
  p =  start;
  start = new IntNode;
  start->next_ = p;
  start->num_ = 28;
  
  //Add another node.
  p =  start;
  start = new IntNode;
  start->next_ = p;
  start->num_ = 13;
  
  //Add another node.
  p =  start;
  start = new IntNode;
  start->next_ = p;
  start->num_ = -48;
  
  //Show the contents of the linked list.
  for(p = start; p != 0; p = p->next_)
  {
    std::cout<<p->num_<<", ";
  }
  std::cout<<std::endl;
  
  //Release the memory.
  while(start != 0)
  {
    p = start;
    start = start->next_;
    delete p;
  }
  
  return 0;
}
As you can see, the main() function is full of code. We should simply encapsulate some code so the big picture becomes clear in a blink of the eye. Just write a class that does all the difficult stuff. Let's make a Facade:
Code:
#include <iostream>

struct IntNode
{
  int num_;
  IntNode* next_;
};

//--------------------------------------------

class LinkedList
{
public:
  void AddItem(int itm);
  void ShowItems();
public:
  LinkedList() : start_(0){ }
  ~LinkedList();
private:
  IntNode* start_;
};

//--------------------------------------------

int main()
{
   LinkedList MyList;
  MyList.AddItem(28);
  MyList.AddItem(13);
  MyList.AddItem(-48);
  MyList.ShowItems();
  
  return 0;
}

//--------------------------------------------

void LinkedList::AddItem(int itm)
{
  IntNode* p = start_;
  start_ =  new IntNode;
  start_->next_ = p;
  start_->num_ = itm;
}

void LinkedList::ShowItems()
{
  for(IntNode* p =  start_; p != 0; p = p->next_)
  {
    std::cout<<p->num_<<", ";
  }
  std::cout<<std::endl;
}

LinkedList::~LinkedList()
{
  IntNode* p;
  while(start_ != 0)
  {
    p = start_;
    start_ = start_->next_;
    delete p;
  }
}
And now a stranger can concentrate on the big issues first.

Let's wrap it up:
Goal:
Hide the difficult stuff in a class (or more if needed).

Advantage:
Makes it client-friendly.

Disadvantage:
Client might have less "power-user" options.

That's it!
- Val -
__________________
Valmont is offline   Reply With Quote
Old 02-17-2005, 08:01 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Actually void LinkedList::ShowItems() doesn't belong in that class (why?), but that is not relevant right now.
__________________
Valmont is offline   Reply With Quote
Old 02-24-2005, 07:36 PM   #3 (permalink)
wishknew
Wisnu Widiarta
 
wishknew's Avatar
 
Join Date: Feb 2005
Location: Indonesia
Posts: 14
wishknew is on a distinguished road
Send a message via Yahoo to wishknew
Facade Design

Simplifying the function main( ) is part of refactoring -> extract method, rite? IMHO, it is suit for all function / method which is too long and doing many tasks.

LinkedList should be just like a container. ShowItems is not really behaviour of the class, isn't it?

Wisnu
wishknew is offline   Reply With Quote
Old 02-25-2005, 12:27 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
If you encapsulate complex tasks in a class, you'll have even more options. For example you could let the Facade create objects (instances) which are relevant to solve a problem at that time. And the user will never know it. For the user, it all seems simple.
But my goal was to demonstrate students that they shouldn't make big, long functions. Yes.

Quote:
LinkedList should be just like a container. ShowItems is not really behaviour of the class, isn't it?
A container shouldn't be responsible for handling the view as well. Indeed.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:33 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting