Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
26
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

Chapter 13 Creating Interfaces and Defining Abstract Classes

247

Implementing an Extensible Framework

In the following exercise, you will familiarize yourself with a hierarchy of interfaces and

classes that together implement a simple framework for reading a C# source file and classifying its contents into tokens (identifiers, keywords, operators, and so on). This framework

performs some of the tasks that a typical compiler might perform. The framework provides a mechanism for “visiting” each token in turn, to perform specific tasks. For example, you could create:

A displaying visitor class that displays the source file in a rich text box.

A printing visitor class that converts tabs to spaces and aligns braces correctly.

A spelling visitor class that checks the spelling of each identifier.

A guideline visitor class that checks that public identifiers start with a capital letter and that interfaces start with the capital letter I.

A complexity visitor class that monitors the depth of the brace nesting in the code.

A counting visitor class that counts the number of lines in each method, the number of members in each class, and the number of lines in each source file.

Note This framework implements the Visitor pattern, first documented by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides in Design Patterns: Elements of Reusable Object-Oriented Software (Addison Wesley Longman, 1995).

Understand the inheritance hierarchy and its purpose

1.Start Microsoft Visual Studio 2008 if it is not already running.

2.Open the Tokenizer project, located in the \Microsoft Press\Visual CSharp Step by Step\ Chapter 13\Tokenizer folder in your Documents folder.

3.Display the SourceFile.cs file in the Code and Text Editor window.

The SourceFile class contains a private array field named tokens that looks like this and is essentially a hard-coded version of a source file that has already been parsed and tokenized:

private IVisitableToken[] tokens =

{

new KeywordToken(“using”), new WhitespaceToken(“ “),

new IdentifierToken(“System”), new PunctuatorToken(“;”),

...

};

248 Part II Understanding the C# Language

The tokens array contains a sequence of objects that all implement the IVisitableToken interface (which is explained shortly). Together, these tokens simulate the tokens of a

simple “hello, world” source file. (A complete compiler would parse a source file, identify the type of each token, and dynamically create the tokens array. Each token would be created using the appropriate class type, typically through a switch statement.) The SourceFile class also contains a public method named Accept. The SourceFile.Accept method has a single parameter of type ITokenVisitor. The body of the SourceFile.Accept method iterates through the tokens, calling their Accept methods. The Token.Accept

method will process the current token in some way, according to the type of the token:

public void Accept(ITokenVisitor visitor)

{

foreach (IVisitableToken token in tokens)

{

token.Accept(visitor);

}

}

In this way, the visitor parameter “visits” each token in sequence. The visitor parameter is an instance of some visitor class that processes the token that the visitor object visits. When the visitor object processes the token, the token’s own class methods come into

play.

4.Display the IVisitableToken.cs file in the Code and Text Editor window.

This file defines the IVisitableToken interface. The IVisitableToken interface inherits from two other interfaces, the IVisitable interface and the IToken interface, but does not define any methods of its own:

interface IVisitableToken : IVisitable, IToken

{

}

5.Display the IVisitable.cs file in the Code and Text Editor window.

This file defines the IVisitable interface. The IVisitable interface declares a single method named Accept:

interface IVisitable

{

void Accept(ITokenVisitor visitor);

}

Each object in the array of tokens inside the SourceFile class is accessed using the IVisitableToken interface. The IVisitableToken interface inherits the Accept method, and each token implements the Accept method. (Recall that each token must implement the Accept method because any class that inherits from an interface must implement all the

methods in the interface.)

6. On the View menu, click Class View.

Chapter 13 Creating Interfaces and Defining Abstract Classes

249

The Class View window appears in the pane used by Solution Explorer. This window displays the namespaces, classes, and interfaces defined by the project.

7.In the Class View window, expand the Tokenizer project, and then expand the {} Tokenizer namespace. The classes and interfaces in this namespace are listed. Notice the different icons used to distinguish interfaces from classes.

