Summer_feeds
Just another WordPress.com weblog

Sep
09

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
Jul
22

The singleton pattern is done to get the Object open for one instance only.There the object is made in static method inside the class.

Classes objects participating in the pattern as members. Load Balancer is a member in the pattern usage:

* defines an Instance operation that lets clients access its unique instance. Instance is a class operation.
* responsible for creating and maintaining its own unique instance

The code samples are made available for C#.net and Java

using System;

namespace DoFactory.GangOfFour.Singleton.Structural
{

// MainApp test application

class MainApp
{

static void Main()
{
// Constructor is protected — cannot use new
Singleton s1 = Singleton.Instance();
Singleton s2 = Singleton.Instance();

if (s1 == s2)
{
Console.WriteLine(“Objects are the same instance”);
}

// Wait for user
Console.Read();
}
}

// “Singleton”

class Singleton
{
private static Singleton instance;

// Note: Constructor is ‘protected’
protected Singleton()
{
}

public static Singleton Instance()
{
// Use ‘Lazy initialization’
if (instance == null)
{
instance = new Singleton();
}

return instance;
}
}
}

Answer = objects are the same instance

Java code organizer as given out
package com.designpatterns.Singleton;

public class Singleton {

public static void Main()
{
// Constructor is protected — cannot use new
Singleton s1 = new Singleton();
Singleton s2 = new Singleton();

if (s1 == s2)
{
System.out.println(“Objects are the same instance”);
}

// Wait for user
System.out.println();
}

// Note: Constructor is ‘protected’
protected Singleton()
{
}

public static Singleton Instance()
{
Singleton instance = null;
// Use ‘Lazy initialization’
if (instance == null)
{
instance = new Singleton();
}

return instance;
}
}

Jul
15

Pattern as given in the pattern recongniztion-

Pattern recogniztion is done with the given observer.

Observer Pattern is specified to communication between layers given in the observer. Observer pattern is specified with the Visitor inside the pattern specified.

Observer Pattern components

participants

The classes and/or objects participating in this pattern are:

  • Subject (Stock)
    • knows its observers. Any number of Observer objects may observe a subject
    • provides an interface for attaching and detaching Observer objects.
  • ConcreteSubject (IBM)
    • stores state of interest to ConcreteObserver
    • sends a notification to its observers when its state changes
  • Observer (IInvestor)
    • defines an updating interface for objects that should be notified of changes in a subject.
  • ConcreteObserver (Investor)
    • maintains a reference to a ConcreteSubject object
    • stores state that should stay consistent with the subject’s
    • implements the Observer updating interface to keep its state consistent with the subject’s

// Observer pattern — Structural example
using System;
using System.Collections;namespace DoFactory.GangOfFour.Observer.Structural
{// MainApp test application

class MainApp
{
static void Main()
{
// Configure Observer pattern
ConcreteSubject s = new ConcreteSubject();

s.Attach(new ConcreteObserver(s,”X”));
s.Attach(new ConcreteObserver(s,”Y”));
s.Attach(new ConcreteObserver(s,”Z”));

// Change subject and notify observers
s.SubjectState = “ABC”;
s.Notify();

// Wait for user
Console.Read();
}
}

// “Subject”

abstract class Subject
{
private ArrayList observers = new ArrayList();

public void Attach(Observer observer)
{
observers.Add(observer);
}

public void Detach(Observer observer)
{
observers.Remove(observer);
}

public void Notify()
{
foreach (Observer o in observers)
{
o.Update();
}
}
}

// “ConcreteSubject”

class ConcreteSubject : Subject
{
private string subjectState;

// Property
public string SubjectState
{
get{ return subjectState; }
set{ subjectState = value; }
}
}

// “Observer”

abstract class Observer
{
public abstract void Update();
}

// “ConcreteObserver”

class ConcreteObserver : Observer
{
private string name;
private string observerState;
private ConcreteSubject subject;

// Constructor
public ConcreteObserver(
ConcreteSubject subject, string name)
{
this.subject = subject;
this.name = name;
}

public override void Update()
{
observerState = subject.SubjectState;
Console.WriteLine(“Observer {0}’s new state is {1}”,
name, observerState);
}

// Property
public ConcreteSubject Subject
{
get { return subject; }
set { subject = value; }
}
}
}

Output- can be taken with observer O’s passing inside

Observer {x}’s new state is {1}

Observer {y}’s new state is {2}

Observer {z}’s new state is {3}

THis is a real world example which cocerns a example with IBM prod

