Saigroup Squiring Series - Design Mode: Recovering Object-Oriented Software Foundation 2

zhaozj2021-02-08  232

Chapter 2 Example Research: Designing a Document Editor This chapter will introduce the practical application of design mode by designing a document editor called LE X i (or "W Y S i W Y"). We will see how the design pattern solutions to design problems in the LE X i and similar applications. In this chapter, the learning of this example will receive 8 models of practical experience. Figure 2 - 1 is the user interface of L e x i. The desired representation of the documentation is indicated by the middle large rectangular area. Documents can be freely mixed with text and graphics in different formats. Around the documentation is the usual drop-down menu and scroll bar, and some page number icons used to jump to a specific page. Figure 2-1 User Interface L E X i is designed to develop D O C based on the text editing of C A L D e R. [C L 9 2] Chapter 2 Example Research: Design a Document Editor 2 3

2.1 Design Issues We will examine 7 issues in LE X i: 1) The document structure has almost impacts each aspect of the LE X i design in the document structure. All editors, format arrangements, display, and text analysis are involved in this representation. How do we organize this information to affect other aspects of your application. 2) Format LE X i How to arrange text and graphics to rows and columns? Which objects are responsible for performing different format strategies? What is the interaction between these strategies? 3) The user interface of the modified user interface L e x i includes the scroll bar, boundary, and shadows used to modify the W Y S i w y g document interface. These modifications may vary with the evolution of the LE X i user interface. Therefore, it is very important to freely increase and remove these modifications without affecting other aspects of the application. 4) Support for a variety of vision (L O o K - a N D - f E E L) standard L e x i should adapt to different visual criteria, such as M O T I f and Presentation Manager (PM). 5) Supporting a variety of window systems Different visual criteria are usually implemented on different window systems. L e x i Design should be independent of the window system as much as possible. 6) User Operate User Controls L E X i by different user interfaces, including buttons and drop-down menus. These interfaces correspond to the functionality in the entire application object. The difficulty here is to provide a unified mechanism, which can access these dispersed functions, and can withdraw (U N D O). 7) Spell Check and Connection Character L e x i supports analysis operations such as checking spelling errors and determining even characters? When we have to add a new analysis operation, how do we try to modify the relevant classes? We will discuss these design issues in each section below. Each question has a set of associated target collections and how we reach these objectives. We will explain the goals and restrictions of design problems before given a specific solution. The problem and its solution list one or more design patterns. The discussion of each question will end after a brief introduction to the related design mode. 2.2 Document Structure Fundamentally, a document is just a arrangement of characters, lines, polygons, and other graphical elements. These elements record the entire information content of the document. However, a document author usually does not regard these elements as a graphical item, but is a physical structure of a document - rows, columns, graphics, tables, and other sub-structures. And these sub-structures also have their own sub-structure. The user interface of L e x i should allow the user to manipulate these sub-structures directly. For example, a user should be able to treat a chart as a unit, rather than a set of collambices. The user should be able to cositively reference the table, rather than the table as a bunch of text and graphics. This helps make the interface simple and intuitive. In order to make the implementation of L e x i have a similar nature, we choose the internal representation of the physical structure that matches the document. Special, internal indication should support the following: • Keep the physical structure of the document. It is about to arrange text and graphics to row, column, table, etc. • Visual generation and display documentation. • Mapping the elements indicated inside the document according to the display location. This can make L E X i determine the document elements referenced by the user according to some of the user in visualization representation. The author often comes from the logical structure to see the documentation, that is, see the sentence, paragraph, section, section and chapter. In order to make this example, our document indicates that the logical structure information is not explicitly stored. However, the design of the description we describe is equally applicable to express logic structure information. In addition to these objectives, there are some restrictions. First, we should consistent text and graphics. The application interface allows the user to be embedded in the text in the graphic, and vice versa. We should avoid a special case of text as text, or a special case of the text as a graphic. Otherwise, we finally get redundant format and manipulation mechanism. The mechanism collection should make text and graphics can be met. Second, our implementation should not excessively emphasize the difference between the single elements and elements groups in the internal representation. L e x i should be able to consistently treat simple elements and combined elements, which allows any complex documents.

For example, the first 0 elements of the 5th row of second columns may be one character, or a complex chart consisting of many sub-elements. Once we know that this element can draw itself and specify its area, how it is not difficult to display on the page and its display location is not difficult. However, in order to check the spelling errors and determine the connection point of the character, the text needs to be analyzed. This has a contradiction with the second restriction condition. We usually don't care about the elements on a line or a complex object, but the text analysis is sometimes dependent on the object being analyzed. For example, checking the spelling of the polygon or connecting it in a consecutive character. The inside of the document indicates that the design should consider and weigh this or other potential contradictory restrictions. 2.2.1 Recreasing Combined Hierarchical Structure Information is usually implemented by a technique called recursive composition. The recursive combination can be gradually established by a simpler element, which is one of the methods we construct a document through simple graphical elements. The first step, we will arrange the characters and graphics from left to right to form a line of documents, then form a column by multi-line, and then form a page, wait, see Figure 2 - 2. Figure 2-2 Recursive combination of text and graphics We represent each important element into an object, which can describe this physical structure. It includes not only visible elements such as characters, graphics, but also invisible, structured elements, such as rows and columns. The result is the object structure as shown in Figure 2 - 3. By indicating each character and graphic elements of the document with an object, we can improve the flexibility of the best design of L e x i. 2 4 Design Mode: Declaring object-oriented software base character space map combination (row) Chapter 2 Example Research: Design a Document Editor 2 5

We can treat graphics and text in terms of display, formatting, and mutual embedding. We can extend L e x i to support new character sets without affecting other features. L e x i's object structure is very similar to the physical structure of the document. Figure 2-3 Object structure of recursive combination is implied herein. The first is obvious, the object needs the corresponding class. The second is not that obvious, because we have to treat these objects consistently, so these classes must have a compatible interface. In languages ​​like C , you can associate clarifiles by inheritance, making the interface compatible. 2.2.2 Tuning We will define an abstract class element (G L y p h) to all objects that appear in the document structure. Its subclass defines both basic graphic elements (like characters and images), and define structural elements (like lines and columns). Figure 2 - 4 depicts the combination (row) combination (row) combination (row) combination (row) combination (rows) combination (rows) [C1 "[C L 9 0] in this context. Most document editing of the same time is not using an object due to efficiency because of a character. C A L D e r is the feasibility of this method in his paper [C A L 9 3]. For the sake of simplicity, we will strictly limit the charm in the class hierarchy, so there is no complicated C a L D e R. The elements of C A L D e r can also reduce storage overhead to form a directionless to ring. We can also use F L Y W E G H T (4. 6) mode to reach the same effect, we will use it as an exercise for the reader. Figure 2-4 Part of the Glyph class level 2 6 Design mode: can be used for object-oriented software

