Need help with Factory Pattern -> previous exam Question. QTCreator

HideInLight

Expert Member
Joined
Oct 31, 2006
Messages
4,600
Reaction score
1,061
kXvbeCZ.jpg


CVhZ0xo.jpg


This is one question I just can't seem to do, caught me out last time.

Start from 4.3

What completely throws me off to the point that I don't know what to do is the fact that Shape2 inherits from Shape.
 
class ShapeFactory

public:

Shape* createShape(QString shapeChoice);



*Shape ShapeFactory::createShape(QString shapeChoice)
{
if (shapeChoice == "circle")
return new Circle;

if (shapeChoice == "square")
return new Square;
}

Not sure if that's correct in terms of 4.3, they only ask for Shape.
I would imagine you'll need one Abstract Factory with two factories inheriting from it, ShapeFactory and Shape2Factory.
 
Last edited:
These questions are not that hard.

In 4.3, write a factory method that returns an object of Shape type. Not Shape2.
In 4.6, the factory must be instantiated only once. So we will need to Singleton pattern for that.
In. 4.7, the feature would be saving and restoring the state of a Shape object upon requested by the application.

The rest is easy :) Check myUnisa for design pattern videos.
 
Not sure if that's correct in terms of 4.3, they only ask for Shape.
I would imagine you'll need one Abstract Factory with two factories inheriting from it, ShapeFactory and Shape2Factory.

The shape pointer can also take Ellipse and Rectangle since they also inherit from Shape (albeit indirectly).
 
class Client
{
public:
Client();
void create();
private:
Shape* myShape


4.4

void Client:create()
{
AbstractShapeFactory* factory = new ShapeFactory;
myShape = factory->createShape("circle");
}


//If it was a Ellipse or Rectangle I would assume
AbstractShapeFactory* factory = new Shape2Factory;
myShape = factory->createShape("ellipse");
 
Last edited:
These questions are not that hard.

In 4.3, write a factory method that returns an object of Shape type. Not Shape2.
In 4.6, the factory must be instantiated only once. So we will need to Singleton pattern for that.
In. 4.7, the feature would be saving and restoring the state of a Shape object upon requested by the application.

The rest is easy :) Check myUnisa for design pattern videos.

Anyone got example of the Singleton pattern or how it would actually be implemented in this case. Unfortunately that's the one pattern that doesn't have a video.
Will see if Java has it.
 
There's one in assignment 2 Question 2. See FilmMap.
 
The shape pointer can also take Ellipse and Rectangle since they also inherit from Shape (albeit indirectly).

Don't think you can do that. Can you?

Shape* s = new Rectangle;

All I know is you can instance the concrete classes.
 
Last edited:
Don't think you can do that. Can you?

Shape* s = new Rectangle;

You can since it's also a shape. Rectangle inherits Shape2 which inherits Shape.
You're instantiating a concrete class.
 
Last edited:
Write and run this code to see if it works.

Ok seems you can.

void Client:create()
{
AbstractShapeFactory* factory = new ShapeFactory;
myShape = factory->createShape("circle"); //Shape* myShape
}

{
AbstractShapeFactory* factory = new ShapeFactory;
myShape = factory->createShape("rectangle"); //Shape* myShape
}

So you can use the same factory to draw any shape.
 
So 4.3 must look more like this then.

class ShapeFactory

public:

Shape* createShape(QString shapeChoice);



*Shape ShapeFactory::createShape(QString shapeChoice)
{
if (shapeChoice == "circle")
return new Circle;

if (shapeChoice == "square")
return new Square;

if (shapeChoice == "rectangle)
return new Rectangle;

if (shapeChoice == "ellipse")
return new Ellipse
}
 
So 4.3 must look more like this then.

// shapefactory.h

#include "shape.h"
#include "shape2.h"


class ShapeFactory

public: {

ShapeFactory(); // The class needs to be written in full in order to get full marks
Shape* createShape(QString shapeChoice); }

// shapefactory.cpp

*Shape ShapeFactory::createShape(QString shapeChoice)
{
if (shapeChoice == "circle")
return new Circle();

if (shapeChoice == "square")
return new Square();

if (shapeChoice == "rectangle)
return new Rectangle();

if (shapeChoice == "ellipse")
return new Ellipse();
}

Ya, but the class needs to be written in full. ie it should be compiled without problems. Syntactical errors will cost you marks.

The factory method pattern's easy, isn't it? :) We only need to know 5 patterns for the exam: Factory Method, Singleton, MetaObject, Serializer and Memento.
 
The question does state that preprocessor directives aren't needed and implementation and definition can be combined.
 
Last edited:
:D

How's your exam preparations going? I still need to study Network programming in tutorial letter 103 and QtConcurrency in chapter 17. Everything else's been studied thoroughly.

Good so far. I haven't seen any questions on QtConcurrency in the exams they mainly seem to focus on QThread.
 
The question does state that preprocessor directives aren't needed and implementation and definition can be combined.

Oh yes, you are right :) My bad.

Good so far. I haven't seen any questions on QtConcurrency in the exams they mainly seem to focus on QThread.

Well, that's comforting. I suppose I should practice subclassing QThread along with the use of QMutex and QMutexlocker. The official solution to the last question (Stock simulation app) in assignment 2 involves the use of QMutex as an alternative to QTimer (I used this in my answer to the question). Better safe than sorry.
 
Top
Sign up to the MyBroadband newsletter
X