This real-world code demonstrates the Observer pattern in which registered investors are notified every time a stock changes value.

Hide code

// Observer pattern — Real World example
using System;
using System.Collections;namespace DoFactory.GangOfFour.Observer.RealWorld
{

// MainApp test application

class MainApp
{
static void Main()
{
// Create investors
Investor s = new Investor(“Sorros”);
Investor b = new Investor(“Berkshire”);

// Create IBM stock and attach investors
IBM ibm = new IBM(“IBM”, 120.00);
ibm.Attach(s);
ibm.Attach(b);

// Change price, which notifies investors
ibm.Price = 120.10;
ibm.Price = 121.00;
ibm.Price = 120.50;
ibm.Price = 120.75;

// Wait for user
Console.Read();
}
}

// “Subject”

abstract class Stock
{
protected string symbol;
protected double price;
private ArrayList investors = new ArrayList();

// Constructor
public Stock(string symbol, double price)
{
this.symbol = symbol;
this.price = price;
}

public void Attach(Investor investor)
{
investors.Add(investor);
}

public void Detach(Investor investor)
{
investors.Remove(investor);
}

public void Notify()
{
foreach (Investor investor in investors)
{
investor.Update(this);
}
Console.WriteLine(“”);
}

// Properties
public double Price
{
get{ return price; }
set
{
price = value;
Notify();
}
}

public string Symbol
{
get{ return symbol; }
set{ symbol = value; }
}
}

// “ConcreteSubject”

class IBM : Stock
{
// Constructor
public IBM(string symbol, double price)
: base(symbol, price)
{
}
}

// “Observer”

interface IInvestor
{
void Update(Stock stock);
}

// “ConcreteObserver”

class Investor : IInvestor
{
private string name;
private Stock stock;

// Constructor
public Investor(string name)
{
this.name = name;
}

public void Update(Stock stock)
{
Console.WriteLine(“Notified {0} of {1}’s ” +
“change to {2:C}”, name, stock.Symbol, stock.Price);
}

// Property
public Stock Stock
{
get{ return stock; }
set{ stock = value; }
}
}
}

Output
Jul
08

Composite Pattern-  A tree structure of simple and composite objects

Is concerned about the Tree structure behavior.

A normal tree consists of  nodes and leafs.

  • Leaf doesnot have a follwing node following it.
  • Node have a following node below it.
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
    C# Implementation of composite Pattern

// Structural Pattern:COMPOSITE
using System;
using System.Collections;
//the single interface for primitives & composite types.|
abstract class Component
{
abstract public void AddChild(Component c);
abstract public void Traverse();
}
//A primitive type.
class Leaf : Component
{
private int value = 0;
public Leaf(int val)
{
value = val;
}
public override void AddChild(Component c)
{
//no action; This method is not necessary for Leaf
}
public override void Traverse()
{
Console.WriteLine(“Leaf:”+value);
}
}
//A composite type.
class Composite : Component
{
private int value = 0;
private ArrayList ComponentList = new ArrayList();
public Composite(int val)
{
value = val;
}
public override void AddChild(Component c)
{
ComponentList.Add(c);
}
public override void Traverse()
{
Console.WriteLine(“Composite:”+value);
foreach (Component c in ComponentList)
{
c.Traverse();
}
}
}
class MyMain
{
public static void Main()
{
//creating a TREE structure.
Composite root = new Composite(100);// Root
Composite com1 = new Composite(200); //Composite 1
Leaf l1 = new Leaf(10);//Leaf1
Leaf l2 = new Leaf(20);//Leaf2
//Add two leafs to composite1
com1.AddChild(l1);
com1.AddChild(l2);
Leaf l3 = new Leaf(30);//Leaf3
root.AddChild(com1);//Add composite1 to root
root.AddChild(l3);//Add Leaf3 directlt to root
root.Traverse();//Single method for both types.
}
}

Jul
07

State Pattern was discussed in detail. The example was discussed was regarding the telephone Booth.

The State pattern allows an object to change its behavior when its internal state changes. This pattern can be
observed in a vending machine. Vending machines have states based on the inventory, amount of currency
deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is
made, a vending machine will either deliver a product and no change, deliver a product and change, deliver
no product due to insufficient currency on deposit, or deliver no product due to inventory depletion.


Vending machines best describes the functionality of states with the different user actions. User actions are inputted and states are taken in.

The state patten manipulation given with the common example is stated as below.

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;
};