The level of the hierarchy indicates that the basic G L y p h interface is described in Table 2 - 1. Table 2-1 Basic G lyph interfaces R esponsibility O peration sA ppearance Virtual Void Draw (Wi ndow *) Virtual Void Bounds (Rect &) hit detection Virtual bool Intersects (Const Point &) S tructure Virtual Void Insert (Glyph *, int) Virtual Void Remove (Glyph *) Virtual Glyph * Child (int) Virtual Glyph * Parent () There are three basic responsibilities. They are 1) how to draw themselves, 2) How much space occupies, 3) their parent charts and subprograms Yuan is what. G l y p h Subcate in order to represent yourself on the window, redefine D R A W operation. When you call D R a W, they deliver a reference to Wi N D O W object. Wi N D O W Class For the text and basic graphics on the screen window, some graphics are defined. A child of a g lyph R Ectangle may define D RAW: Void Rectangle :: Draw (Window * W) {W-> DrawRect (_x0, _y0, _x1, _y1);} here's _ x 0, _ Y 0, _ x 1, _ y 1 is the data member of R ectangle defines the rectangle to the vertex. D R A W R E C T is Wi N D O W operation for displaying a rectangle on the screen. Parent Metail typically needs to know how large spaces need to occupy more spaces to arrange it and other primitives in a row, and ensure that they will not be covered with each other (see Figure 2 - 2). B O U N D S Operates the rectangular area occupied by the chamfer, which is returned to a diagonal vertex containing the minimum rectangle of the element. G L Y PH Different subclasses redefine this operation, returning to the rectangular area used by their respective drawings. I n t e R s E C TES operation determines if a designated point intersects with the primitive. When the user clicks somewhere in the document, the LE X i can call this operation to determine the chamber or element structure where the mouse is located. RE C T A N g L E Class Duver is defined to calculate the rectangle and the intersection of the given point. Because the primitives can have sub-primitives, we need a public interface to add, delete, and access these subunits. For example, a subunit of a row is all of the elements on the line. I N s e R T operation Insert a primitive in the position specified by integer I n d e x. RE MO V e operation removes a designated subunit. The C H i L D operation returns a subunit (if any) of the given I n d e x, and the primary image of the sub-element such that the line should be internally used to operate instead of directly access the sub-data structure. This way you use the data structure by an array to a connection table, you do not need to modify the operation of repeating the DR A W to repeat the individual subunits. Similarly, P A R e n t operation provides a standard access parent element interface. L E X i's primitive saves a guide to its parent element, and P A R e n t operation is only simply returned to this guide. In order to simplify the discussion, we use the minimized interface here. A complete interface should include a graphical property of the management color, font, and coordinate conversion, and manages the operation of more complex sub-objects. An integer I n D e x may not be the best way to specify the subunit, which relies on the data structure used in the primitive. If the primer is stored in the connection table, the use of the connection table pointer should be more efficient.

We will give a better solution for index problems at 2. 8 section. Chapter 2 Example Research: Designing a Document Editor 2 72.2.3 Combined Mode Recursive combination Not only can be used to represent documents, we can also use it to represent any potentially complex, graded structure. C O m P O S I T E (4. 3) The mode describes the nature of an object-oriented recursive combination. Now is the time when I go back to this mode and learn it, and I will refer to this scene when needed. 2.3 Formatting We have solved the representation of the document physical structure. Then, what we need to solve is how to construct a special physical structure, which corresponds to a properly formatted document. Representation and formatting are different, the ability to record the physical structure of the document does not tell us how to get a special formatting structure. This responsibility is mostly lu x i, which must decompose the text into rings, decompose the line to a list. At the same time, we must also consider the high level of users, for example, users may specify boundary width, indent size, and table form, whether to interrupted and other possible many format limits. The formatting algorithm of L e x i must consider all of these factors. Now we limit the "formatting" meaning to the decomposition of a set element into several lines. Below we can use the terms "formatted" (F O R m A T T T T T T T T T T T T T TI N G) and "Branch" (L i N E B R E A K I N G). The techniques discussed below are equally applicable to decomposition of rows as columns and will be broken down into pages. 2.3.1 Packaging Formatting Algorithms Due to all of these restrictions and many details, the formatting process is not easy to be automated. There are many solutions here, in fact, people have proposed a variety of formatting algorithms with different capabilities and defects. Because L e x i is a ready-made editor, an important weigh must be considered in the formatting quality and formatting speed. We usually want to get good reflection speeds without sacrificing the beautiful appearance of the document. This trade-off is affected by many factors, not all factors that can be determined at compilation moments. For example, the user may have to endure a slower response speed to exchange better format. This option may result in a thorough different formatting algorithm that is more applicable than the current algorithm. Another example, more implementation of the trade-off is between formatting speed and storage requirements: it is very likely to reduce the formatting speed to cache more information. Because the formatting algorithm tends to be complicated, it can be considered in the document structure, but it is best to completely independent of the document structure. Ideally, we can freely add a G L Y p H subclass without considering the format algorithm. In turn, adding a format algorithm should not be modified to modify the existing primitive class. These features require the LE X i designed to easily change the formatting algorithm. It is best to change this algorithm at runtime, if it is difficult to implement, at least in compilation time, it should be convenient to change. We can independent algorithms and package it into the object to make it easy to replace. Further, a class hierarchy of an object of an object of a package formatting an algorithm can be defined. The root node of the class hierarchy will define an interface that supports many formatting algorithms, each subclass implementing this interface to perform a particular algorithm. At that time, the G L Y PH subclass object can automatically use the given algorithm object to arrange their subunit. 2.3.2 Compositor and Composition We define a C O m P O S i T O R class for an object that can encapsulate the formatting algorithm. Its interface (see Table 2 - 2) allows users to be more concerned about the logical structure of the document - sentences, paragraphs, sections, chapters, and more. In contrast, there is no such interest in the physical structure. Most users do not have the wrap in the meaning of the descending, as long as the paragraph can be formatted correctly. The formatted column and page are also true. Therefore, the user ends only the high-level restriction conditions of the physical structure, and it is used to meet their difficult work. 2 8 Design mode: can be used for object-oriented software

C O m P O S i t o r knows when to format which elements are formatted. It is formatted to each sub-element of a particular element called C O m P O S I t I O n. A C O m P O S i t I o N gets a C O m P O S i t O R subclass example at the time of creation, and when the user changes the document, it makes C O m P O s e operation of the C O m P O S e. Fig. 2 - 5 describes the relationship between C O m P O S I t I O N-category and C O m P O S I t O R class. Table 2-2 Basic c ompositor interface responsible to formatted content void setcomposition (Composition *) When you format Virtual Void Compose () Figure 2-5 Composition and Compositor class relationship between unformatted C omposition objects only The visible element of the basic content of the document is constituted. It does not include a charm of a decision document that is like a document. The C O m P O S i t i O N object is only in this state only after initialization of the primitives that are just created and to format it. When C O m P O S I t I o N needs to format, the C O m P O S e operation of C O m P O S I t O R is called. C O m P O S i T O r sequentially traversed the individual sub-elements of C O M P O S I Ti N N, insert new rows and column chart according to the branch algorithm. Figure 2 - 6 shows the obtained object structure. The chart is created and inserted into the object structure in the picture. Figure 2-6 Object structure reflects the ranking space C O m P O S i t O r generated by the compositor-guided branch. The character code must know the character code of the character element. At 2.8, we will see: How can I add a character-specific operation in the G L Y interface, and give this information. Display with gray background. Each C O m P O S i T O R subclass can achieve a different branch algorithm. For example, a S i m p L e c o m p o s i t o r can be implemented soon, regardless of the deeper thing that is like document "color". Good color means text and blank smoothing distribution. A TE X C O M P O S i T O R will achieve a complete TEX algorithm [k n u 8 4], consider something like color, while the time is used as a cost. Separation of C O M P O S I T O R- C O M P O S IT I O N N: The code supporting the physical structure of the document and the separation of code supporting different formatting algorithms. We can add new C O m P O S i t O R subclasses without touching G L Y, and vice versa. In fact, we increase the branch algorithm at runtime by adding an S e T C O M P O S i T O R to the basic element interface of C O M P O S i Ti N n. 2.3.3 Policy Mode The Package Algorithm in Objects is the purpose of S T R A T E G Y (5. 9) mode. The main participants of the mode are S T R A T E G Y objects (different algorithms have been encapsulated in these objects) and their operating environments. In fact, C O m P O S i t O r is S T R A T E G Y. They encapsulate different format algorithms. C O m P O S i Ti O N is an environment of the C O M P O S i T O R Policies. The key point of the S t R A T E G Y mode application is to support a series of algorithms for S T R a T E G Y and its environment. You don't have to change S T R a T E G Y or its environment in order to support a new algorithm.

