SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы
НазадМетки: python
The information about Trash objects will be read from an outside file. The file has all of the necessary information about each piece of trash on a single line in the form Trash:weight, such as:
# PatternRefactoring/trash/Trash.dat patternRefactoring.trash.Glass:54 patternRefactoring.trash.Paper:22 patternRefactoring.trash.Paper:11 patternRefactoring.trash.Glass:17 patternRefactoring.trash.Aluminum:89 patternRefactoring.trash.Paper:88 patternRefactoring.trash.Aluminum:76 patternRefactoring.trash.Cardboard:96 patternRefactoring.trash.Aluminum:25 patternRefactoring.trash.Aluminum:34 patternRefactoring.trash.Glass:11 patternRefactoring.trash.Glass:68 patternRefactoring.trash.Glass:43 patternRefactoring.trash.Aluminum:27 patternRefactoring.trash.Cardboard:44 patternRefactoring.trash.Aluminum:18 patternRefactoring.trash.Paper:91 patternRefactoring.trash.Glass:63 patternRefactoring.trash.Glass:50 patternRefactoring.trash.Glass:80 patternRefactoring.trash.Aluminum:81 patternRefactoring.trash.Cardboard:12 patternRefactoring.trash.Glass:12 patternRefactoring.trash.Glass:54 patternRefactoring.trash.Aluminum:36 patternRefactoring.trash.Aluminum:93 patternRefactoring.trash.Glass:93 patternRefactoring.trash.Paper:80 patternRefactoring.trash.Glass:36 patternRefactoring.trash.Glass:12 patternRefactoring.trash.Glass:60 patternRefactoring.trash.Paper:66 patternRefactoring.trash.Aluminum:36 patternRefactoring.trash.Cardboard:22
Note that the class path must be included when giving the class names, otherwise the class will not be found.
This file is read using the previously-defined StringList tool, and each line is picked aparat using the String method indexOf( ) to produce the index of the ‘:‘. This is first used with the String method substring( ) to extract the name of the trash type, and next to get the weight that is turned into a double with the static Double.valueOf( ) method. The trim( ) method removes white space at both ends of a string.
The Trash parser is placed in a separate file since it will be reused throughout this chapter:
# PatternRefactoring/trash/ParseTrash.py # Parse file contents into Trash objects, # placing each into a Fillable holder. class ParseTrash: def fillBin(String filename, Fillable bin): for line in open(filename).readlines(): String type = line.substring(0, line.index(':')).strip() weight = Double.valueOf( line.substring(line.index(':') + 1) .strip()).doubleValue() bin.addTrash( Trash.factory( Trash.Messenger(type, weight))) # Special case to handle Collection: def fillBin(String filename, Bin): fillBin(filename, FillableCollection(bin))
In RecycleA.py, an ArrayList was used to hold the Trash objects. However, other types of containers can be used as well. To allow for this, the first version of fillBin( ) takes a reference to a Fillable, which is simply an interface that supports a method called addTrash( ):
# PatternRefactoring/trash/Fillable.py # Any object that can be filled with Trash. class Fillable: def addTrash(self, Trash t)
Anything that supports this interface can be used with fillBin. Of course, Collection doesn’t implement Fillable, so it won’t work. Since Collection is used in most of the examples, it makes sense to add a second overloaded fillBin( ) method that takes a Collection. Any Collection can then be used as a Fillable object using an adapter class:
# PatternRefactoring/trash/FillableCollection.py # Adapter that makes a Collection Fillable. class FillableCollection(Fillable): def __init__(self, cc): self.c = cc def addTrash(self, t): self.c.add(t)
You can see that the only job of this class is to connect Fillable‘s addTrash( ) method to Collection‘s add( ). With this class in hand, the overloaded fillBin( ) method can be used with a Collection in ParseTrash.py:
def fillBin(filename, bin): fillBin(filename, FillableCollection(bin))
This approach works for any container class that’s used frequently. Alternatively, the container class can provide its own adapter that implements Fillable. (You’ll see this later, in DynaTrash.py.)