Expand the IVisitableToken interface, and then expand the Base Types node. The interfaces that the IVisitableToken interface extends (IToken and IVisitable) are displayed, like this:

8.In the Class View window, right-click the IdentifierToken class, and then click Go To Definition to display this class in the Code and Text Editor window. (It is actually located in SourceFile.cs.)

The IdentifierToken class inherits from the DefaultTokenImpl abstract class and the IVisitableToken interface. It implements the Accept method as follows:

void IVisitable.Accept(ITokenVisitor visitor)

{

visitor.VisitIdentifier(this.ToString());

}

Note The VisitIdentifier method processes the token passed to it as a parameter in whatever way the visitor object sees fit. In the following exercise, you will provide an implementation of the VisitIdentifier method that simply renders the token in a particular

color.

The other token classes in this file follow a similar pattern.

250Part II Understanding the C# Language

9.In the Class View window, right-click the ITokenVisitor interface, and then click Go To Definition. This action displays the ITokenVisitor.cs source file in the Code and Text Editor window.

The ITokenVisitor interface contains one method for each type of token. The result

of this hierarchy of interfaces, abstract classes, and classes is that you can create a class that implements the ITokenVisitor interface, create an instance of this class, and pass this instance as the parameter to the Accept method of a SourceFile object. For example:

class MyVisitor : ITokenVisitor

{

public void VisitIdentifier(string token)

{

...

}

public void VisitKeyword(string token)

{

...

}

}

...

class Program

{

static void Main()

{

SourceFile source = new SourceFile(); MyVisitor visitor = new MyVisitor(); source.Accept(visitor);

}

}

The code in the Main method will result in each token in the source file calling the matching method in the visitor object.

In the following exercise, you will create a class that derives from the ITokenVisitor interface and whose implementation displays the tokens from our hard-coded source file in a rich text box in color syntax (for example, keywords in blue) by using the “visitor” mechanism.

Write the ColorSyntaxVisitor class

1.In Solution Explorer (click the Solution Explorer tab below the Class View window), double-click Window1.xaml to display the Color Syntax form in the Design View window.

Chapter 13 Creating Interfaces and Defining Abstract Classes

251

You will use this form to test the framework. This form contains a button for opening a file to be tokenized and a rich text box for displaying the tokens:

The rich text box in the middle of the form is named codeText, and the button is named

Open.

Note A rich text box is like an ordinary text box except that it can display formatted content rather than simple, unformatted text.

2.Right-click the form, and then click View Code to display the code for the form in the

Code and Text Editor window.

3.Locate the openClick method.

This method is called when the user clicks the Open button. You must implement this method so that it displays the tokens defined in the SourceFile class in the rich text box, by using a ColorSyntaxVisitor object. Add the code shown here in bold to the openClick method:

private void openClick(object sender, RoutedEventArgs e)

{

SourceFile source = new SourceFile();

ColorSyntaxVisitor visitor = new ColorSyntaxVisitor(codeText); source.Accept(visitor);

}

Remember that the Accept method of the SourceFile class iterates through all the

tokens, processing each one by using the specified visitor. In this case, the visitor is the ColorSyntaxVisitor object, which will render each token in color.

Note In the current implementation, the Open button uses just data that is hard-coded in the SourceFile class. In a fully functional implementation, the Open button would prompt

the user for the name of a text file and then parse and tokenize it into the format shown in the SourceFile class before calling the Accept method.

252Part II Understanding the C# Language

4.Open the ColorSyntaxVisitor.cs file in the Code and Text Editor window.

The ColorSyntaxVisitor class has been partially written. This class implements the ITokenVisitor interface and already contains two fields and a constructor to initialize a reference to the rich text box, named target, used to display tokens. Your task is to implement the methods inherited from the ITokenVisitor interface and also create a method that will write the tokens to the rich text box.

5.In the Code and Text Editor window, add the Write method to the ColorSyntaxVisitor class exactly as follows:

private void Write(string token, SolidColorBrush color)