In our example, the basic G Lyph interface that supports sub-meta access, insert, and delete operation is sufficient to meet the general user needs, regardless of the algorithm for C OMPOSITOR subclasses, is sufficient to support its modification of the physical structure of the document. . Similarly, the C O M P O S I t O R interface is also sufficient to support the formatting of C O m P O S I t I o n. 2.4 Modification User Interface We consider two modifications for the LE X i user interface, the first is to add borders around the text editing area to define the text page; the second is that the plus roll bar allows the user to see the same page. In order to facilitate increasing and removal of these modifications (especially at runtime), we should not add them to the user interface through inheritance. If other user interfaces don't know where there is these modifications, then we have achieved the greatest flexibility. This allows us to increase and remove these modifications without changing other classes. 2.4.1 Transparent Fence From the program design angle, the modified user interface involves the expansion of the existing code. We can use inheritance to complete this expansion, but this is very difficult to rearrange these modifications. And the same serious problem is that the class inheritance method usually causes the class explosion. We can create a subclass B O R D E R E D C O M P O S i T I O N to add a boundary to C O m P O S I t I o n, or create a subclass S C R O L A B L E C O M P O S I t I O N to add a scroll bar in the same manner. If we want to scroll strips and want the boundary, you can create B O R d E R E D S C R O L1 B L E C O M P O Si I Ti O N, etc. In extreme cases, we create a class that contains various possible modifications. But once the modification is increased, it becomes invalid. Object portfolio provides a potential and more flexible extension mechanism, but what objects we combine? Since we know that there is already an existing element, we can treat the modification itself as an object (eg, instance of class B O R d E R). This way we have two combinations of candidate objects: chammeters (G L Y P h) and boundary (B O R D E R). The next step is to decide who to use who is combined. We can include primitives in the boundary, which gives people a sense of chapter 2 of the primitives on the screen on the screen: Design a document editor 2 9

feel. Alternatively, it contained in the primitive in the primary, but we must modify the corresponding G L Y P Hs to make the boundary valid for all subclasses. In our first selection, you can completely save the code of the border in the B O R D e R class, but independent of other classes. What is the B o R d e R? Boundary tangible This fact shows that it should be a primitive, ie the B O R d e R class should be a subclass of G L Y P h. In addition, there is a mandatory reason why the customer should treat the chamber consistent, and should not be concerned about whether there is a border. When a customer draws a simple, boundless primitive, you don't have to fix it. If that element is included in a boundary object, the customer should draw this boundary object with the same method of drawing the previous simple primitive, and should not treat the boundary object. This suggests that the B O R D e R interface is matched to the G L y P h interface. We enable B O R d E R as a subclass of G L Y P h to ensure this relationship. We derive the concept of transparent enclosure based on these. It combines two concepts: 1) single child (single component) combination; 2) compatible interface. Customers typically do not distinguish between the fences of processing components or components (ie, the parent components of this component), especially when the fence is just all the operations of the agent component. However, the fence can also modify the behavior of the component by adding some own operations before or after proxy operation. The fence can also be effectively added to the component. 2.4.2 Monoglyph We can use the concept of a transparent fence for all modified other primitives. In order to make this concept, we define the subclass M o N o g l y P H of G L Y PH as a "Decorative action" abstract class (see Figure 2-7). M O N O G L Y P h saves a reference to a component and passes all requests to this component. Figure 2-7 MONOGLYPH relationship This makes M o N o g l y p H default to the customer completely transparent. For example, M Ono G LYPH implements D RAW operations as follows: M Ono G Lyph's subclasses re-implement one such delivery operation, for example, B Order:: D RAW first activates component-based parent class operation M ONO G LYPH::::::::::::::::::::::::::::::::: D RAW, let the components do some work - that is, the other things other than the border. B O R D E R:: D R A W draws the boundaries by calling the private operation D R A W B O R D E R. Details Don't deeney here: 3 0 Design mode: Declarable object-oriented software foundation Chapter 2 Example Research: Design a Document Editor 3 1

Note B O R D E R:: D r a w How to effectively expand the parent class to draw borders. This is quite different from the call to ignore M O N O G L Y P H:: D R a W, which is completely instead of parental operation. Another MO N O G L Y P H subclass appearing in Fig. 2-7 is S C R O L E R, which draws components in different positions depending on the position of the two scroll bars as a modified. When drawing its components, it tells the graphics system to cut the part other than the boundary, scrolling the part other than the view is not displayed on the screen. Now we already have all the preparations needed to increase the boundary and scroll interface to the LE X i text editing area. We can combine existing C O M P O S i T i O N instances in one S C R O LE R example to increase the scroll interface, and then combine it into the B O R D e R instance. The resulting object structure is shown in Figure 2-8. Figure 2-8 Embedded Object Structure Note that we can also exchange the combination order, put a combination of boundaries in the S C R O LE R example. Such boundaries can scroll together with text, but we generally do not require this. The key is that the transparent fence makes the test different choices make it easy, so that the customer and modification code are not related. Also note how B O R D E R combines one rather than two or more G L Y. This is different from the combination we have so far, where combined mid -fects are allowed to have multiple uncertain sub-objects. Here, something is added to the border, hintting "something" is a single. We can define the behavior of multiple objects, but we have to mix multiple combinations and modifications concepts to form so-called row, columns, etc. Because we have many classes that can be used to do these combinations, this behavior does not help us. We'd better use existing classes to make a combination of combination, and increase the results of new class to modify the combination. Make the modification independently of other combinations, which can simplify the number of modifications and reduce their number, but also guarantee that we don't repeat existing combination functions. 2.4.3 Decorator Mode D E C O R A T O R (4. 4) Mode describes the relationship between the transparent fence to support the modified class and object. In fact, the meaning of "modification" is more extensive than we discussed here. In D e c O R A T O R mode, modifications refer to things that are subject to increasing their responsibilities. We can think of a semantic action to modify an abstract syntax tree, and use new conversion modifications with poor state automators or in the property tag to modify a lasting object network. D E C O R A T O R Generally, the method we used in L e x i, which makes it a broader practicability. 2.5 Supporting a variety of vision criteria to obtain portability of the hardware and software platform is one of the main issues of system design. Relocating L e x i in a new platform should not require significant modifications to LE X i, otherwise, if you lose the value of repositioning L e x i. We should make transplant as easy as possible. A major obstacle to transplantation is the difference between different visual standards. The visual standard is used to enhance the consistency of the user interface between the applications on a window platform. These standards define how the application should display and reflect the user request. Although the existing standards are different from each other, users can also clearly share them - an application will not be exactly the same as other platforms on the M O T I F platform, and vice versa. A application running in multiple platforms must meet the user interface styles of each platform. Our design goal is to make LE x i meet multiple existing visual criteria, and it is easy to increase support for new standards when new standards appear. We also hope that our design can support maximum flexibility: the runtime can change the appearance and feel of L E X i. 2.5.1 Abstract Object Create We see and operate in the LE X i user interface is a primitive, which is combined to be in an invisible element such as rows and columns. These invisible elements combine buttons, characters, etc. can be seen, and they can display them correctly. Interface style has many visual rules on so-called "window components" (Wi D g e t s). Window components are another term for views such as buttons such as control elements as control elements.

