Design Patterns for all – Chapter I (Introduction to Design patterns and OOPs)

Monday, June 22, 2009

Design Patterns for all – Chapter I (Introduction to Design patterns and OOPs)
In this chapter we will see the basics of Design patterns and OOPs which will give the foundation for understanding Design patterns better. In the coming chapters we will see the Design patterns in detail with practical examples and code.

Definition:

Design Patterns are best proven techniques for a common design problem.
It is only a design technique and not code. Though code is available for almost all design patterns in all popular languages, design patterns mean the design technique alone.
Each design pattern explains a repeated problem, gives standard solution to the problem.

History:
GoF Gang of four, a four member team (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) are said to be people who brought up this idea. They wrote some patterns which are considered the base for all other patterns.

Types of Patterns:
Design Patterns are generally classified into three categories, they are:
1. Creational
2. Structural
3. Behavioral

1. Creational
They deal with object creation. They provide you the best way to create objects based on the current situation.
Example:
Singleton, Factory method, Abstract factory, Builder

2. Structural
They deal with relationship between entities. They provide you the best way to relate entities on various scenarios.
Example:
Adapter, Aggregate, Bridge

3. Behavioral
They deal with communication between objects. They provide you the best way to communicate between objects.
Example:
Chain of responsibility, Command, Interpreter

Further Classification:
Though the above three are the major classification, one more category also exists which may be considered as a sub part of the above. It is:

Concurrency
They deal with multi-threaded programs. They provide solutions for multi-thread design problems.
Example:
Balking, Double checked locking, guarded suspension

OOP Techniques and Design Patterns:
Before going into design patterns, we need to know some of the OOP (Object Oriented Programming) features, which will in turn lead to a better understanding of Design patterns.
Let us now see the OOPs features one by one,