{

target.AppendText(token);

int offsetToStartOfToken = -1 * token.Length - 2; int offsetToEndOfToken = -2;

TextPointer start = target.Document.ContentEnd.GetPositionAtOffset(offsetToStartOfToken);

TextPointer end = target.Document.ContentEnd.GetPositionAtOffset(offsetToEndOfToken);

TextRange text = new TextRange(start, end); text.ApplyPropertyValue(TextElement.ForegroundProperty, color);

}

This code appends each token to the rich text box identified by the target variable using the specified color. The two TextPointer variables, start and end, indicate where the

new token starts and ends in the rich text box control. (Don’t worry about how these

positions are calculated. If you’re wondering, they are negative values because they are offset from the ContentEnd property.) The TextRange variable text obtains a reference

to the portion of the text in the rich text box control displaying the newly appended token. The ApplyPropertyValue method sets the color of this text to the color specified

as the second parameter.

Each of the various “visit” methods in the ColorSyntaxVisitor class will call this Write method with an appropriate color to display color-coded results.

6.In the Code and Text Editor window, add the following methods that implement the

ITokenVisitor interface to the ColorSyntaxVisitor class. Specify Brushes.Blue for keywords, Brushes.Green for StringLiterals, and Brushes.Black for all other methods. (Brushes is a class defined in the System.Windows.Media namespace.) Notice that this code implements the interface explicitly; it qualifies each method with the interface name.

void ITokenVisitor.VisitComment(string token)

{

Write(token, Brushes.Black);

}

void ITokenVisitor.VisitIdentifier(string token)

{

Write(token, Brushes.Black);

Chapter 13 Creating Interfaces and Defining Abstract Classes

253

}

void ITokenVisitor.VisitKeyword(string token)

{

Write(token, Brushes.Blue);

}

void ITokenVisitor.VisitOperator(string token)

{

Write(token, Brushes.Black);

}

void ITokenVisitor.VisitPunctuator(string token)

{

Write(token, Brushes.Black);

}

void ITokenVisitor.VisitStringLiteral(string token)

{

Write(token, Brushes.Green);

}

void ITokenVisitor.VisitWhitespace(string token)

{

Write(token, Brushes.Black);

}

It is the class type of the token in the token array that determines which of these methods is called through the token’s override of the Token.Accept method.

Tip You can either type these methods into the Code and Text Editor window directly or

use Visual Studio 2008 to generate default implementations for each one and then modify the method bodies with the appropriate code. To do this, right-click the ITokenVisitor identifier in the class definition sealed class, ColorSyntaxVisitor : ITokenVisitor. On the shortcut menu, point to Implement Interface and then click Implement Interface Explicitly. Each method will contain a statement that throws a NotImplementedException. Replace this

code with that shown here.

7.On the Build menu, click Build Solution. Correct any errors, and rebuild if necessary.

8.On the Debug menu, click Start Without Debugging. The Color Syntax form appears.

9.On the form, click Open.

254

Part II Understanding the C# Language

The dummy code is displayed in the rich text box, with keywords in blue and string literals in green.

10. Close the form, and return to Visual Studio 2008.

Generating a Class Diagram

The Class View window is useful for displaying and navigating the hierarchy of classes and interfaces in a project. Visual Studio 2008 also enables you to generate class diagrams that depict this same information graphically. (You can also use a class diagram to add new classes and interfaces and to define methods, properties, and other class members.)

Note This feature is not available in Visual C# 2008 Express Edition.

To generate a new class diagram, on the Project menu, click Add New Item. In the Add New Item dialog box, select the Class Diagram template, and then click Add. This action

will generate an empty diagram, and you can create new types by dragging items from the Class Designer category in the Toolbox. You can generate a diagram of all exist-

ing classes by dragging them individually from the Class View window or by dragging the namespace to which they belong. The diagram shows the relationships between the classes and interfaces, and you can expand the definition of each class to show its contents. You can drag the classes and interfaces around to make the diagram more readable, as shown in the image on the following page.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]