Window components can represent data using simple primitives such as characters, circles, rectangles, and polygons. We assume that the two window component sets are used to implement multiple visual criteria: 1) The first collection is composed of abstract g Lyph subclasses, and there is an abstract g Lyph subclass for each window component. . For example, abstract subclass S C R O L1 is amplified for the basic G L Y interface to increase the general rolling operation; B U t t o n is an abstract class for increasing the button's related operations; and so on. 2) Another collection is a collection of specific subclasses that achieve different visual criteria corresponding to abstract subclasses. For example, S C R O L1 R may have M O T I f S C R O L1 R and P M S C R O L B A R Two subclasses to achieve a rolling bar of the corresponding M O T I f and P M (P R E S E N T A Ti O NM A N A g E R). L e x i must distinguish between differences between window components of different visual styles. For example, when L e x i needs to put a button on the interface, it must instantiate a G L Y p H subclass with the correct button style (M O T T t o n or M a C B U t t o n, etc.). Obviously, the implementation of LE x i cannot be done directly by calling a C constructor, then hard to be hard to be hard to be 32 design mode: can be based on object-oriented software

A special style that cannot be selected at runtime. When LE X i is ported to other platforms, we have to make code search to change all of these constructors. And buttons are just one of the many window components on the LE X i user interface. The constructor call for a particular visual sensor will make the code confusing, generate maintenance difficulties - as long as you miss it, you might use M O T I F menu in the M A C application. L e x i requires a way to determine the visual criteria required to create the appropriate window components. We must not only avoid explicit constructor calls, but must also be able to easily replace the entire window component collection. The above two requirements can be created by an abstract object, we will use an example to explain. 2.5.2 Factory class and product class usually we may use the following C code to create a M otif scroll bar entrepore: scrollbar * sb = new motifscrollbar; but if you want to make L EXI's sensation dependence This code should be avoided as much as possible. If we initialize S B: Scolbar * SB = Guifactory-> CreateScrollbar (); here G u i f A c t o R Y is an example of the M O T I f F a C T O R Y class. C R E A T E S C R O L B A R Returns a new example of a suitable S C R O L B A R subclass, such as M O T I f S C R O L,,, such as M O T I f S C R O L B. Once is connected to the customer, it is equivalent to calling a constructor of M O T I f S C R O L B A R. But there is an essential difference: it is not like the name of MOTIF in program code as using the direct constructor. G u I f A c t o R Y object Abstract the creation process of the scroll bar under the visual standard, not just M O T I f scroll bar. And G U I f A c t o R Y is not limited to creating a scroll bar, which is widely used in the window component of the scroll bar, button, input domain, menu, and the like. The above approach is feasible because M otif f actory is a subclass of Guif Actory, and Guif Actory is an abstraction class that defines the public interface of the creation window component element, which contains elements of different window components. Action like C Reate S Croll B Ar and C Reate B Utton. The subclass of G u I f A C T O R Y implements these operations, and returns a primitive element such as MO T I f S C R O L1 B A R and P M B U T T O N. Figure 2 - 9 shows the result class hierarchy of G u I f A c t o R y object. Figure 2-9 Research on the 2nd Chapter 2 of Guifactory Class: Design a document editor 3 3

We say that the factory (F A C T O R Y creates the product (P R O D U C T) object. Further, the products produced by the factory are related to each other; in this case, the product is all window components of the same vision. Figure 2 - 1 0 shows such a product class, which uses them when the factory generates a window assembly. Figure 2-10 Abstract product classes and specific subclasses We have to answer the last question is: G u i f A c t o r y, where is it? Where is the answer that is convenient? Variable G u I f A c t o R Y can be a global variable, a static member of a well-known class, or if the entire user interface is created in a class or a function, it can even be a local variable. There is even a design mode S i n g l e t o n (3. 5) dedicated to managing such a well-known, only one object can be created. However, it is important to initialize G U I f A C T O R Y in a suitable place in the program, which is clear before it is used to create a window component, and after the desired visual criteria clearly. If the sensation is known at the compilation time, the GUI F Actory can initialize the value of a new plant instance at the beginning of the program: guifactory * guifactory = new motifFactory; if the user can specify when the user can start the string at startup Sight, then the code for creating a factory may be: There is also a more advanced way to select a factory at runtime. For example, you can maintain a registration form to map the string to the factory object. This allows you to register new factory sub-class instances without changing the existing code, and the front method requires you to change the code. And so you don't have to connect all platforms to your application. This is important because it is unlikely to connect to a MO T i f F a c t O R y on a platform that does not support M O T I F. 3 4 Design Mode: Credible Object-Oriented Software Basis Chapter 2 Example Research: Designing a Document Editor 3 5

But the key is that once we give the application of the correct factory object, its souping is set up from then. And if we change your idea, we can reinitialize G U I f A C T O R Y and re-construct the interface with a different vision plant. We know, no matter what, when will I initialize G u i f a c t o r y, once this is done, the application can create a suitable appearance without modifying the code. 2.5.3 Abstract Factory Mode Factory (F A C T O R Y) and Products (P R O D U C T) are the main participants of the Abstract Factory (3.1) mode. This mode describes how to create a series of related product objects without direct instantiation classes. It is most suitable for the number and species of the product object, and there are different situations between the specific product lines. We choose a product line by instantifying a specific factory object, and the plant has been used in the future. We can also replace the original factory objects by using a different specific factory example to change the entire product line. Abstract Factory Mode emphasizes the product line to distinguish it from other creative modes related to one product object. 2.6 Supporting a variety of window system is only one of the many transplant problems. Another transplantability is the window environment running by L e x i. A platform exhibits multiple overlapping windows on a dot matrix display. It manages the screen space and keyboard, the mouse to the input channel of the window. There are currently important window systems that are incompatible with each other (such as M A C I N T O S H, Presentation Manager, Wi N D O W S, X, etc.). We hope that LE X i can run on the window system as possible, which is the same as the same simplication. 2.6.1 Whether we can use the Abstract Factory mode, this seems to be a situation using the Abstract Factory mode. However, the restriction conditions of the window system transplantation are greatly different from the independence conditions of the visual. When using the Abstract Factory mode, we assume that we can define specific window component classes for each visual criteria. This means we can derive each particular product from an abstract product class (such as S C R O L1 R), for a particular standard (such as M O T I f S C R O L1 R, M A C R O L1 R, etc.). Now suppose we have some different manufacturers of class hierarchies, each class level corresponds to an aspect standard. Of course, these types of layers are unlikely to have too many compatibility. Therefore, we cannot create a public abstract product class for each window component (scroll bar, buttons, menu, etc.). And without these ABSTRACT FACTORY patterns cannot work. So we have to adjust different window component class hierarchies based on the common collection of abstract product interfaces. Only in this way can we define suitable C R E A T E.................................................................................................................................... For window components, we solve this problem by developing our own abstraction and specific product classes. Now when we try to make L e x i work in a system with a window, we face a similar problem. That is, different window systems are incompatible with program interface. But this time is bigger, because we can't achieve our own non-standard window system. But things still have room for recovery. Like a visual criteria, the interface of the window system is not very different. Because all window systems are always doing the same thing. We can make a unified abstraction on different window systems, making some adjustments to each window system, making it in line with public interfaces. 2.6.2 Packaging Relationship between 2.6.2 In 2. 2, we introduce Wi N D O W class to display a primitive or element structure. We don't specify the window system for this object, because it does not come from which particular window system. Wi N D O W Class Package Window To do something to do: • They provide operations for drawing basic geometry. • They can turn icons or restore to windows.

• They can change their size. • They can draw (or sang out) windows content as needed. For example, when they are restored by the icon to a window, or they overlap them on the screen space, and the outbound part is heavy, it is necessary to redraw, as shown in Table 2 - 3. Table 2-3 Wi ndows class interface liability management operation window virtual void Redraw () virtual void Raise () virtual void Lower () virtual void Iconify () virtual void Deiconify (). Graphical virtual void DrawLine (...) virtual void DrawRect (...) Virtual Void DrawPolygon (...) Virtual Void Drawte XT (.) The window function of the .wi NDOW class must span different window systems. Let us consider two extreme views: 1) The functional intersection of the Wi N D O W class only provides all window systems. The problem is that the Wi N D O W interface is only similar to a smallest feature on the capabilities, which is also unavailable for some of the advanced features supported by most window systems. 2) Function and set the interface that merges all the functionality of the system. But such an interface is bound to be large, and there is inconsistent place. In addition, when a vendor modifies its window system, we have to modify this interface and L e x i because l e x i relies on it. The above two methods are not practicable, so our design will take a discount. Wi N D O W class will provide a convenient interface that supports most window systems. Because the L e x i processes the Wi N D O W class directly, it must also support the elements of L E X i. This means that the Wi N D O W interface must include allowing primitives that can be drawn from their own basic graphics operations in the window. Table 2 - 3 shows some of the operations of the Wi N D O W class. Wi N D O W is an abstract class. Its specific subclass supports different types of windows used by users. For example, application windows, icons, and warning dialogs, etc. are all windows, but they are slightly different in behavior. So we can define subclasses such as A P P L I C A T I O NWI N D O W, I C O N Wi N D O W to describe these differences. The class hierarchy gave a unified window abstraction like L e x i, which does not depend on any particular vendor's window system, as shown in the following page. Now we have defined the window interface of the work for L e x i, then what is the truly window related window? Since we can't realize your window system, this window abstraction must be implemented with the target window system platform. How to achieve? One way is to implement multiple versions of the Wi N D O W class and its subclasses, each version corresponding to a window platform. When we 3 6 design mode: can be used for object-oriented software

