Cos3711 Additional Notes
Cos3711 Additional Notes
Cos3711 Additional Notes
// comment out
// comment out
Page 2 of 16
Page 3 of 16
<root>
....
</root>
This root then has branches that are called children.
<root>
<child>
<sub_child>
...
</sub_child>
<sub_child>
...
</sub_child>
</child>
<child>
...
</child>
</root>
Section 15.2: SAX Parsing (sax1.pro)
There are several things you need to note about running this example (which should run
as given).
Add the line CONFIG += console to the sax1.pro file.
Note that the xml file that you want to parse should maybe be placed in the builddesktop folder instead of in its debug folder it depends on which folders Qt will
use as its default folder.
You can place several xml files here, and then list them in the Arguments edit box in
Run Settings remember to type at least one name (the samplefile.xml file, for
example) in the Arguments box if you want the program to run.
If you are getting slightly garbled output, then use the samplefile.xml that is
supplied with this document in the Additional Resources folder (in the Chapter
additional notes folder) there is sometimes a problem with the new line and
carriage return characters.
The example given here simply parses the xml file and displays it. Suppose you had a
Person class that had a name and an age. An xml file for this could look as follows:
<people>
<person name=Xin age=12 />
<person name=Ndou age=21 />
</people>
You thus know the structure of the xml file. If you now wanted to parse this file and then
create instances of your Person class, you could do something along the lines of the
following:
bool StockReader::startDocument()
{
return true;
}
Page 4 of 16
&namespaceURI,
const
bool StockReader::endDocument()
{
return true;
}
You would obviously have to have some way of dealing with p, the Person object that
was created, and that would depend on your individual program.
However, if the xml file is not based on attributes but on text, we could have the following:
<people>
<person>
<name>Xin</name>
<age>12</age>
</person>
<person>
<name>Ndou</name>
<age>21</age>
</person>
</people>
Parsing this is different, and would involve something along the lines of the following. The
variables used in the example (inName and inAge) would need to be members of the
XMLHandler class.
bool XMLHandler::startDocument()
{
inName = false;
inAge = false;
return true;
}
Page 5 of 16
Page 6 of 16
<<
<<
<<
<<
Root element
1st child
2nd child
Page 7 of 16
Create document
Create child
element and
add it to the
root
and
Process
Control
(randomNumbers.pro
and
The LogTail project as included in Ezust2 does not produce the desired output on
Windows because the command tail is only applicable on a UNIX variant operating
system.
A new example is provided in the Chapter additional notes folder (in the QProcess and
process control folder) to demonstrate starting, controlling, and communicating with other
processes similar to LogTail. To test the new project, follow the instructions below.
Firstly, open, build, and run the randomNumbers project so that randomNumbers.exe
is generated in the debug folder of the project. This project should display 10 random
numbers. You can now close this project.
Secondly, open and build the logRandom project. Before you execute the project, copy
and paste randomNumbers.exe from the debug folder of the randomNumbers project
RandomNumbers into the debug folder of the logRandom project. Run the project to
view the output.
Section 17.1.1: Processes and Environment (environment.pro)
A modified version of this example, which works exactly as described in Ezust2 is provided
in the QProcess and process control\environment folder. Test the project using the
command line argument f NULL. The only difference is that the Windows version uses
Page 9 of 16
putenv rather than setenv, and different environment variables are used (ones that are
known in a Windows environment).
Section 17.1.2: Qonsole (qonsole1.pro)
This program will work as discussed in Ezust2. However, you will need to create your own
project file. In a Windows environment, you can use commands like
dir (to list directory contents)
cd (to change to another directory)
md (to make a directory)
cls (to clear the screen)
date (to get the current date)
help (to get help)
to check that the example is working. Please remember to type in the command exit
before you close the window to exit the command line process.
This design pattern is applicable when a class needs to create objects but doesnt know
which objects to create and the responsibility for creating objects can be transferred to the
subclasses. So, this design pattern defines a class with a function for creating an object
but the subclasses implement this function to create appropriate objects.
The UML structure of this design pattern is given below:
Page 10 of 16
There are a number of variations of this design pattern. Firstly, the FactoryMethod() in
Factory may provide a default implementation of the factory method and it may call the
FactoryMethod in one of its functions (operation()). This means the Factory is not
always necessarily abstract. Secondly, the factory method could take parameters and
based on them, several different objects can be created.
This means a
ConcreteFactory may create more than one type of object.
(2)
This design pattern is applicable when you need to create families of related or dependent
objects. This design pattern mainly has two hierarchies
(1) hierarchies of product classes, whose objects are meant to be used together
(2) then you have a hierarchy of factory classes to facilitate creation of the product
classes.
The UML structure of the design pattern is given below:
Singleton
This design pattern is applicable when you want to have a class which has only one
instance and this single instance can be accessed via a global access point. This is
Page 11 of 16
generally achieved by making the constructor of the class private, creating one
instance, and a function that can be used to return the single instance.
Since there is only one class involved, a UML diagram is not include here. Instead, one
way of implementing Singleton is demonstrated below:
Class declaration
class A{
public:
static A* getInstance();
private:
A();
static A* onlyInstance
};
Class definition
A* A::onlyInstance = NULL;
A::A(){}
A* A::getInstance(){
if(onlyInstance == 0)
onlyInstance = new A();
return onlyInstance;
}
As demonstrated in the code, the single instance of A is stored in the static variable
onlyInstance. When a request for an A instance is made via getInstance(), an
instance of A is created and stored in onlyInstance. All subsequent calls to
getInstance() do not create new instances but rather return the only instance of A
stored in onlyInstance.
Behavioural Patterns
(1)
Memento Pattern
This design pattern is applicable when the state of an object needs to be saved so that its
state can be restored later without violating the encapsulation of the class. The data
members of the class, which determine the state of its objects, may not all be accessible
(via setters and getters) outside the class. Hence the class itself has to be involved in
saving and restoring the state of its objects.
The UML structure of the design pattern is given below:
Page 12 of 16
Strategy pattern
The Strategy pattern is applicable when there is a family of algorithms that needs to be
made interchangeable based on the context. Using this design pattern each algorithm is
encapsulated in a class and the run time selection of a relevant algorithm is made
possible.
The UML diagram of this design pattern is given below:
Page 13 of 16
Faade Pattern
The Faade pattern is applicable when you would like to provide a simplified interface to a
complicated set of systems but yet allowing the client to access the functionality of the
underlying system. This design pattern defines a Faade that interacts and invokes the
subsystems to satisfy the request of the client.
A simplified diagram of this design pattern is given below:
(2)
Adapter/Wrapper Pattern
This design pattern is applicable when the functionality of a class can be reused but the
interface of the class is not compatible with the existing classes. So this design pattern
converts the interface of an existing class into another as expected by the client. This
Page 14 of 16
design pattern is sometimes called a Wrapper pattern since it can be seen as wrapping
around an existing class to present a different interface to other classes.
There are two forms (Class Adapter and Object Adapter) of the Adapter pattern and the
UML diagrams of both forms of the Adapter pattern are given below:
Class Adapter:
Object Adapter:
Page 15 of 16
References:
E Gamma, R Helm, R Johnson & JM Vlissides (1995). Design Patterns: Elements of
Reusable Object-Oriented Software. Addision-Wesley
Lasater CG (2007). Design Patterns. Wordware Applications Library
Page 16 of 16