Tuesday, February 12, 2008

Beginning Design Patterns

In this series we will take a review around the design patterns that are the great elements of reusable object oriented software.
We will speak first about the meaning and importance of this great technique
Then we will move to the discussion of each of the 23 design patterns which are the basic design patterns in Erich Gamma's book (Gang of four)
This book will be my reference in this series and also the book of James W. Cooper (Introduction to Design patterns).
The samples code of this discussion will be in the C# language you can also create them in any language of your choice we will not speak about the syntax of the C# language we will just discuss the design pattern technique.
The reader of this series should have a good understanding of the OOP technique and also some about OOD.
I think that's enough of blab and let's move to our discussion.

Sitting at your desk in front of your workstation, you stare into space, trying to figure out how to write a new program feature. You know intuitively what must be done, what data and what objects come into play, but you have this underlying feeling that there is a more elegant and general way to write this program.
In fact, you probably don’t write any code until you can build a picture in your mind of what the code does and how the pieces of the code interact.
The more that you can picture this organic whole, the more likely you are to feel comfortable that you have developed the best solution to the problem. If you don’t grasp this whole right away, you may keep staring out the window for a time, even though the basic solution to the problem is quite obvious.
In one sense you feel that the more elegant solution will be more reusable and more maintainable, but even if you are the sole likely programmer, you feel reassured once you have designed a solution that is relatively elegant and that doesn’t expose too many internal inelegances.
One of the main reasons that computer science researchers began to recognize design patterns is to satisfy this need for elegant, but simple, reusable solutions.


-What are design patterns?
before we know design patterns we need to know what's a pattern?
Pattern describes a problem which occurs over and over again in our
environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.
The term “design patterns” sounds a bit formal to the uninitiated and can be somewhat off-putting when you first encounter it.
But, in fact, design patterns are just convenient ways of reusing object oriented code between projects and between programmers.
In other words, design patterns describe how objects communicate without become entangled in each other’s data models and methods. Keeping this separation has always been an objective of good OOP, and if you have been trying to keep objects minding their own business, you are probably using some of the common design patterns already. Design patterns recognized to satisfy the need to elegant, simple, and reusable solutions, also
The fundamental reason for using design patterns is to keep classes separated and prevent them from having to know too much about one another. Equally important, using these patterns helps you avoid reinventing the wheel and allows you to describe your programming approach succinctly in terms other programmers can easily understand.

The authors divided these patterns into three types: creational, structural, and behavioral.
•Creational patterns create objects for you rather than having you instantiate objects directly. This gives your program more flexibility in deciding which objects need to be created for a given case.
•Structural patterns help you compose groups of objects into larger structures, such as complex user interfaces or accounting data.
•Behavioral patterns help you define the communication between objects in your system and how the flow is controlled in a complex program.


Example:
One of the frequently cited patterns from early literature on programming frameworks is the MVC (Model-View-Controller) framework for Smalltalk
(Krasner and Pope 1988), which divided the user interface problem into three parts.
The parts were referred to as
a data model, which contains the computational parts of the program;
the view, which presented the user interface;
and the controller, which interacted between the user and the view.
Each of these aspects of the problem is a separate object, and each has its own rules for managing its data. Communication among the user, the GUI, and the data should be carefully controlled, and this separation of functions accomplished that very nicely. Three objects talking to each other using this restrained set of connections is an example of a powerful design pattern.

I think that's enough for now we will begin our discussion of the first design pattern in the next article.
Thanks for reading, and enjoy your time.

Friday, February 8, 2008

Welcome to PHP world

In the name of Allah

