Java Web Hosting for Developers

Developing Applications with Java

}); } //paint the button public void paint(Graphics

Filed under: Design Patterns Java — webmaster @ 4:38 pm

}); } //paint the button public void paint(Graphics g) { super.paint(g); //first draw the parent button if(! mouse_over) { //if the mouse is not over the button //erase the borders Dimension size = super.getSize(); g.setColor(Color.lightGray); g.drawRect(0, 0, size.width-1, size.height-1); g.drawLine(size.width-2, 0, size.width-2, size.height-1); g.drawLine(0, size.height-2, size.width-2, size.height-2); } } } Using a Decorator Now that we ve written a CoolDecorator class, how do we use it? We simply create an instance of the CoolDecorator and pass it the button it is to decorate. We can do all of this right in the constructor. Let s consider a simple program with two CoolButtons and one ordinary JButton. We create the layout as follows: super (”Deco Button”); JPanel jp = new JPanel(); getContentPane().add(jp); jp.add( new CoolDecorator(new JButton(”Cbutton”))); jp.add( new CoolDecorator(new JButton(”Dbutton”))); jp.add(Quit = new JButton(”Quit”)); Quit.addActionListener(this); This program is shown below, with the mouse hovering over one of the buttons.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

Developing Applications with Java

decorator. In Java, this is completely impractical, because

Filed under: Design Patterns Java — webmaster @ 4:20 am