When establishing L e x i on a given platform, we choose a corresponding version. But imagine that the maintenance problem is really a headache, we have saved multiple names all the classes of "Wi N D O W", and each class is implemented in a different window system. Another method is to create a specific implementation subclass for each window hierarchy, but this will generate the problem of the same subclass of subclasses we encountered at an attempt to increase the modification. These two methods have another disadvantage: we have not changed the flexibility of the window system used after compiling. So we have to keep a number of different executables. Since these two methods are not attractive, what can we do? That is what we have done when formatting and modifying: the concept of changing is packaged. Now there is a change in the window system. If we can encapsulate the function of the window system in an object, we can implement Wi N D O W class and its subclasses according to the object interface. Furthermore, if that interface provides services we are interested in all window systems, we need to change the Wi N D O W class or its subclasses, and support different window systems. We can set the window object to the window system we want by simply delivering the right window system package object. We can even set the window at runtime. 2.6.3 Window and Windowme We will define a separate Wi N D O W I M P Class Hierarchy to hide the implementation of different window systems. Wi N D O W i MP is an abstract class that encapsulates an object of the window system related code. In order to make L e x i run in a specific window system, we set the Wi N D O W object with a Wi N D O W I m P subclass example of the system. The following figure shows the relationship between Wi N D O W and Wi N D O W I M P hierarchy. Chapter 2 Example Research: Designing a Document Editor 3 7 Hidden in the Wi N D O W I M P class, we avoid direct dependence on the window system, which allows the Wi N D O W level to maintain relatively small and more stable. At the same time, we can easily extend the hierarchy to support new window systems. 1. Subclasses of Wi N D O W I M P Sub-class Wi N D O W I M P convert the user request to the operation of a particular window system. Consider the examples used in 2. 2, we define R Ectangel:: D RAW: D RAW R ECT default implementation of the default implementation of Wi NDOW I MP definition using the default implementation of Wi NDOW I MP definition Abstract operation: Here _ IMP is a member variable of Wi NDOW, which saves the Wi NDOW I MP setting Wi NDOW. The implementation of the window is defined by the example of the Wi N D O W I m P subclass referred to in _ I m P. For an X Wi NDOW I MP (the Wi NDOW I MP subclass of the X window system), the implementation of the D Evice R ECT may be as follows: D Evice R ECT is because XDR AW R Ectangle (painted in the X system The interface) is based on the left vertex, width, and height definition rectangle of the rectangle, and D Evice R ECT must calculate these values ​​according to the parameter value. First it must determine the left vertices (because (x 0, y 0) may be any of the four vertices of the rectangle) and then calculate the width and height. P M Wi N D O W I m P (Wi N D O W I M p Subcada) defines D e V i c E R E C T different: 3 8 Design mode: can be used for object-oriented software

Why is this and the X version so big? Because Pm does not explicitly draw a rectangle as X, it has a more general interface to specify a vertex of multiple segments (or paths), drawing these line segments and populates them. The P m implementation of D e ​​v i c e r e c t is obviously different from the implementation of X, but the problem is not large. Wi N D O W i m P hides the difference in each window system interface with a huge but stable interface. This allows the implementation of the Wi N D O W subclass to place more energy on the window of the window, not the details of the window system. It also supports us to add new window systems without messing with Wi N D O W class. 2. Use Wi N D O W i m P to configure Wi N D O W S We have not discussed a key issue: How to configure a window with a suitable Wi N D O W i m p subclass? That is, when is it initialized? I M P, who knows what window system is being used (which is the Wi N D O W I m p subclass)? The window needs some Wi N D O W i M p before you can do what it is interested in. There are many possibilities for the answers to these issues, but we only focus on using the Abstract Factory (3.1) mode. We can define an abstract factory class Wi NDOW S Ystem F Actory, which provides an interface for creating a realized object related to the window system: Now we can define a specific factory for each window system: Wi NDOW basic class construct Enter the Wi NDOW S Ystem F Actory interface and the appropriate window system Wi NDOW I MP to initialize the member variable _ IMP: Chapter 2 Instance Research: Design a Document Editor 3 94 0 Design mode: can be used object-oriented software Foundation

W i N D O W S Y S TET is an example of Wi N D O W S Y S E M f A C T O R Y, which is a common visible, as G u i f a c t O R Y is a variable of common visual definition. The W i N D O W S Y S TET E M F A C T O R Y variable can be initialized by the same method. 2.6.4 Bridge mode Wi N D O W I m P defines an interface of a public window system facility, but its design is driven by a restriction condition that is different from the Wi N D O W interface. The application is not directly processed by the interface of Wi N D O W i m P; they only processes Wi N D O W objects. So the interface of Wi N D O W i M p does not have to match the objective world view of the application, just like we only care about Wi N D O W Levels and Interfaces. The interface of Wi N D O W i M p is more likely to reflect what window system is available. It can be biased toward the intersection of the functional method, or the integration of the functional method, as long as it is best for each target window system. It is to be noted that the Wi N D O W class interface is for the application, and the Wi N D O W i m p interface is for the window system. Separate window functions to Wi N D O W and Wi N D O W I m P Class Hierarchy, so we can independently implement these interfaces. These types of hierarchical object works to implement LE X i does not need to modify the goal of multi-window system. The relationship between Wi N D O W and Wi N D O W i M p is an example of the B R I D g E (4. 2) mode. The purpose of B R i D g E mode is to allow the separated class level to work together, even if they are independent. Our design guidelines make we created two separate classes, a logical concept of a support window, and another describing the different implementations of the window. B R i D g E Mode allows us to maintain and strengthen our logic abstraction to the window without tanning the code related to the window system. Easy the same. 2.7 Some functions of user operation L e x i can be expressed by the W Y S i w y g of the document. You can knock and delete text, move the insert point, select the text area by direct point, hit and typing on the document. Other functions are indirect through the drop-down menu of L e x i, buttons, and keyboard acceleration keys. These features include: • Create a new document. • Open, save, and print an existing document. • Cut the text from the document and paste it back to the document. • Change the fonts and styles of the selected text. • Change the format of the text, such as alignment, and adjustment formats. • Exit application. and many more. L e x i provides different interfaces for these user operations. But we don't want a particular user operation to contact a specific user interface. Because we may want multiple user interfaces to be actions (for example, you can use a page button, you can also use a menu item to represent a page page). You may change the interface later. Besides, these operations are implemented in different classes. We want to access these features, but do not want to establish too many dependencies between user interface classes and its implementation. Otherwise, in the end we get the immediate coupling implementation, it is difficult to understand, expand and maintain. More complicated is that we hope that LE X i can support most features (U N D O) and redo (R E D O) operations. In particular, we want to revoke users who do not pay such a non-payment that will destroy the operation of the data. But we should not try to have a regular basis for the operation of the operation. Research: Design a document editor 4 1