Then a simple pen tool could be used as shown in the PenTool

class PenTool : public AbstractTool {
public:
PenTool() : mMouseIsDown(false) {}
virtual void MoveTo(const Point& inP) {
if(mMouseIsDown) {
DrawLine(mLastP, inP);
}
mLastP = inP;
}
virtual void MouseDown(const Point& inP) {
mMouseIsDown = true;
mLastP = inP;
}
virtual void MouseUp(const Point& inP) {
mMouseIsDown = false;
}
private:
bool mMouseIsDown;
Point mLastP;
};

class SelectionTool : public AbstractTool {
public:
SelectionTool() : mMouseIsDown(false) {}
virtual void MoveTo(const Point& inP) {
if(mMouseIsDown) {
mSelection.Set(mLastP, inP);
}
}
virtual void MouseDown(const Point& inP) {
mMouseIsDown = true;
mLastP = inP;
mSelection.Set(mLastP, inP);
}
virtual void MouseUp(const Point& inP) {
mMouseIsDown = false;
}
private:
bool mMouseIsDown;
Point mLastP;
Rectangle mSelection;
};

Apr
21

My Summer of Code Proposal abstract ,I have made public.Hope its not againts the ethics of Google.
But as they have stated i have not made the detailed description public.Hope this will make some sence

My Interests:Geographical Mapping,Map Manipulation and Data visualization.

*GPS technologies+Navigation systems.
*Road Navigators+PDA navigation(TomTom One/TomTom PLUS services-http://www.tomtom.com/plus/index.php?Lid=8)
*Mapping/GPS Receivers
*AUhttp://code.google.com/soc/2008/app.html?org=osgeo&csaid=thinesh.gen%40gmail.com%3A275eeac7%3A68953ea1
Google Code – Summer of Code – ApplicationTOMATIC Routing.

Rough work Break-Down.

Month 1 (Starting From April 7th as with the student application Deadlines ) :-

Week 1/    Week2:– Requirements gathering , understanding the OSGeo domain also the OpenLayers workings withing OSGeo.
OpenLayer simple implementations will be practiced with practice examples.
These will be implemented by OpenLayers 2.5

Week 3/    Week4:– Use GIS software to input data files and manipulate that data to my requirement. Manipulate layers/points and get the data out as a learning experience.
Extensive work will be done with Java Script library.

•    Month 2:-
Week 1/    Week2:– Admin module design/manage feeds and WMS services.

Week 3/    Week4:- module -organize feeds/services into functional groups.

Month 3:-
Week 1/    Week2:- will make a thorough check of the functions fix any bugs present and release the Module on 20th July.

Week 3/    Week4:- Bug fixing /Developer guide.

Some of the points may be not required for Openlayers current module.I like to know what was the work progress done with Gsoc2007 and other projects around Openlayers to streamline the deliverables.

Apr
21

It was a great experience attending an international conference as a Bsc Student from Sri Lanaka.

I did not think this chance would break through,but it did somehow. Though it was little hard all alone there ,it was a great expossure in to the work going on in the GIS field…..

Nov
11

I was totally away from my own blog and it surely has not help things at all..SO from now on i am determined to update things as it happens..

Good to start off things with a new note(bury the flaming and get on with it 😦 )

I can talk about my new iPod nano from next week I guess…So it will not be so boring after all… Till then fingers crossed.

Jul
13

IRC is a wicked cool way to chat with the community.After using gmail chat-converssion was not all that hard.Just had to login and follow the rules and etiquote,..

Can login to any IRC with using >Gaim also. Have to put the hash and org name and u ll get in……..

Some of my favourite IRC’s are here as in irclog>

http://sahana.telascience.org/irclogs/

cheers

Jul
11
  • Sahana GIS Module.
  • These are the main three front ends.
  • 1) user_Feeds_manipulate= shows the user logged in and his access to feeds/adding/deleting feeds to the system. (Tab comes under Situation
    mapping->User/Roles)

situation mapping->Manage Data

2) data_format_Convert=coverts the data currently in the local machine to
the desired data format. They can be uploaded after that.(comes under
Situation mapping->Format Conversion)

data formats

3) Admin_manage_Groups=this page is for the Administrator only.He can view
all the feeds data available .He can add /delete any feed and can Organize
the services and feed.(Situation mapping->Manage Data).
Adding deleting feeds will be restricted to normal users. They can not access
this page.And all the feeds will not be accessible to them even in the
first page(user_Feeds_manipulate).

User feed manupulate