Tree Structure processing-combination mode (1)

Source: Internet
Author: User

Tree structures can be seen everywhere in software, such as the directory structure in the operating system, the menu in the application software, and the company organization structure in the office system, how to Use the object-oriented method to deal with this kind of tree structure is a problem that needs to be solved in the combination mode, the combination mode uses a clever design scheme to allow users to process parts of the entire tree structure or tree structure in a consistent manner, you can also process the leaf nodes (excluding the child nodes) and container nodes (including the child nodes) in the tree structure in a consistent manner ). We will learn this combination mode for processing tree structures.

 

11.1 design the framework of anti-virus software

Sunny wants to develop an anti-virus software, which can be used to disinfect a folder or a specified file. The antivirus software also provides different anti-virus methods for different types of files based on the characteristics of various types of files, such as imagefile and textfile. The overall anti-virus software framework design scheme is required.

Before introducing the initial solution proposed by Sunny developers, let's analyze the file directory structure in the operating system. For example, in the Windows operating system, the directory structure shown in 11-1 exists:

Figure 11-1 Windows directory structure

Figure 11-1 can be simplified to the tree directory structure shown in Figure 11-2:

Figure 11-2 tree directory structure

As shown in figure 11-2Files (gray nodes) and folders (white nodes) are different elements., WhereYou can include files in a folder or subfolders in a folder.. Here, we canA folder is called a container, and different types of files are its members, also known as leaf (leaf). A folder can also be used as a member of another larger folder.. If you want to operate a folder, such as searching for a file, you need to traverse the specified folder. If there is a sub-folder, open its sub-folder and continue the traversal, if it is a file, the search result is returned after judgment.

Through analysis, developers of Sunny software company decided to use object-oriented methods to perform operations on files and folders. They defined the image file, text file textfile, and folder Folder:

// To highlight the core framework code, we have simplified the implementation of the anti-virus process by importing Java. util. *; // class imagefile {private string name; Public imagefile (string name) {This. name = Name;} public void killvirus () {// simplify the code and simulate the anti-virus system. out. println ("---- Anti-Virus for image files '" + name + "'") ;}// class textfile for text files {private string name; Public textfile (string name) {This. name = Name;} public void killvirus () {// simplify the code and simulate the anti-virus system. out. println ("---- Anti-Virus for text files '" + name + "'"); }}// folder class folder {private string name; // defines the set folderlist, private arraylist <folder> folderlist = new arraylist <folder> (); // defines the imagelist set, private arraylist <imagefile> imagelist = new arraylist <imagefile> (); // defines the set textlist, private arraylist <textfile> textlist = new arraylist <textfile> (); public folder (string name) {This. name = Name;} // Add a new member of the folder type public void addfolder (Folder f) {folderlist. add (f);} // Add public void addimagefile (imagefile image) {imagelist. add (image);} // Add a new textfile member public void addtextfile (textfile text) {textlist. add (text) ;}// you must provide three different methods: removefolder (), removeimagefile (), and removetextfile () to delete members, code omitting // three different methods are required: getchildfolder (int I), getchildimagefile (int I), and getchildtextfile (int I) to obtain members. The Code omitting public void killvirus () {system. out. println ("*****" + name + "'"); // simulates antivirus activities. // If the folder is a member of the folder type, recursively call the killvirus () method of folder for (Object OBJ: folderlist) {(folder) OBJ ). killvirus ();} // if it is a member of the imagefile type, call the killvirus () method of imagefile for (Object OBJ: imagelist) {(imagefile) OBJ ). killvirus ();} // if it is a member of the textfile type, call the killvirus () method of textfile for (Object OBJ: textlist) {(textfile) OBJ ). killvirus ();}}}

Write the following client test code for testing:

Class client {public static void main (string ARGs []) {folder folder1, folder2, folder3; folder1 = new folder ("Sunny Info "); folder2 = new folder ("Image File"); folder3 = new folder ("text file"); imagefile image1, image2; image1 = new imagefile ("maid "); image2 = new imagefile ("zhangwu.gif"); textfile text1, text2; text1 = new textfile ("jiuyin Zhenjing .txt"); text2 = new textfile ("sunflower pawn .doc"); folder2.addimagefile (image1 ); folder2.addimagefile (image2); folder3.addtextfile (text1); folder3.addtextfile (text2); folder1.addfolder (folder2); folder1.addfolder (folder3); folder1.killvirus ();}}

Compile and run the program. The output result is as follows:

* *** Disinfect the 'sunny information' folder

* *** Disinfect the 'image file' folder

----Attack the image file 'Dragon girl .jpg'

----Perform anti-virus on the image file 'No. gif'

* *** Disinfect the 'text file' folder

----Attack the virus on the ghost file 'jiuyin Zhenjing .txt'

----Conduct anti-virus on the ghost file 'sunflower pawnshop .doc'

Sunny developers "succeeded" in implementing the anti-virus software framework design, but through careful analysis, we found that the design solution has the following problems:

(1) The design and implementation of folder-type folder are very complicated. You need to define multiple sets to store different types of members, in addition, different Members must be provided with methods for managing and accessing members, such as adding, deleting, and obtaining members. A large amount of redundant code exists, making system maintenance more difficult;

(2) because the system does not provide an abstraction layer, the client code must treat the folder acting as a container differently and the imagefile and textfile acting as a leaf, and cannot process them in a unified manner;

(3) The system has poor flexibility and scalability. To add new types of leaves and containers, you must modify the original code, for example, to add a new type of video file videofile to the system, you must modify the source code of the folder class. Otherwise, you cannot add a video file to a folder.

In the face of the above problems, how should developers of Sunny software solve them? This requires the combination mode described in this chapter,The combination mode provides a perfect solution for processing tree structures. It describes how to recursively combine containers and leaves so that users do not need to distinguish them during use, containers and leaves can be treated in a consistent manner..

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.