Design your C # Program (4)

Source: Internet
Author: User

Use the design mode to solidify your C # Program (Part 1)

Design Patterns: Solidify Your C # Application Architecture with Design Patterns Chinese edition (Part 1)
Author: Samir Bajaj
Translator: glory
Composite

The composite mode comes in handy when you need to process clustered objects and individual objects in a consistent way. A common example is to list folder content. Folders may not only contain files, but may also contain subfolders. Applications that recursively list a top-level folder can use conditional statements to distinguish between files and subfolders, and print all files in subfolders by traversing the directory tree.

A better solution to this problem is to use the composite mode. In this way, every project in a folder, whether it is a file, subfolders, a network printer, or an alias for any directory element, is an instance of a class that follows the same interface, this interface provides a method to describe the user-friendly names of these elements. In this way, the customer application does not have to distinguish every different element, which also reduces the complexity of the application logic.

Another example is that I want to use the C # language below. It is a drawing application. It extracts basic and combined graphic elements from the object database and draws them on the canvas. Assume that the database can accommodate Line, Circle, and Drawing (including Line and Circle ). Let's take a look at the interface shown in table 7.

Table 7

Interface Shape
{
Void Draw ();
}

The interface Shape has a method Draw. Simple graphic objects such as Line can implement this interface. The reload method Draw [NOTE: For interfaces in C # And their implementations, the virtual or override keyword is not required, so it is not so much a "reload ", to draw a line on the canvas. See table 8.

Table 8

Class Line: Shape
{
Private double x1, y1, x2, y2;
Public Line (double x1, double y1, double x2, double y2)
{
This. x1 = x1; this. y1 = y1;
This. x2 = x2; this. y2 = y2;
}
Public void Draw ()
{
// Draw a line from (x1, y1) to (x2, y2)
}
}

The Circle class is similar to the Line class. To ensure consistent processing of clustering classes and simple object classes, the Shape interface should also be implemented for clustering objects. Drawing is a collection class of graphic objects. It implements the Draw method and lists all the basic graphic objects it contains and draws them one by one. The code in Table 9 shows how it works.

Table 9

Class Drawing: Shape
{
Private ArrayList shapes;
Public Drawing ()
{
Shapes = new ArrayList ();
}
Public void Add (Shape s)
{
Shapes. Add (s );
}
Public void Draw ()
{
IEnumerator enumerator = shapes. GetEnumerator ();
While (enumerator. MoveNext ())
(Shape) enumerator. Current). Draw ();
}
}

Note: you do not need to care whether a graphic object belongs to the basic type or set type. The public interface that spans them enables us to add a new object to it, the new type of object, without any impact on the customer program.


Through the public interface, the composite mode avoids the need for the customer program to distinguish between individual objects and container objects, which promotes code reuse and simplifies the customer program logic to a considerable extent.
Note that the Draw method of the Drawing class uses the class in the System. Collections namespace. For more information about class libraries, see. NET Framework SDK documentation.

The following is a complete example of the composite mode.

C # example:
Using System;
Using System. Collections;
Interface Shape
{
Void Draw ();
}
Class Line: Shape
{
Private double x1, y1, x2, y2;
Public Line (double x1, double y1, double x2, double y2)
{
This. x1 = x1;
This. y1 = y1;
This. x2 = x2;
This. y2 = y2;
}
Public void Draw ()
{
File: //Draw a line from (x1, y1) to (x2, y2)
Console. WriteLine ("Drawing a line ");
}
}
Class Circle: Shape
{
Private double x, y, r;
Public Circle (double x, double y, double radius)
{
This. x = x;
This. y = y;
This. r = r;
}
Public void Draw ()
{
File: // use(X, y) is the center of the circle, and r is the radius to draw a circle.
Console. WriteLine ("Drawing a circle ");
}
}
Class Drawing: Shape
{
Private ArrayList shapes;
Public Drawing ()
{
Shapes = new ArrayList ();
}
Public void Add (Shape s)
{
Shapes. Add (s );
}
Public void Draw ()
{
IEnumerator enumerator = shapes. GetEnumerator ();
While (enumerator. MoveNext ())
(Shape) enumerator. Current). Draw ();
}
}
Class Application
{
Public static void Main ()
{
Shape [] array = new Shape [3];
Array [0] = new Line (0, 0, 10, 12 );
Array [1] = new Circle (2, 3, 5.5 );
Drawing dwg = new Drawing ();
Dwg. Add (new Line (3, 4, 3, 5 ));
Dwg. Add (new Circle (5, 6, 7.7 ));
Array [2] = dwg;
// Draw all images. Note: You can access all objects in the same way.
For (int I = 0; I <3; ++ I)
Array [I]. Draw ();
}
}
/* The output result is as follows:
Drawing a line
Drawing a circle
Drawing a line
Drawing a circle
*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.