Undo operation like saving a picture and exiting the application. These operations should not be affected by the revocation operation. We also do not want any restrictions on the revocation and heavy. It is clear that the support of the user's operation has penetrated into the application. The challenge we face is to propose a simple, expandable mechanism to meet all of these requirements. 2.7.1 Package a request from our designer's perspective, a drop-down menu is only another element of other primitives. Differential drop-down menus and other children's chambers in that the elements in most menus will respond to the mouse click to do some operations. Let us assume that the chambers of these things are an example of a G L y P H subclavab that is called M e n u t e m, and they do something to respond to a request. Performing a request may involve a plurality of operations of an object or multiple objects of an object, or other situations between the two. We can define a subclass of M e N u I t e m for each user operation and then encode each subclass. But this is not the right way, we don't need to define a M e N u i t e m class for each request. As we don't need to define a subclass for each drop-down menu. Besides, this method will request the request with a particular user interface, which is difficult to meet the same request from different user interfaces. Suppose you can pass the last page of the document through the page icon at the bottom of the L E X i interface through the menu item of the drop-down menu (the short document may be more convenient). If we connect the user request and menu item with inheritance, we must also treat the page icon or other similar window components that send the user request. The number of classes such that the class generated is the product of the number of window components and the number of requests. It is now lacking a mechanism that allows us to parameterize the menu item with the request of the menu item. This approach can avoid the population of subclasses and get greater flexibility in operating time. We can call a function to parameterize a M e n u i t e m, but because of the following three reasons, this is not a very complete solution: 1) It has not stressed the revocation / redo operation. 2) It is difficult to link status and functions. For example, a function of changing fonts requires which font is known. 3) The function is difficult to expand, and it is difficult to partially use them. These are indicated that we should use objects to parameterize M e n u I t e m instead of a function. We can implement the expansion and multiplex requests. We can also save status and implement revocation / redo functions. Here is an example of another package change concept, that is, package request. We will package each request in the C O m M A n D object. 2.7.2 COMMAND class and its subclasses First we define a C O m M A N D abstraction class to provide an interface to send a request. This basic interface consists of an abstract operation "E x E C U t e". The subclasses of C O m M a N D implement E x E C U TE operation in different ways to meet different requests. Some subclasses can delegate some or all of the work to other objects. Other subclasses may be completely met themselves (see Figure 2 - 11). However, for the requester, a C O m M a N D object is a C O m M M a N D object, which are all consistent. Now, M e n u I t e m can save a C O m M a N D object (as shown in Figure 2 - 1 2). We give each menu item a C O m M a N D subclass example suitable for this menu item, just as we specify a text string for each menu item. When the user selects a specific menu item, the menu item just calls its E x E C U t e operation of the C O m M A N D object to perform the request. Note Button and other window components can handle requests in the same way. In terms of concept, customers are LE x i users, but in fact, customers are another object that manages users (such as event send objects) 4 2 Design mode: can be used for object-oriented software

Figure 2-11 Partial Command class level Figure 2-12 MenuItem-Command relationship 2.7.3 Undo and redo the ability to revoke and re-do (U N D O / R E D O) capabilities in interactive applications. In order to revoke and redo a command, we increase U N e x e c u t e operation in the C O m M A N D interface. U N e x e c u TE O operation is the inverse operation of e x e c u t e, which uses the cancellation information saved by the previous E x E C u TE E operation to eliminate the effects of the E x E C U TE. For example, in an example of F O N T C O m M A N D, the E X E C U TE operation stores the text area of ​​the font and the previous font. The u n e x e c u t e operation of F O N T C O m M a N D will reply the text of the area to the previous font. Sometimes you need to decide and re-do at runtime. If the font of the text is a font to modify, then this request is meaningless, it does not have any impact. Have some text, then send a meaningless font to change request. So what is the result of the next revoked request? Is it a meaningless font change operation that causes unable to do anything when the request is canceled? It should not be the case. If the user repeats the meaningless font changing operation, he should not have to perform the same number of undo operations to return to the previous meaningful operation. If you do not have any impact if an command is executed, then the corresponding revocation operation is required. Therefore, in order to determine if a command can be revoked, we add an abstract R E V E R S I B L E operation to the C O m M A N D interface, which returns the B O O L E a n value. Subclasses can redefine this operation to return T R u e or F A L s E based on the operation. 2.7.4 Command History Supports the last step of the revocation and redance commands for any level is to define a command history (C O m M A N D Save Command H i S T O R Y), or the command list (or some commands that have been revoked). From a conceptually understood, the history of the command looks like a shape, as shown below. Each circle represents a C O m M m M M M a N D object. In this example, the user has issued four commands. The leftmost command is the first one, according to the next, the rightmost command is recently. A line tracking of "P R E S E N t" indicates a command that is recently executed (and canceled). To revoke the recent command, we call the U N e x E C U TE operation of the rightmost C O m M A N D object, as shown in the following figure. After the recent command calls UN e x e c u t e, we left the "P R E S E N T" to move the distance of the C O m M M A N D object. If the user selects the revocation operation again, the next recently sent command is revoked in the same way, we can see the following state, as shown below. You can see that we can perform multi-level revocations by repeating this process. The number of layers is only limited by the length of the command history. To redo a command just revoked, we only need to do the above inverse process. Commands in the right of the P R E S E N t should be re-made later. When the command that is just revoked, we call the E x E C U t E of the C O m M a N D object on the right of the P R e s e n t, as shown below. Then we move the P R e s e n tline to facilitate the next redo to call the next command object, as shown below. Of course, if the next operation is not a heavy but revoke, then the command on the left of the P R E S e n t wire will be revoked. This will effectively cancel the command in time when it is necessary to recover from an error. Chapter 2 Example Research: Designing a Document Editor 4 3

Previously command current unExecute () Current future current current current Execute () previous 2.7.5 Command mode l e x i command is the application of C O m m a n d (5. 2) mode. This mode describes how to encapsulate requests, which also describe interfaces that consistently send requests, allowing you to configure clients to handle different requests. This interface protects the implementation of the customer request. A command can delegate all or part of the request to other objects or not. This is quite perfect for applications that must provide centralized access to the dispersion function such as L E X i. This mode also discusses the revocation and redo mechanism based on basic C O m M A N D interface. 2.8 Spelling Check and Broken Letter Processing The last design problem involves text analysis, specifically referring to the inspection of the wrong check and a good format. The restriction conditions here are similar to the limitations of the design problem. There are also a variety of methods similar to the computing of the wrap strategy, spelling and linker points. Therefore, we can support multiple algorithms at the same time. A set of different algorithms can provide time / space / quality choice, we also hope that new algorithms should be easily added. We must try to avoid closely coupled to document structures, and this goal is even more important than formatting. Because spelling checks and hyphens are just two of the potential text analysis we hope to support by L e X i.不避 免,, we may extend multiple analysis capabilities of LE X i multiple times. We may add a lookup, word count statistics, compute the facilities of the total value of the table. But we don't want to change the G L y p H and its subclasses at each new function. In fact, this problem can be divided into two parts: 1) Access the information that needs to be analyzed, and they are dispersed in the primitive of the document structure; 2) Analyze this information. We treat these two parts separately. 2.8.1 Access Dispersion Information Many analytics require typography, and the text we need to analyze is in the hierarchical structure of dispersion in the primary object. In order to check the text in this structure, we need a way of knowing the access mechanisms included in the data structure. Some primitives may save their subunit in the connection table, and others may be saved with array, and some may use more complex data structures. Our access mechanism should process all these possibilities. In addition, more complicated situations are that different analysis algorithms will access information in different ways. Most analytical algorithms always traverse text from head to tail, but there are some examples of justice - for example, the access order of the reverse search is neutralized, not from going back. The evaluation of the arithmetic expression may require a sequential traversal process. So our access mechanism must accommodate different data structures, and we must also support different traversal methods, such as pre-sequence, sequence, and order. 4 4 Design mode: can be used for object-oriented software

