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.

Why would Chain of Responsibility be inappropriate when there is only one handler for each request?

I am currently working on USB firmware for a project. While figuring out how to best handle servicing requests from the host, I decided to implement a Chain of Responsibility pattern.

In my application, there will always be exactly one handler for each request retrieved from the host. So basically I set up my handler chain ahead of time and the loop starts with waiting for a request from the host. The request is passed to the "root" handler who decides, by examining part of the request, whether to handle it or pass it down the chain. Once the request is handled, we loop back to waiting for another request from the host.

Again, only one handler will ever handle a request. A request not being handled, i.e., traversing the entire handler chain, is an error condition.

After reading up on the Chain of Responsibility pattern from www.sourcemaking.com, this quote caught my eye (last paragraph in the Discussion section):

Do not use Chain of Responsibility when each request is only handled by one handler, or, when the client object knows which service object should handle the request.

Is there a good practical reason why this is true? It seems to me that using Chain of Responsibility in the case the author warns against is perfectly valid. Could anyone give me some insight on why this would be a bad design choice?

Java Extracting out Initializing private fields of a class

Is it possible to solve the following problem using inbuilt Java API? (I want to retain the strict private access as shown)

  1. I have n subclasses of an abstract class BaseModel.
  2. Each of these subclasses declare their own set of private String fields.
  3. Within the subclass constructor, I wish to set the private fields from a Map using Java Reflection. An example of this function:

    void setPrivateFields(Map<String, String> fieldsValuesMap) throws NoSuchFieldException, IllegalAccessException {
        for (Map.Entry<String, String> entry : fieldsValuesMap.entrySet()) {
            String fieldName = entry.getKey();
            String fieldValue = entry.getValue();
    
            Field field = this.getClass().getDeclaredField(fieldName);
            field.set(this, fieldValue);
        }
    }
    
    
  4. Is it possible to extract out the function I have described in 3) such that I do not have to rewrite the algorithm in all the constructors of the subclasses?

    class BaseModel {}
    
    class Project extends BaseModel {
        private String name;
        private String type;
    
        public Project(Map<String, String> fieldsValuesMap) {
            setPrivateFields(fieldsValuesMap);
        }
    }
    
    class Task extends BaseModel {
        private String description;
        private String rawDescription;
    
        public Task(Map<String, String> fieldsValuesMap) {
            setPrivateFields(fieldsValuesMap);
        }
    }
    
    class SubTask extends BaseModel {
      ...   
    }
    
    ...
    
    

Are all Java design patterns relevant to C# and vice versa? [on hold]

Design patters should be standard or template of how to solve a programming problem. They should be language-agnostic. However, I believe this is true only partially.

For example, C# and Java are usually considered very similar languages. However, while Java is purely object-oriented, C# mixes OO and functional programming. Thus, there should be some design patterns that do not make sense in Java or C#, but do in the other language.

One of such design patterns is definitely Observer. This is one of the most used design patterns in Java, but totally obsolete in C#, where events are used instead. I believe, Strategy is another design pattern that does not make sense in C#, since it can be replaced by delegate or Command.

So, are all design patterns relevant to both languages? Or does there exist a comprehensive list of Java- and C#-specific design patterns? Moreover, are statements from functional programming preferred to design patterns, or are they equal? Or should design patterns be used instead of functional programming?

Print alphabets using patterns in C programming

I have written the following:

char a[7]={0b11111111,
           0b10000001,
           0b10000001,
           0b11111111,
           0b10000001,
           0b10000001,
           0b10000001};

this I have written for all alphabets. I don't know the code to print respective char array when I type a text (for eg. Hello).

best practice in reading all sensors data

I'm new into android development, and at this stage I try to mimic some functionalities from other apps (please consider the image from bellow).

For example, there is one app o google play which gives details about all the sensors from a device. I know how to read the values from any sensor and how to get a list of all the sensors, but I don't know how to take all the values from all the sensors and to display them in real time.

Are there some good practices or design patterns which can help me achieve the real-time values in an elegant manner?

enter image description here

Mapping multiple routes (controller) using a router

I am looking at danny vankooten router library here. This looks good (though not sure how will it handle project from medium to large, example an ecommerce site). Now, going by the example this is mapping

$router->map('GET','/', 'home.php', 'home');
$router->map('GET','/home/', 'home.php', 'home-home');
$router->map('GET','/plans/', 'plans.php', 'plans');
$router->map('GET','/about/', 'about.php', 'about');
$router->map('GET','/contact/', 'contact.php', 'contact');
$router->map('GET','/tos/', 'tos.html', 'tos');

Let say I have a scenario where my website has 20-30 static pages or near about 50 controllers with 2-3 action/method each.

How do I map them all. If I use the above method of mapping I will probably end up having more than 100 lines of mapping and this does not looks right.

I believe there should be a way or short cuts/wildcard like check if there is a page or controller available,then load it or else throw a 404.

How do I map all the routes the right way?.