this is the second but it should be the third series on this blog but my friend adel is some kind late
so we will start learning php from installing to ...... as far as the author of the book go
first : installation
there are a hundreds of ways i choose the easiest one just go to http://sourceforge.net/projects/quickeasyphp/
and download easyphp then install it , congratulation u finished the first step
it works good for windows i don't know what about Linux
for the setup for Linux just contact me
after installation you will find an icon behind the clock right click on it choose local web it will open ur browser with a page have the files u have in folder www this folder located in C:\Program Files\EasyPHP 2.0b1\www but it depend on where u installed the easyphp so any file u make and want to test u move it to the www folder and open it from local web
second : introduction to php
now lest talk about php its the most powerful scripting language it's free , open scource ,and dynamic
in this series we will use php 5 and hope u make php 6
and we wont talk about just php alone but we will talk about AMP
Apache "Server" , Mysql "Database" , and the Php
it some kind like a restaurant the waiter is php , the chief is apache and it get the ingredients from mysql
third : start
u write the php code within the html code and name the file *.php or *.html but using easyphp it just work with *.PHP
let's see an example
< html>< body>
< ?php
echo "that's my first page ";
?>
< /body>< /html>
first just open the php tag and after u finish u close it
and remmber to end ur statment with semicolon
u can open it more than one time and u can use html inside the tag of php
like this
< ?php
echo "< h1>hello< /h1 >";
echo "< h2>bye< /h2 >";
?>
echo is used to print some thing like text or variables or html tags
like this
echo "< br/>"; to make a new line
Constants :
its like constants any where u make it define it cant change it again but can reuse it print it
define(); is builtin function to make contants
it take arguments the constant name and it's value
like
define("year","1998");
year is constant and its value is 1998
Variables:
it start with $ and you can make it give it a value and reset it change it's value call it or display and without declaration
u can do any thing with it like
$year=1998;
here u make a variable and give it a value
in php 5 all variables passed by reference
some built in function:
rand(min,max); it takes 2 values and give a random number between them
ceil(number); it take a number and round a decimal up to the next highest integer
floor(number); it take a number and round a decimal down to the next lowest integer
number_format(number,number,separator,separator);
it can take 1 , 2 ,or 4 parameters "no 3 parameters"
one parameter it separate the thousand with a comma and round the decimal
12345.23->12,345
two parameters it take the number and the number of digit after the decimal point
number_format(1234.4,3); ->1234.400
four parameters they are( number , the number of digits after the decimal point , the separator sign for the thousands and the separator sign for the float)
max(n,n1,n1,.....); return the maximum
min(n,n1,n1,..... ); return the minimum
and the next episode we will talk about passing variable between pages

C# language enhancements in .NET 3.5 (Part 2)

If any one read my first article he will know that that's
the second part of the language enhancements in C# 3.5 if not
and that is what I'm sure of, so you know that now.
Let's continue our discussion directly.

- Extending types using Extension method
Extension method allows us to extend any type
without the need to sub-classing it,
in other words we can add more functionalities to types
to which we don't have the source code.

Example:

//this example will add another method to the string class
//this method will check if the string contains your name or not

public static class Extensions
{
public static bool IsMyNameHere(this string str)
{
if (str.Contains("your name"))
return true;
return false;
}
}

//in the main method we can use that by the following syntax
static class Program
{
static void main()
{
string str= "blah blah blah";
bool IsMyNameHere;

IsMyNameHere= str.IsMyNameHere();
IsMyNameHere= "another blah blah".IsMyNameHere();
}
}


As you can see in the previous example you can easily add a new method to a class that you don't have its source code you can here use the IsMyNameHere() method
the same way you use ToUpper() method. You can now easily extend that framework you use daily and use your own methods on someone else’s class.

Notice that to define your extension method you need some steps
1-a public static class that contains your extension methods
2-your extension method must be a static method
3-the first word in the parameters of your extension method should be this keyword
4-also the second word in the parameters of your extension method should be the name of the class which you extend or which you add this method to.

Extension methods can be added to any type, including the generic types such as List and Dictionary.
You can also create an extension method that takes any number of parameters.
//the signature of the method will be like that
public static bool IsMyNameHere (this string str,int i)
//you can use it like that
bool IsMyNameHere = str.IsMyNameHere(0);

I know this is very short post but that's because i'm very lazy and want to sleep now
keep smile and wait for the next thread in this series.