Summer_feeds
Just another WordPress.com weblog

State Pattern


State Patterns-Pattern which shows the states it can remain in a machine.Best example to be taken is the Gumball machine example as stated. The gumball machine has 4 states;
1 Has quarter
2 No quarter
3 Put quarter
4 Machine full

These 4 states has to be manipulated in series.
The implementation code is given below:

public void hasQuarter()
{//Show the quarters existence as needed

Boolean hasQuarter;
Boolean endState;
int value;
String machineState;

}

private Element addComponent(String componentType, String componentId,
String componentName, String[] attributeNames, String[] methodNames) {
int height;
int width;

if (componentType.equalsIgnoreCase(CLASS_TYPE_CLASSNODE)) {
height = classHeight;
width = classWidth;
} else if (componentType.equalsIgnoreCase(CLASS_TYPE_PACKAGENODE)) {
height = classHeight;
width = classWidth;
}

else {height = classHeight;
width = classWidth;
}

Element element = document.createElement(“void”);
element.setAttribute(“method”, “addNode”);

Element element2 = document.createElement(“object”);
element2.setAttribute(“id”,componentId);
element2.setAttribute(“class”, componentType);

// element2.setAttribute(“id”,”ClassNode”+componentCounter);
element.appendChild(element2);

Element element3 = document.createElement(“void”);
element3.setAttribute(“property”, “bounds”);
element2.appendChild(element3);

Element element4 = document.createElement(“object”);
element4.setAttribute(“class”, “java.awt.geom.Rectangle2D$Double”);
element3.appendChild(element4);

Element element5 = document.createElement(“void”);
element5.setAttribute(“method”, “setRect”);
element4.appendChild(element5);

Element bounds1 = document.createElement(“double”);
bounds1.setTextContent(“” + lenX);

Element bounds2 = document.createElement(“double”);
bounds2.setTextContent(“” + lenY);

Element bounds3 = document.createElement(“double”);
bounds3.setTextContent(“” + width);

Element bounds4 = document.createElement(“double”);
bounds4.setTextContent(“” + height);

element5.appendChild(bounds1);
element5.appendChild(bounds2);
element5.appendChild(bounds3);
element5.appendChild(bounds4);

Element elementAttribute = document.createElement(“void”);
elementAttribute.setAttribute(“property”, “attributes”);

Element element9 = document.createElement(“void”);
element9.setAttribute(“property”, “text”);
elementAttribute.appendChild(element9);

Element stringElement = document.createElement(“string”);
if (attributeNames != null) {
String attributes = “”;
for (int i = 0; i < attributeNames.length; i++) {
attributes = attributes + attributeNames[i] + “\n”;
}
stringElement.setTextContent(attributes);
element9.appendChild(stringElement);
element2.appendChild(elementAttribute);
}

Element elementMethod = document.createElement(“void”);
elementMethod.setAttribute(“property”, “methods”);

Node elementvoidText = document.importNode(element9, true);

// adding the method names
if (methodNames != null) {
String methods = “”;
for (int i = 0; i < methodNames.length; i++) {
methods = methods + methodNames[i] + “\n”;
}
elementvoidText.getChildNodes().item(0).setTextContent(methods);
elementMethod.appendChild(elementvoidText);
element2.appendChild(elementMethod);
}

Element element8 = document.createElement(“void”);
element8.setAttribute(“property”, “name”);
element2.appendChild(element8);

// Element element9 = document.createElement(“void”);
// element9.setAttribute(“property”, “text”);
Node elementvoidText2 = document.importNode(element9, true);
elementvoidText2.getChildNodes().item(0).setTextContent(componentName);
element8.appendChild(elementvoidText2);

// Element stringElement2 = document.createElement(“string”);
// Node stringElement2 = document.importNode(stringElement, true);
// stringElement2.setTextContent(componentName);
// elementvoidText.appendChild(stringElement2);

Element element10 = document.createElement(“object”);
element10.setAttribute(“class”, “java.awt.geom.Point2D$Double”);
element.appendChild(element10);

Element element11 = document.createElement(“void”);
element11.setAttribute(“method”, “setLocation”);
element10.appendChild(element11);

element11.appendChild(document.importNode(bounds1, true));
element11.appendChild(document.importNode(bounds2, true));

lenX += 100;
lenY += 100;

return element;

}

STATE PATTERN

The state pattern is a behavioral software design pattern, also known as the objects for states pattern. This pattern is used in computer programming to represent the state of an object. This is a clean way for an object to partially change its type at run time [1].

Take for example, a drawing program, in which there could be an abstract interface representing a tool, then concrete instances of that class could each represent a kind of tool. When the user selects a different tool, the appropriate tool would be instantiated.

For example, an interface to a drawing tool could be

class AbstractTool {
public:
virtual void MoveTo(const Point& inP) = 0;
virtual void MouseDown(const Point& inP) = 0;
virtual void MouseUp(const Point& inP) = 0;

};

Advertisements

No Responses to “State Pattern”

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: