Introduction
Class Diagrams are newly added in Visual Studio 2005 edition. They provide designers, architects and developers a graphical interface to manipulate methods, properties, fields, events for datastructures likeclasses,interfaces, structs,etc. In this article we will use this designer to create these datastructures without touching the code and while we are interacting with this designer we should note that the code will be generated and updated in the background.
Let us now explore all the features of Class designer.
Creating Classes using Class Diagrams
1. Create a new Class Library called MyLib and add a class Diagram called MyClassDiagram to it as shown below.
2. Lets now add a new class called Computer to it. To add a class using the class designer right click in theclass diagram. On right click you will see a popup menu as shown below. Select Add --> Class
.
3."Add New Class" dialogue will be displayed. We will enter our Classname as "Computer", select the access as public and select the option to create a new file called as Computer.cs for our class. After entering all the details click on "OK".
4.Our Class "Computer" will be added to the class diagram. Let us now add Method,Properties,Events and fields to out class.Select the class and Right Click on it to view the popup menu as shown below.
5. Select the appropriate option to add Method,Property,Field or event. Please note an option to add a constructor and destructor is also given. Let us now add property called Monitor. Right click on the class select Add --> Field. Add a field called as monitor as shown below.
6.After adding the field select the field, right click and select Encapsulate Field. Encapsulate Field dialogue will be displayed as shown below. Input the Property name as Monitor and check Preview Reference Changes and click on "OK". AFter clicking "OK" Preview Reference Changes diaglogue is displayed. Click on "Apply" and Monitor property will be added.
Now let us see the code.
using System;
using System.Collections.Generic;
using System.Text;
namespace MyLib
{
public class Computer
{
private int monitor;
public int Monitor
{
get { return monitor; }
set { monitor = value; }
}
}
}
One obvious question that should come to your mind should be why didnt we add the property using add property option. The reason behind this is, using the add property option will only add a non-implemented property without any field.
Now let us add some methods to our class called Boot,LoadOS and LoginUser by selecting the class,right click and Add --> Method. After adding all the methods our class
Now let us add some methods to our class called Boot,LoadOS and LoginUser by selecting the class,right click and Add --> Method. After adding all the methods our class
will appear as follows :-
7. After adding the methods add the code will be
using System;
using System.Collections.Generic;
using System.Text;
namespace MyLib
{
public class Computer
{
private int monitor;
public int Monitor
{
get { return monitor; }
set { monitor = value; }
}
public void LoginUser()
{
throw new System. NotImplementedException();
}
public void Boot()
{
throw new System. NotImplementedException();
}
public void LoadOS()
{
throw new System. NotImplementedException();
}
}
}
8. Let us now change the Inheritance Modifier of our "Computer" class to abstract as shown below :
9.After the Computer class is made abstract note that the name of the class is displayed in Italics and belowclass name "Abstract Class" is displayed.
10, The is an old design saying code to an interface not to a class so let us create an interface called IComputer for our Class Computer. With Class Designer we can do this easily, VS Class Designer provides us with a opion called "Extract Interface",
11. RightClick on the class , select eXtract interface option. Extract Interface dialogue will be displayed. The Interface name should be displayed as IComputer and all the public members of the class will be displayed. Let us select all the methods displayed as click on ok. This interface will be created in a new file called IComputer.cs as shown below.
Let us view the code
using System;
namespace MyLib
{
interface IComputer
{
void Boot();
void LoadOS();
void LoginUser();
int Monitor { get; set; }
}
}
12. If you note the interface added does not have a class view. In order to view all the methods for the interface select the interface, right click and select show interface.
13. Let us now add 2 more classes called Laptop and Tablet. These two classes will inherit from the abstractclass Computer.
Let us inherit both of them from Computer, Go to Main Menu View ---> Toolbox and select inheritance.
Let us inherit both of them from Computer, Go to Main Menu View ---> Toolbox and select inheritance.
14. Click on the Inheritance option and then first click on the derived class and then on the base class. i.e. first click on the Tablet class and then on Computer abstract base class.
Code of tablet and Laptop classes is as follows :-
namespace MyLib
{
public class Laptop : Computer
{
}
}
namespace MyLib
{
public class Tablet : Computer
{
}
}
15.You can view a lot of shapes in the toolbox for creating class, enum,interface,abstractclass,
16.The class designer provides options to view method signatures and thier names and types. Select the ClassComputer and goto menu option called Change Members Format. The current selection should be Display Name. If we want to view member names and types we should select the 2 option and to view the complete method signature we should select option 3.
17. One last cool feature of class designer is that it allows you to export the entire diagram as an Image. Go toClass Diagram and select the option "Export Diagram as Image" as shown below.
Conculsion
We have seen how to work with Classes and interfaces today. During this entire exercise we did not type any code. All the classes and interfaces, their methods,properties,fields were added through class designer without
No comments :
Post a Comment