The future current 2.8.2 package access and traversal, if our interface interface uses a integer digital index, let the customer reference subunit. Although this is reasonable for the chart of the set of groups of groups, it is inefficient to use the chamfer class using the connection table. An important role of the abstraction of the primitive is to hide the data structure that stores its subunit, we can change the data structure of the chamfer class without affecting other classes. Thus, only the primitives you know the data structure it uses. There can be such a inference: The primitive interface should not be presented to a certain data structure. Should not be like the above, ie the array is better than using the connection table. We may solve this problem and support multiple traversal methods. We can put multiple access and traversal functions into the primitive class, and provide an option, which may be used as parameters by adding an enumeration. The class passes the parameter during the traversal to ensure that the same traverses used are used, which must pass any of the information accumulated during the traversal process. We can add abstract operations as follows from the interface of G L Y P H to support this method: f i r s t, n e x t, and I S D O N e operation control traversal. F IRST initialization traversal process, it determines what traverses of execution based on the parameter value of the enumeration type Tr Aversal, which can be Children (direct sub-primary primary in the element), preorder (in an orderly manner traversing the entire structure), Pos to Rder and Inorder. N e x T advances to the next element over time. I S D O N e reports whether the traversal is completed. G e t C U R E N T replaces the C H i L D operation, it accesses the current element of traversal. I N s e R T operation replaces the previous operation, which is inserted into a given element in the current location. One analysis can use the following C code to implement the primary sequence structure of G as root nodes: Note We have abandoned the digital index of the element interface. This will not be biased in a certain data structure. We also make customers do not have to achieve universal traversal methods. But this method still has some problems. For example, it does not support new traversal methods without extending the enumeration value or increasing new operations. For example, we want to modify the order traversal, so that it can automatically skip non-text chambers. We have to change the enumerated type TR A V E R S A L so that it contains values ​​such as TEEx T u a L_ P R e O R D E R. We'd better avoid changing the existing instructions. Put the traversal mechanism to the G L Y h-H level, will lead to some classes when modified and expanded. It also makes it difficult to multi-use traversal mechanisms to traverse other object structures, and multiple traverses can be made at the same time in one structure. Once again, a good solution is to encapsulate the concept of changes, in this case we refer to access and traversal mechanism. We introduce an object called I T E R A T O R S, and its purpose is to define different collections of these mechanisms. We can unify different data structures and support new traversal methods through inheritance. basis

The elevation implemented in chaos. 2.8.3 Iterator classes and their subclasses We use abstract class I t e R A T O R to define a universal interface for access and traversal. For specific subclasses, such as A RRAY -I TERATOR and L IST I TERATOR, responsible for implementing this interface to provide access to array and list; and PRERDER I TERATOR and P OSTORDER I TERATOR and similar classes are different on specified structures Traversing method. Each I t e R A T O R subclass has a reference to the structure it traversed. When you create a subclass instance, you need to initialize this reference. Figure 2 - 1 3 shows the relationship between I T E R A T O R and its several subclasses. Note that we add a C R E A TET E I T E R A T O R Abstract Operation in the G L Y-H-class interface to support I T E R A T O R. Figure 2-13 Iterator class and its sub-class I t e R A T O R interface provide F i R S T, N e x T, and I S D O N e operation to control traversal. L IST I TERATOR class implemented F IRST operation points to the first element of the list; n extway advances to the next element of the list; I S D One returns the list pointer to point to the list range; C URrent I TEM Returns Iterator Primitive. A r r a y i t e R A T O R Class implementation is similar, but it is for a primitive array. Now we don't need to know the child that can also access a chamber structure: Returns a N u L i t eR a t O r in the default C R e a t e I t eR A T O r returns an example. N u L i t E R A T O R is a degraded I T E R A T O R, which is suitable for the leaf chamber, ie the primary element of the sub-element. The I S D O N e operation of N u L i t e R A T O R always returns T R u e. A child's primitive element G L Y P H class returns an example of different I t E R A T O R subclasses, depending on the structure used by the child's children. If the roller classes of G Lyph are saved in a _ children list, then its C REATE I TERATOR operation is implemented as follows: I Teerator used to traverse the order and the order is implemented with each primitive Iterator implementation. of. These traversed I T E R A T O R also saves references to the root element of the structure they traversed. They call the C R E A TET ER E R A T O R in the structure in the structure, and save the returned I t e R A T O R in the stack. For example, class P REORDER I TERATOR gets I Teerator from the root chart, initializes it to point to the first element, then press it into the stack: C URrent I TEM just call the I Teerator of the stack of I TeeRRrent I TEM operation : N EXT Operation Gets the I Teerator on the top of the stack and lets it create an I Teerator, which is traversed to the farthest structure (because this is a preface traversal). N e x t Sets the new I T E R A T O R to the first element in the traversal, and then press it.

Then n e x t tests the nearest I T E R A T O R, if its I S D O N e operation returns T R u E, then we complete the traversal of the current subtree (or leaves). In this example, the N e x T pops up the I t eR a t O r of the top of the stack and repeat the above process until the next one has not completed traversal; otherwise, we have completed traversal of the entire structure. Note how I TERATOR class hierarchy allows us to increase the meta-class class, such as PREORDER I TERATOR, we only need to create I teraroor subclasses, and add a new traversal algorithm to can. G L Y P H Chapter 2 Example Research: Designing a Document Editor 4 7

Subclasses provide customers with the same interface to access their children, do not reveal the underlying data structure. Since I T E R A T O R is stored in its own traversal state, we can perform multiple traversal at the same time, and even traversal of the same structure. Although we are in this example, we have no reason to make the class parameters such as P R E O R D e R I T E R A T O R to traverse other types of object structures. We can use C template technology to do this, so that we can multiplex the mechanism of P R e O R d E R I t eR a t O R when it is traversing other structures. 2.8.4 Iterator Mode I TE E R A T O R (5. 4) Mode describes technologies that support access and traverse the object structure, which can not only be used in combination structures or collection. This mode abstracts the traversal algorithm that hides the internal structure of the object it traverses it. I T E R A T O R Mode once again illustrates how to encapsulate changes, help us get flexibility and reuse. Despite this, the complexity of I T E R A T I O N is still surprising, I T E R A T O R mode contains more subtle differences and trade-off than we think about here. 2.8.5 Action in the traversal and traversal now We have a method of traverse the chamber structure, which can check spelling and supporting hyphens. Both analytics involve the accumulation of information during the traversal process. First we have to decide what to put the responsibility of the analysis. We can be analyzed in the I T E R A T O R class, which will analyze and traverse organic combine. However, if we can distinguish between traversal and traversal, you can get more flexibility and potential reuse, because different analysis usually requires the same traversal method. Thus, for different analyzes, we can reuse the same I T E R A T O R Collection. For example, preface traversal for many analyzes, including spelling, even characters, forward search, and word count statistics, etc. are common. Therefore, we should separate the analysis and traversal, so where is the analysis responsibility? We know that there are many analytics to do, each analysis will do different things in different traversal points. According to the type of analysis, some G L Y P h is more important than other primitives. If the spell check and hyphen analysis, we should consider the characteristic element, not like a line and bitmap graphics. If we make color segmentation, we have to consider visible elements, rather than not visible. Therefore, different analysis processes must be analyzed different primitives. Thus a given analysis must distinguish between different types of elements. An obvious way is to put the analytical capabilities in the primitive. For each analysis, we add one or more abstraction operations for G L Y, Class, and acts in the G L y p h subcategory according to their analysis. But trouble is that every new analysis must be changed, each of which must change each chamber class. In some cases, this problem can simplify: Sometimes only partial class participation is analyzed. In addition, most classes are analyzed in the same way, then we can supplement a default implementation for abstract operations in the G L Y class. This default will contain many common conditions. This allows us to convert the modification is limited to G L y p h and those non-standard subclasses. However, even if the default implementation can reduce the number of classes that need to be modified, an implicit problem still exists: As new analytical functions increase, G L Y PH's interface is getting bigger and bigger. Many analytical operations will gradually blur the basic G L Y interface, which is difficult to see that the main purpose of the primitive is to define and structured those with appearance and shape - these interfaces are completely overwhelmed. 2.8.6 Packaging analysis All signs show that we need to encapsulate in an independent object, just like we have done many times before: the foundation for object-oriented software

