[ Pobierz całość w formacie PDF ]
.Cooper 141Set Garden_getCenter = pltCenterEnd Function'-----Private Function Garden_getShade() As PlantSet Garden_getShade = pltShadeEnd FunctionIn a similar way, we can create Garden classes for PerennialGarden andAnnualGarden.Each of these concrete classes is known as a Concrete Factory,since it implements the methods outlined in the parent abstract class.Now wehave a series of Garden objects, each of which returns one of several Plantobjects.This is illustrated in the class diagram in Figure 5.1.Figure 5.1  The major objects in the Gardener program.We can easily construct our abstract factory driver program to return one of theseGarden objects based on the radio button that a user selects as shown in the userinterface in Figure 5.2.Copyright © 2001, by James W.Cooper 142Figure 5.2  The user interface of the Gardener program.How the User Interface WorksThis simple interface consists of two parts: the left side, that selects the gardentype and the right side, which selects the plant category.When you click on oneof the garden types, this causes the program to return a type of garden thatdepends on which button you select.At first, you might think that we would needto perform some sort of test to decide which button was selected and theninstantiate the right Concrete Factory class.However, a more elegant solution isto just listen for the radio button click and change the current garden.Then whena user clicks on one of the plant type buttons, the plant type is returned from thecurrent garden and the name of that plant displayed:Private Sub opAnnual_Click()Set gden = New AnnualGarden 'select Annual gardenEnd Sub'-----Private Sub opPeren_Click()Set gden = New PerenGarden 'select Perennial gardenEnd Sub'-----Copyright © 2001, by James W.Cooper 143Private Sub opVeggie_Click()Set gden = New VeggieGarden 'select vegetable gardenEnd SubThen, when we are called upon to draw a plant name on the garden display, weerase the old name by XORing it, and then draw a new one in its place, bygetting the correct Plant from the current Garden'-----Private Sub btCenter_Click()Set plt = gden.getCenter 'get the center plantdrawCenter plt.getName 'and draw it's nameEnd Sub'-----Private Sub drawCenter(st As String)pcGarden.PSet (1200, 1000)pcGarden.Print oldCenter 'XOR out old namepcGarden.PSet (1200, 1000)pcGarden.Print st 'draw in new nameoldCenter = st 'remember this name so we can eraseEnd SubCreating an Abstract Factory Using VB7The same GardenMaker program differs substantially in VB7.While it iscertainly possible to write Garden as an interface and have each of the derivedgardens implement that interface, it is easier to have some of the methods in thebase Garden class and the rest in the derived classes.The other major differences in VB7 have to do with the event system.In VB7,you do not draw on the screen directly from your code.Instead, the screen isupdated when the next OnPaint event occurs, and you must tell the paint routinewhat objects it can now paint.Since each garden should know how to draw itself, it should have a draw methodthat draw the appropriate plant names on the garden screen.And, since weprovided push buttons to draw each of the types of plants, we need to set aBoolean which indicates that you can now draw each of these plant types.We start with our simplified Plant class, where we pass the name of the plant inright in the constructor:Copyright © 2001, by James W.Cooper 144Public Class PlantPrivate plantName As String'-----Public Sub New(ByVal nm As String)MyBase.New()plantName = nm 'save the plant nameEnd Sub'-----Public Function getName() As StringgetName = plantName 'return the plant nameEnd FunctionEnd ClassThen we create the basic Garden class, which contained the getShade, getCenterand getBorder methods in the original implementation, no longer needs thesemethods in this implementation, because the Garden itself does the drawing.Public Class Garden'protected objects are accessed by derived classesProtected pltShade, pltBorder, pltCenter As PlantProtected center, shade, border As Boolean'These are created in the constructorPrivate gbrush As SolidBrushPrivate gdFont As Font'Constructor creates brush and font fro drawingPublic Sub New()MyBase.New()gBrush = New SolidBrush(Color.Black)gdFont = New Font("Arial", 10)End Sub'-----The drawing is done in a simple draw method, where we check as to whether weare supposed to draw each kind of plant name, and draw it if that Boolean is true.Public Sub draw(ByVal g As Graphics)If border Theng.DrawString(pltBorder.getName, gdFont, _Copyright © 2001, by James W.Cooper 145gbrush, 50, 150)End IfIf center Theng.DrawString(pltCenter.getName, gdFont, _gbrush, 100, 100)End IfIf shade Theng.DrawString(pltShade [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • hanula1950.keep.pl