mardi 24 janvier 2017

Design Pattern: Abstract Factorization C++

ok so basically we are trying to build Paint application in C++ using MFC. we currently trying to implement Abstract factorization dp to our project and we just "running in circles" so called. so shortly about the classes we have (there are others but their irrelevant to the question)

Shape.h  
sRectangle.h  
sTriangle.h  
sEllipse.h  
shapeFactory.h  
RectangleFactory.h  
TriangleFactory.h  
ellipseFactory.h  
Add_Shape.h  
Command.h  

the shape factorization currently works but what we trying to implement is Command factorization, which will allow us to switch around the tool bar between creation of some specific shape or using another functionality like "Move" or "Fill" which will be used to fill in some shapes with different colors.

we tried reading in different places about the abstract factorization but no successful try yet.

this is the usability of the Add_Shape factorization currently:

void CPaintDlg::OnLButtonDown(UINT nFlags, CPoint point)
{       
    isPressed = true;
    Shape* s=chosenShape->createShape();
    Command* com = new Add_Shape(Shapes, s);
    com->perform();

    commands.push(com);
    Clear_Stack(undoes);
    s->SetAll(point, point, picked_color, picked_border_color, Pen_Width, Cpen_Style);


    Invalidate();

    CDialogEx::OnLButtonDown(nFlags, point);
}

there are 3 buttons in the tool bar (1 for each shape) with the following example functionality (There are buttons for the "Move" and "fill" with no current functionality):

void CPaintDlg::DrawRectangle()
{
    chosenShape= RectangleFactory::getInstance();
}

the CpaintDlg class members are:

CTypedPtrArray<CObArray, Shape*> Shapes;
bool isPressed;
stack<Command*> commands, undoes;
shapeFactory* chosenShape;
RECT rect;

Command class implementation:

#pragma once
class Command
{
public:
    Command();
    ~Command();
    virtual void perform() = 0;
    virtual void rollBack() = 0;
};

Add_Shape class implementation:

class Add_Shape :
    public Command
{
public:
    Add_Shape(CTypedPtrArray<CObArray, Shape*> &Shapes, Shape *figure);
    ~Add_Shape();
    virtual void perform();
    void rollBack();

private:
    CTypedPtrArray<CObArray, Shape*> &Shapes;
    Shape *figure;
};


//Add_Shape.cpp
#include "stdafx.h"
#include "Add_Shape.h"

Add_Shape::Add_Shape(CTypedPtrArray<CObArray, Shape*> &Shapes, Shape *figure) :
        figure(figure), Shapes(Shapes) {}

Add_Shape::~Add_Shape(){}

void Add_Shape::perform()
{ Shapes.Add(figure); }

void Add_Shape::rollBack()
{ Shapes.RemoveAt(Shapes.GetSize()-1); }

Let me know if any of you need any other information or implementations. help would be gladly accepted.

Aucun commentaire:

Enregistrer un commentaire