kind. We can package a given analysis in a class and combine the instances of the class and the appropriate I T E R A T O R. This I T E R A T O R is responsible for carrying this example into each of the translucent structures. This analysis of the object can do some analysis work at each traversal point. In the traversal process, the analyst accumulates the information it is interested (in this example, "as shown below. The basic problem of this method is that the analysis of how many different primitives can be treated without using type testing or mandatory type conversion. We don't want S P E L i n g C H E C K E R containing the following (pseudo) code: this code is quite bad. It relies on a relatively high-profile type of security to convert such capabilities and is difficult to expand. Whenever we change to the G L y p-class level, remember to modify this function. In fact, this is also the kind of code for object-oriented language to eliminate. How do we avoid this immature way? What happens when we add the following code in the G Lyph class: Void Checkme (SpellingChecker &) We define C Heck M e in each G Lyph subclass: Chapter 2 Example Research: Design a Document Editor 4 95 0 Design Mode: can be used for object-oriented software

The g l y p h S UB C L A S S here will be replaced by the name of the icon class. Note When CH E C K M E is called, which particular G L y p h is known - after all, we are using its operation. Compared, the interface of the S Pelling C Hecker class contains an operation of each G Lyph subclass similar to C Heck G Lyph S Ubclass: S Pelling C Hecker's check character specifier is as follows: note that we have A special G et C Har C ODE is defined in the C HaaCter class. Spelling examrs can handle the operation of specific subclasses without type checking or conversion - this allows us to treat each object separately. C h e c k C H A R A C T E R accumulates alphanumeric characters in the _ C U R E N T WO R D array. When it comes to a non-alphabetic character like an underscore, it uses I S M i s S P E L E D operation to check _ C U r RE N T Wo R D spelling. If the word spelling errors, we can use the function overload to give each such member function with the same name, because their parameters have been distinguished. We give different names here to emphasize their differences, especially when calling them. I S M i s S P E L E D realizes the spelling algorithm because it is independent of the design of L e x i, so we will not say this here. We support different algorithms by subclass S P E L i N g C HE E C K E R; however, you can also use S TR a T E G Y mode to support different spell check algorithms (just like it is formatted in 2. 3). Chapter 2 Example Research: Designing a Document Editor 5 1

C HE E C K C H A R A C T E R adds it to the list of orthogonal words. Then you must clear the array _ C u R e n T Wo R d to check the next word. After the traversal is over, you can operate the list of words that spell erroneous words via g e t m i s s p e l. Now, we call the CHE C k M e operation of each element with a spellper inspector to implement the traversal of the chamber structure. This makes the spell checker S p e l i n g c h e c k E r can effectively distinguish each primitive and constantly push the inspector to check the following. The following interaction shows how C H A R A C T E R is synergistic to work together: this method is suitable for finding spelling errors, but how can we help us support multiple analysis? It seems a bit like a new analysis every additional analysis, it has to increase the operation of G L y p h and its subclasses similar to CH E C K M E (S P E L I N G C H E C K E R &). If we insist on each analysis corresponding to a separate class, the fact is true. But there is no reason to say that we cannot give all the analytical types. We should allow us to use various analyzes. That is, we should be able to replace the operation of a specific analysis such that the CHE C K E (S P E L I N G C HE E C K E R &) is used in an operation with a general parameter. 2.8.7 Visitor class and its subclasses We use the term visitor (V I S i T O R) to "access" traversed objects and do appropriate operations in the traversal process. In this example we use a VI S i T O R class to define an interface between the entry in the access structure. Check the full word "Access" is just a little more generally a little more than "analysis". It shows the terms we used in design patterns. 5 2 Design mode: can be used for object-oriented software

The specific subclasses of the visitor do different analyzes, for example, we can use a S P E L I n g C h E C K I N g Vi S i t O R subclass to check the spelling; use H y p H E N a T I O N Vi S I t O R Subcarpse Connection Character Analysis. S P E L 1 N G C HE E C K I N G VI S i T O R can be implemented as the SP E L 1 of the SP E L I N G C H E C K E R above, except that the operand is to reflect the generic accessor's class interface. For example, C H E C K C H A R A C T E R should be changed to VI S I T C H A R A C T E R. Since C H E C K M E is not suitable for visitors, the visitor does not check anything. Therefore, we want to use a more common name: A c c e p t, which should also be changed to VI S i T O R & to reflect what it can accept any visitors. Now defining a new analysis only needs to define a new VI S i T O R subclass - we don't need to touch any of the numeral class. We can support all future analysis methods by adding this in G L Y and its subclasses. We have seen how to do spelling. We can use a similar approach to the use of similar methods in H Y P H E N A T I O N VI S T I T O R, but once the VI S I t C H A R A C TET C H A R A C TER in H Y PH E N A T I O N Vi S I T O R is operated to handle the entire word, its work mode will be slightly different. It is not a spelling error that checks the word, but uses a linked character algorithm to determine the location of the word possible linker point (if any). Then insert a D i S C R e t i o n a r y. D i s c r e T I O N A R Y is an example of G L Y P H subclass D i S C R e T I O N A R Y. A D i S C R e t I o n a R Y is two possible appearance, which is determined whether it is the last character of a row. If it is the last character, then D i s c r e t i o n a r y looks like a hyphen; if not, then D i s c r e t i o n a r y does not display anything. D i s c r e t I o n a r y exams its parent object (a row object) to determine if it is the last child. D i s c r e t i o n a r y must be used as this check when you are activated you or calculate its boundaries. The formatting policy will see D i s c r e t I o n a R y as a space, which all as a flag ending. The figure below shows how an embedded D I S C R e T I O N A R Y is displayed. 2.8.8 Visitor mode We describe an application of the VI S i t o R mode. The front VI S i T O R class and its subclasses are the main participants in this mode. The VI S i T O R mode describes such a technique we have used earlier, which allows the number of analyzes to be analyzed without restrictions without having to change the map metadomism. Another advantage of the visitor class is that it is not limited to the composite of the image structure, and is also applicable to any other object structure. Includes collection, list, and even ring-free. Furthermore, there is no need to associate between the categories that the visitor can access by a public parent class. That is, the visitor can span the class hierarchy. You have to ask yourself an important issue before using the VI S i t o R mode: which type hierarchy is most powerful? This mode is most suitable for the case where you want to do many different things to a stable class structure. Add a new visitors without changing the class structure, which is especially important for a large class.

转载请注明原文地址:https://www.9cbs.com/read-3327.html

New Post(0)