decorator. In Java, this is completely impractical, because there are literally hundreds of method calls in the base JComponent class that we would have to reimplement. Instead, while we will derive our Decorator from the JComponent class, we will use its container properties to forward all method calls to the button it will contain. Design Patterns suggests that classes such as Decorator should be abstract classes and that you should derive all of your actual working (or concrete) decorators from the abstract class. In this Java implementation, this is scarcely necessary since the base Decorator class has no public methods at all other than the constructor, since all of them are methods of JComponent itself. public class Decorator extends Jcomponent { public Decorator(JComponent c) { setLayout(new BorderLayout()); //add component to container add(”Center”, c); } } Now, let s look at how we could implement a CoolButton. All we really need to do is to draw the button as usual from the base class, and then draw gray lines around the border to remove the button highlighting. //this class decorates a CoolButton so that //the borders are invisible when the mouse //is not over the button public class CoolDecorator extends Decorator { boolean mouse_over; //true when mouse over button JComponent thisComp; public CoolDecorator(JComponent c) { super(c); mouse_over = false; thisComp = this; //save this component //catch mouse movements in inner class c.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { mouse_over=true; //set flag when mouse over thisComp.repaint(); } public void mouseExited(MouseEvent e) { mouse_over=false; //clear if mouse not over thisComp.repaint(); }

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Developing Applications with Java

THE DECORATOR PATTERN The Decorator pattern provides us

Filed under: Design Patterns Java — webmaster @ 3:49 pm

THE DECORATOR PATTERN The Decorator pattern provides us with a way to modify the behavior of individual objects without having to create a new derived class. Suppose we have a program that uses eight objects, but three of them need an additional feature. You could create a derived class for each of these objects, and in many cases this would be a perfectly acceptable solution. However, if each of these three objects require different modifications, this would mean creating three derived classes. Further, if one of the classes has features of both of the other classes, you begin to create a complexity that is both confusing and unnecessary. For example, suppose we wanted to draw a special border around some of the buttons in a toolbar. If we created a new derived button class, this means that all of the buttons in this new class would always have this same new border, when this might not be our intent. Instead, we create a Decorator class that decorates the buttons. Then we derive any number of specific Decorators from the main Decorator class, each of which performs a specific kind of decoration. In order to decorate a button, the Decorator has to be an object derived from the visual environment, so it can receive paint method calls and forward calls to other useful graphic methods to the object that it is decorating. This is another case where object containment is favored over object inheritance. The decorator is a graphical object, but it contains the object it is decorating. It may intercept some graphical method calls, perform some additional computation and may pass them on to the underlying object it is decorating. Decorating a CoolButton Recent Windows applications such as Internet Explorer and Netscape Navigator have a row of flat, unbordered buttons that highlight themselves with outline borders when you move your mouse over them. Some Windows programmers call this toolbar a CoolBar and the buttons CoolButtons. There is no analogous button behavior in the JFC, but we can obtain that behavior by decorating a JButton. In this case, we decorate it by drawing plain gray lines over the button borders, erasing them. Let s consider how to create this Decorator. Design Patterns suggests that Decorators should be derived from some general Visual Component class and then every message for the actual button should be forwarded from the

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services

Developing Applications with Java

unless the computation is relatively intensive and you

Filed under: Design Patterns Java — webmaster @ 3:44 am

unless the computation is relatively intensive and you are quite certain that the underlying data have not changed, this may not be worth the effort.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services

Developing Applications with Java

table or vector. However, it is perfectly possible

Filed under: Design Patterns Java — webmaster @ 5:30 pm

table or vector. However, it is perfectly possible for any composite element to remember its parent by including it as part of the constructor: public Employee(Employee _parent, String _name, float _salary) { name = _name; //save name salary = _salary; //and salary parent = _parent; //and parent subordinates = new Vector(); isLeaf = false; //allow children } This simplifies searching for particular members and moving up the tree when needed. Other Implementation Issues Implementing the list in the parent. If there are a very large number of leaves in a composite but only a few nodes, then keeping an empty Vector object in each leaf has some space implications. An alternative approach is to declare all of the objects of type Member, which implements only getName() and getValue() methods. Then you derive a Node class from Member which implements the add, remove and elements methods. Now only objects that are Node classes can have an enumeration of members. You can check for this in the recursive loop instead of returning empty Vector enumerators. if(emp instanceof Node) { Enumeration e = emp.elements(); while(e.hasMoreElements()) { Employee newEmp = (Employee)e.nextElement(); // etc. } In most cases it is not clear that the space saving justifies this additional complexity. Ordering components. In some programs, the order of the components may be important. If that order is somehow different from the order in which they were added to the parent, then the parent must do additional work to return them in the correct order. For example, you might sort the original Vector alphabetically and return the Enumerator to a new sorted vector. Caching results. If you frequently ask for data which must be computed from a series of child components as we did here with salaries, it may be advantageous to cache these computed results in the parent. However,

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

Developing Applications with Java

path.getLastPathComponent().toString(); //find that employee in the composite Employee

Filed under: Design Patterns Java — webmaster @ 5:37 am

path.getLastPathComponent().toString(); //find that employee in the composite Employee emp = boss.getChild(selectedTerm); //display sum of salaries of subordinates(if any) if(emp != null) cost.setText(new Float(emp.getSalaries()).toString()); } Restrictions on Employee Classes It could be that certain employees or job positions are designed so that they never should have subordinates. Assembly workers or salesmen may advance in the company by being named to a new position, but those holding these leaf positions will never have subordinates. In such a case, you may wish to design your Employee class so that you can specify that this is a permanent leaf position. One way to do this is to set a variable which is checked before it allows subordinates to be added. If the position is leaf position, the method returns false or throws an exception. public void setLeaf(boolean b) { isLeaf = b; //if true, do not allow children } //————————————- public boolean add(Employee e) { if (! isLeaf) subordinates.addElement(e); return isLeaf; //false if unsuccessful } Consequences of the Composite Pattern The Composite pattern allows you to define a class hierarchy of simple objects and more complex composite objects so that they appear to be the same to the client program. Because of this simplicity, the client can be that much simpler, since nodes and leaves are handled in the same way. The Composite pattern also makes it easy for you to add new kinds of components to your collection, as long as they support a similar programming interface. On the other hand, this has the disadvantage of making your system overly general. You might find it harder to restrict certain classes, where this would normally be desirable. The composite is essentially a singly-linked tree, in which any of the objects may themselves be additional composites. Normally, these objects do not remember their parents and only know their children as an array, hash

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Developing Applications with Java

Once we have constructed this Composite structure, we

Filed under: Design Patterns Java — webmaster @ 5:36 pm

Once we have constructed this Composite structure, we can load a visual JTree list by starting at the top node and calling the addNode() method recursively until all the leaves in each node are accessed: private void addNodes(DefaultMutableTreeNode pnode, Employee emp) { DefaultMutableTreeNode node; Enumeration e = emp.elements(); while(e.hasMoreElements()) { Employee newEmp = (Employee)e.nextElement(); node = new DefaultMutableTreeNode(newEmp.getName()); pnode.add(node); addNodes(node, newEmp); } } The final program display is shown below: In this implementation, the cost (sum of salaries) is shown in the bottom bar for any employee you click on. This simple computation calls the getChild() method recursively to obtain all the subordinates of that employee. public void valueChanged(TreeSelectionEvent evt) { //called when employee is selected in tree llist TreePath path = evt.getPath(); String selectedTerm =

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Developing Applications with Java

((Employee)subordinates.elementAt(i)).getSalaries(); return sum; } Note that this method

Filed under: Design Patterns Java — webmaster @ 6:51 am

((Employee)subordinates.elementAt(i)).getSalaries(); return sum; } Note that this method starts with the salary of the current Employee, and then calls the getSalaries() method on each subordinate. This is, of course, recursive and any employees which themselves have subordinates will be included. Building the Employee Tree We start by creating a CEO Employee and then add his subordinates and their subordinates as follows: boss = new Employee(”CEO”, 200000); boss.add(marketVP = new Employee(”Marketing VP”, 100000)); boss.add(prodVP = new Employee(”Production VP”, 100000)); marketVP.add(salesMgr = new Employee(”Sales Mgr”, 50000)); marketVP.add(advMgr = new Employee(”Advt Mgr”, 50000)); //add salesmen reporting to Sales Manager for (int i=0; i<5; i++) salesMgr .add(new Employee("Sales "+ new Integer(i).toString(), 30000.0F +(float)(Math.random()-0.5)*10000)); advMgr.add(new Employee("Secy", 20000)); prodVP.add(prodMgr = new Employee("Prod Mgr", 40000)); prodVP.add(shipMgr = new Employee("Ship Mgr", 35000)); //add manufacturing staff for (int i = 0; i < 4; i++) prodMgr.add( new Employee("Manuf "+ new Integer(i).toString(), 25000.0F +(float)(Math.random()-0.5)*5000)); //add shipping clerks for (int i = 0; i < 3; i++) shipMgr.add( new Employee("ShipClrk "+ new Integer(i).toString(), 20000.0F +(float)(Math.random()-0.5)*5000));

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

Developing Applications with Java

The Employee Class Our Employee class will store

Filed under: Design Patterns Java — webmaster @ 8:41 pm

The Employee Class Our Employee class will store the name and salary of each employee, and allow us to fetch them as needed. public class Employee { String name; float salary; Vector subordinates; //————————————- public Employee(String _name, float _salary) { name = _name; salary = _salary; subordinates = new Vector(); } //————————————-public float getSalary() { return salary; } //————————————-public String getName() { return name; } Note that we created a Vector called subordinates at the time the class was instantiated. Then, if that employee has subordinates, we can automatically add them to the Vector with the add method and remove them with the remove method. public void add(Employee e) { subordinates.addElement(e); } //————————————-public void remove(Employee e) { subordinates.removeElement(e); } If you want to get a list of employees of a given supervisor, you can obtain an Enumeration of them directly from the subordinates Vector: public Enumeration elements() { return subordinates.elements(); } The important part of the class is how it returns a sum of salaries for the employee and his subordinates: public float getSalaries() { float sum = salary; //this one s salary //add in subordinates salaries for(int i = 0; i < subordinates.size(); i++) { sum +=

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Developing Applications with Java

An Implementation of a Composite Let s consider a

Filed under: Design Patterns Java — webmaster @ 9:37 am

An Implementation of a Composite Let s consider a small company. It may have started with a single person who got the business going. He was, of course, the CEO, although he may have been too busy to think about it at first. Then he hired a couple of people to handle the marketing and manufacturing. Soon each of them hired some additional assistants to help with advertising, shipping and so forth, and they became the company s first two vice-presidents. As the company s success continued, the firm continued to grow until it has the organizational chart we see below: CEO Vp Mkt Vp prod Now, if the company is successful, each of these company members receives a salary, and we could at any time ask for the cost of any employee to the company. We define the cost as the salary of that person and those of all his subordinates. Here is an ideal example for a composite: The cost of an individual employee is simply his salary (and benefits). The cost of an employee who heads a department is his salary plus those of all his subordinates. We would like a single interface that will produce the salary totals correctly whether the employee has subordinates or not. public float getSalaries(); At this point, we realize that the idea of all Composites having the same standard interface is probably na ve. We d prefer that the public methods be related to the kind of class we are actually developing. So rather than have generic methods like getValue(), we ll use getSalaries(). Sales mgr Mkt mgr Pro mgr Ship mgr Sales Sales Secy Ship ShipManu Manu Manu

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

Next Page »

Powered by Java Web Hosting