Class:
It defines characteristics of a thing and its behavior. It is blueprint from based on which objects are created.
Example: Employee (which will have all the details about the employee like name, id, salary, etc and the methods which describes his behavior like calcSalary(),displaydetails(),etc.

Object:
Is an instance of a class. It will be exactly the same as a class but can be assigned values and used whereas a class can’t be used without creating objects.

Inheritance:
A feature in which the base class’s characteristics and behavior are derived to the child class also.
Types:
Single
Multiple

Single:
A class inherits from a single class.
Multiple:
A class inherits from more than 1 class.

Abstraction:
“Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.” Is what the Wikipedia says.

Encapsulation:
It is a feature which hides data and functions from others classes.

Polymorphism:
A feature using which more than one definition is given to a single name. It can either be function polymorphism or operator overloading.

Association:
Association defines a relationship between classes of objects which allows one object to cause another to perform an action on its behalf.

Composition:
Composition is a way to combine simple objects or data types into more complex ones.

Aggregation:
Aggregation is a form of composition where one looks at the system as a whole rather than as parts.

Design Patterns for all – Chapter I (Introduction to Design patterns and OOPs)

Design Patterns for all – Chapter I (Introduction to Design patterns and OOPs)
In this chapter we will see the basics of Design patterns and OOPs which will give the foundation for understanding Design patterns better. In the coming chapters we will see the Design patterns in detail with practical examples and code.

Definition:

Design Patterns are best proven techniques for a common design problem.
It is only a design technique and not code. Though code is available for almost all design patterns in all popular languages, design patterns mean the design technique alone.
Each design pattern explains a repeated problem, gives standard solution to the problem.

History:
GoF Gang of four, a four member team (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) are said to be people who brought up this idea. They wrote some patterns which are considered the base for all other patterns.

Types of Patterns:
Design Patterns are generally classified into three categories, they are:
1. Creational
2. Structural
3. Behavioral

1. Creational
They deal with object creation. They provide you the best way to create objects based on the current situation.
Example:
Singleton, Factory method, Abstract factory, Builder

2. Structural
They deal with relationship between entities. They provide you the best way to relate entities on various scenarios.
Example:
Adapter, Aggregate, Bridge

3. Behavioral
They deal with communication between objects. They provide you the best way to communicate between objects.
Example:
Chain of responsibility, Command, Interpreter

Further Classification:
Though the above three are the major classification, one more category also exists which may be considered as a sub part of the above. It is:

Concurrency
They deal with multi-threaded programs. They provide solutions for multi-thread design problems.
Example:
Balking, Double checked locking, guarded suspension

OOP Techniques and Design Patterns:
Before going into design patterns, we need to know some of the OOP (Object Oriented Programming) features, which will in turn lead to a better understanding of Design patterns.
Let us now see the OOPs features one by one,

Class:
It defines characteristics of a thing and its behavior. It is blueprint from based on which objects are created.
Example: Employee (which will have all the details about the employee like name, id, salary, etc and the methods which describes his behavior like calcSalary(),displaydetails(),etc.

Object:
Is an instance of a class. It will be exactly the same as a class but can be assigned values and used whereas a class can’t be used without creating objects.

Inheritance:
A feature in which the base class’s characteristics and behavior are derived to the child class also.
Types:
Single
Multiple

Single:
A class inherits from a single class.
Multiple:
A class inherits from more than 1 class.

Abstraction:
“Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.” Is what the Wikipedia says.

Encapsulation:
It is a feature which hides data and functions from others classes.

Polymorphism:
A feature using which more than one definition is given to a single name. It can either be function polymorphism or operator overloading.

Association:
Association defines a relationship between classes of objects which allows one object to cause another to perform an action on its behalf.

Composition:
Composition is a way to combine simple objects or data types into more complex ones.

Aggregation:
Aggregation is a form of composition where one looks at the system as a whole rather than as parts.

Design Patterns for all – Chapter I (Introduction to Design patterns and OOPs)

Design Patterns for all – Chapter I (Introduction to Design patterns and OOPs)
In this chapter we will see the basics of Design patterns and OOPs which will give the foundation for understanding Design patterns better. In the coming chapters we will see the Design patterns in detail with practical examples and code.

Definition:

Design Patterns are best proven techniques for a common design problem.
It is only a design technique and not code. Though code is available for almost all design patterns in all popular languages, design patterns mean the design technique alone.
Each design pattern explains a repeated problem, gives standard solution to the problem.

History:
GoF Gang of four, a four member team (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) are said to be people who brought up this idea. They wrote some patterns which are considered the base for all other patterns.

Types of Patterns:
Design Patterns are generally classified into three categories, they are:
1. Creational
2. Structural
3. Behavioral

1. Creational
They deal with object creation. They provide you the best way to create objects based on the current situation.
Example:
Singleton, Factory method, Abstract factory, Builder

2. Structural
They deal with relationship between entities. They provide you the best way to relate entities on various scenarios.
Example:
Adapter, Aggregate, Bridge

3. Behavioral
They deal with communication between objects. They provide you the best way to communicate between objects.
Example:
Chain of responsibility, Command, Interpreter

Further Classification:
Though the above three are the major classification, one more category also exists which may be considered as a sub part of the above. It is:

Concurrency
They deal with multi-threaded programs. They provide solutions for multi-thread design problems.
Example:
Balking, Double checked locking, guarded suspension

OOP Techniques and Design Patterns:
Before going into design patterns, we need to know some of the OOP (Object Oriented Programming) features, which will in turn lead to a better understanding of Design patterns.
Let us now see the OOPs features one by one,

Class:
It defines characteristics of a thing and its behavior. It is blueprint from based on which objects are created.
Example: Employee (which will have all the details about the employee like name, id, salary, etc and the methods which describes his behavior like calcSalary(),displaydetails(),etc.

Object:
Is an instance of a class. It will be exactly the same as a class but can be assigned values and used whereas a class can’t be used without creating objects.

Inheritance:
A feature in which the base class’s characteristics and behavior are derived to the child class also.
Types:
Single
Multiple

Single:
A class inherits from a single class.
Multiple:
A class inherits from more than 1 class.

Abstraction:
“Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.” Is what the Wikipedia says.

Encapsulation:
It is a feature which hides data and functions from others classes.

Polymorphism:
A feature using which more than one definition is given to a single name. It can either be function polymorphism or operator overloading.

Association:
Association defines a relationship between classes of objects which allows one object to cause another to perform an action on its behalf.

Composition:
Composition is a way to combine simple objects or data types into more complex ones.

Aggregation:
Aggregation is a form of composition where one looks at the system as a whole rather than as parts.

Part II (A startup lesson on using FxCop)

Monday, June 15, 2009

Part II (A startup lesson on using FxCop)

In the previous article (FxCop Part I – Introduction), we saw the basics of FxCop , its definition, download path, comparison with other Code analysis tools, a simple demo for doing a analysis,etc.

In this article (Part II -A startup lesson on using FxCop), we are going to see the usage of FxCop at a beginner’s level.

As we saw in the previous article two things are required for doing any analysis in FxCop, they are:
· Assembly ( either a .exe or .dll)
· Rule

A brief explanation about the windows and menus in FxCop
Run FxCop by clicking , Start > All Programs > Microsoft FxCop > Microsoft FxCop 1.36
After the FxCop window was opened,
Click File > New Project.
The following Figure 1.0 shows the FxCop tool,
Fig 1.0


Panes in FxCop
There are 3 panes in the tool, they are:
· Main Pane
· Messages Pane
· Output Pane

Main Pane
Figure 1.1 shows the Main Pane,
Fig 1.1
It will have 2 tabs in it, they are
· Targets
· Rules
These two tabs serve the respective purpose of showing and managing targets and Rules in the current project.

Messages Pane
This Pane shows the messages that are generated by the FxCop tool after doing the Code analysis and it will be of various categories.
Figure 1.2 shows the Messages pane.
Fig 1.2
It will have 4 tabs in it they are,
· Active
· Exclude in Project
· Exclude in Source
· Absent
The usage of these we will see in the later articles.

Output Pane
This will show the properties in code format when an item is clicked on the Main Pane.
Figure 1.3 shows the Output Pane,
Fig 1.3

Menus
For the initial level, it is enough to know about the 3 menus in the tool. They are
1. File > New Project
It is used to create a new project.
2. Project > Add Targets
It is used to add assemblies from the Project on which you want to do the analysis.
3. Project > Analyze
To start the analysis, you need to click this or press F5 or click Analyze button on the toolbar.

A simple analysis:
1. Run FxCop.
Start > All Programs > Microsoft FxCop > Microsoft FxCop 1.36
2. File > New Project.
3. Project > Add Targets.
4. Select the desired assembly you want to analyze (it can either be a .dll or an .exe).
For example, I am selecting here a .net application (Which was downloaded from internet)
5. If you have selected multiple assemblies then select any one of them (the one you want to analyze first) and then click Project > Analyze or press F5 or Click Analyze from toolbar.
6. Analyzing with introspection Engine dialog box will appear when the analysis is under process.
7. Then it will display list of messages in the right pane.
The resultant window is what is shown in the Figure 1.0. The messages shown will be of the following categories, they are:
· Critical error
This means that it is an major issue and need to be corrected.
· Warning
This means it’s just a warning and not that much serious, but if corrected the it will be good.

More details about the messages:
Double click on the respective message, then Message Details window will appear, which gives a detailed explanation about the message along with other options.
That window will have 5 tabs in it which elaborates about he message and its dependencies.
Figure 2.0 shows the Message Details window,
Fig 2.0

Stopping the Analysis
Click the Cancel button on the Analyzing with introspection Engine dialog box to cancel the Analysis in the middle.
Viewing a summary of the Analysis
Click Project > Analysis Summary, it will display a summary of the analysis done.
Figure 2.1 shows the Analysis Summary window,
Fig 2.0

Saving the Project
After the analysis is over , save the project before saving the Message XML file otherwise it will throw an error.

Note:
Moreover save the Project file in the same directory of the Assembly for which the code analysis is done.
To save the project click,
File > Save Project.

Saving the Report
After saving the Project file, you a save the report file wherever you want.
To save the Report click,
File > Save Report
This might have given a basic idea about the usage and components of FxCop. We shall see more in the coming Chapters.

Part II (A startup lesson on using FxCop)

Part II (A startup lesson on using FxCop)

In the previous article (FxCop Part I – Introduction), we saw the basics of FxCop , its definition, download path, comparison with other Code analysis tools, a simple demo for doing a analysis,etc.

In this article (Part II -A startup lesson on using FxCop), we are going to see the usage of FxCop at a beginner’s level.

As we saw in the previous article two things are required for doing any analysis in FxCop, they are:
· Assembly ( either a .exe or .dll)
· Rule

A brief explanation about the windows and menus in FxCop
Run FxCop by clicking , Start > All Programs > Microsoft FxCop > Microsoft FxCop 1.36
After the FxCop window was opened,
Click File > New Project.
The following Figure 1.0 shows the FxCop tool,
Fig 1.0


Panes in FxCop
There are 3 panes in the tool, they are:
· Main Pane
· Messages Pane
· Output Pane

Main Pane
Figure 1.1 shows the Main Pane,
Fig 1.1
It will have 2 tabs in it, they are
· Targets
· Rules
These two tabs serve the respective purpose of showing and managing targets and Rules in the current project.

Messages Pane
This Pane shows the messages that are generated by the FxCop tool after doing the Code analysis and it will be of various categories.
Figure 1.2 shows the Messages pane.
Fig 1.2
It will have 4 tabs in it they are,
· Active
· Exclude in Project
· Exclude in Source
· Absent
The usage of these we will see in the later articles.

Output Pane
This will show the properties in code format when an item is clicked on the Main Pane.
Figure 1.3 shows the Output Pane,
Fig 1.3

Menus
For the initial level, it is enough to know about the 3 menus in the tool. They are
1. File > New Project
It is used to create a new project.
2. Project > Add Targets
It is used to add assemblies from the Project on which you want to do the analysis.
3. Project > Analyze
To start the analysis, you need to click this or press F5 or click Analyze button on the toolbar.

A simple analysis:
1. Run FxCop.
Start > All Programs > Microsoft FxCop > Microsoft FxCop 1.36
2. File > New Project.
3. Project > Add Targets.
4. Select the desired assembly you want to analyze (it can either be a .dll or an .exe).
For example, I am selecting here a .net application (Which was downloaded from internet)
5. If you have selected multiple assemblies then select any one of them (the one you want to analyze first) and then click Project > Analyze or press F5 or Click Analyze from toolbar.
6. Analyzing with introspection Engine dialog box will appear when the analysis is under process.
7. Then it will display list of messages in the right pane.
The resultant window is what is shown in the Figure 1.0. The messages shown will be of the following categories, they are:
· Critical error
This means that it is an major issue and need to be corrected.
· Warning
This means it’s just a warning and not that much serious, but if corrected the it will be good.

More details about the messages:
Double click on the respective message, then Message Details window will appear, which gives a detailed explanation about the message along with other options.
That window will have 5 tabs in it which elaborates about he message and its dependencies.
Figure 2.0 shows the Message Details window,
Fig 2.0

Stopping the Analysis
Click the Cancel button on the Analyzing with introspection Engine dialog box to cancel the Analysis in the middle.
Viewing a summary of the Analysis
Click Project > Analysis Summary, it will display a summary of the analysis done.
Figure 2.1 shows the Analysis Summary window,
Fig 2.0

Saving the Project
After the analysis is over , save the project before saving the Message XML file otherwise it will throw an error.

Note:
Moreover save the Project file in the same directory of the Assembly for which the code analysis is done.
To save the project click,
File > Save Project.

Saving the Report
After saving the Project file, you a save the report file wherever you want.
To save the Report click,
File > Save Report
This might have given a basic idea about the usage and components of FxCop. We shall see more in the coming Chapters.

Part II (A startup lesson on using FxCop)

Part II (A startup lesson on using FxCop)

In the previous article (FxCop Part I – Introduction), we saw the basics of FxCop , its definition, download path, comparison with other Code analysis tools, a simple demo for doing a analysis,etc.

In this article (Part II -A startup lesson on using FxCop), we are going to see the usage of FxCop at a beginner’s level.

As we saw in the previous article two things are required for doing any analysis in FxCop, they are:
· Assembly ( either a .exe or .dll)
· Rule

A brief explanation about the windows and menus in FxCop
Run FxCop by clicking , Start > All Programs > Microsoft FxCop > Microsoft FxCop 1.36
After the FxCop window was opened,
Click File > New Project.
The following Figure 1.0 shows the FxCop tool,
Fig 1.0


Panes in FxCop
There are 3 panes in the tool, they are:
· Main Pane
· Messages Pane
· Output Pane

Main Pane
Figure 1.1 shows the Main Pane,
Fig 1.1
It will have 2 tabs in it, they are
· Targets
· Rules
These two tabs serve the respective purpose of showing and managing targets and Rules in the current project.

Messages Pane
This Pane shows the messages that are generated by the FxCop tool after doing the Code analysis and it will be of various categories.
Figure 1.2 shows the Messages pane.
Fig 1.2
It will have 4 tabs in it they are,
· Active
· Exclude in Project
· Exclude in Source
· Absent
The usage of these we will see in the later articles.

Output Pane
This will show the properties in code format when an item is clicked on the Main Pane.
Figure 1.3 shows the Output Pane,
Fig 1.3

Menus
For the initial level, it is enough to know about the 3 menus in the tool. They are
1. File > New Project
It is used to create a new project.
2. Project > Add Targets
It is used to add assemblies from the Project on which you want to do the analysis.
3. Project > Analyze
To start the analysis, you need to click this or press F5 or click Analyze button on the toolbar.

A simple analysis:
1. Run FxCop.
Start > All Programs > Microsoft FxCop > Microsoft FxCop 1.36
2. File > New Project.
3. Project > Add Targets.
4. Select the desired assembly you want to analyze (it can either be a .dll or an .exe).
For example, I am selecting here a .net application (Which was downloaded from internet)
5. If you have selected multiple assemblies then select any one of them (the one you want to analyze first) and then click Project > Analyze or press F5 or Click Analyze from toolbar.
6. Analyzing with introspection Engine dialog box will appear when the analysis is under process.
7. Then it will display list of messages in the right pane.
The resultant window is what is shown in the Figure 1.0. The messages shown will be of the following categories, they are:
· Critical error
This means that it is an major issue and need to be corrected.
· Warning
This means it’s just a warning and not that much serious, but if corrected the it will be good.

More details about the messages:
Double click on the respective message, then Message Details window will appear, which gives a detailed explanation about the message along with other options.
That window will have 5 tabs in it which elaborates about he message and its dependencies.
Figure 2.0 shows the Message Details window,
Fig 2.0

Stopping the Analysis
Click the Cancel button on the Analyzing with introspection Engine dialog box to cancel the Analysis in the middle.
Viewing a summary of the Analysis
Click Project > Analysis Summary, it will display a summary of the analysis done.
Figure 2.1 shows the Analysis Summary window,
Fig 2.0

Saving the Project
After the analysis is over , save the project before saving the Message XML file otherwise it will throw an error.

Note:
Moreover save the Project file in the same directory of the Assembly for which the code analysis is done.
To save the project click,
File > Save Project.

Saving the Report
After saving the Project file, you a save the report file wherever you want.
To save the Report click,
File > Save Report
This might have given a basic idea about the usage and components of FxCop. We shall see more in the coming Chapters.

FxCop – Part I (Introduction)

FxCop – Part I (Introduction)

This will be a series of articles on FxCop, which explains from the basics to advanced usage of FxCop. This article is the first of them which gives an introduction to FxCop.

Definition
It is a code analysis tool which analyzes the code and gives suggestions to improve coding.

Overview
It analyzes programming elements in targets, and provides an informational report that contains messages about the targets;
It can analyze either a dll or an exe.

Target
They are nothing but managed assemblies.

Message
Messages include suggestions about how to improve the source code used to generate them.

Rule
A rule is a code which analyses and finds defects and informs about it.

For analysis 2 things are required,
1. Target assemblies
2. Rules

Running FxCop
· From GUI
After installation, you will have shortcut in Programs menu; you can start FxCop by clicking the link.
· Command Line
You can run FxCop.exe with respective command line arguments to start it from command line.

Using FxCop you can do the following:
· Manage Rules (decide which rules need to be applied to which assembly)
· Apply Styles to Reports.
· Filter and save messages.
· And also you can save and reuse application settings.

Download Link
www.microsoft.com/Downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772&displaylang=en

Similar Code Analysis tools:
StyleCop is another tool which does code analysis.

Difference between FxCop and StyleCop:
FxCop does code analysis over the binaries, whereas StyleCop does code analysis over the source code itself.

Latest Versions:
FxCop : ver. 1.36
StyleCop : ver. 4.3.1.3

Steps to do an analysis:
1. Run FxCop.
2. File > New Project.
3. Project > Add Targets.
4. Select the desired assembly you want to analyze(it can either be a dll or an exe).
5. If you have selected multiple assemblies then select any one of them ( the one you want to analyze first) and then click Project > Analyze or press F5.
6. Then it will display list of messages in the right pane.
Blog Widget by LinkWithin