tag:blogger.com,1999:blog-62687675328088512032024-02-07T08:00:42.927-08:00Sandeep Gadhvisandeep.gadhvihttp://www.blogger.com/profile/15389821904029065699noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-6268767532808851203.post-49381548122829804242010-02-01T06:41:00.001-08:002010-02-01T06:47:30.405-08:00The Template Method Pattern<span style="font-weight: bold;">Note: CLICK IMAGE TO ENLARGE.<br /><br />About</span> :The Template Pattern uses the inheritance beautifully to take advantage of code reuse. No composition is used because of the strict nature of the circumstances where it should be used. The subclasses are bound to implement the abstract behavior of superclass and give the implementation in their own way and yet the sequence of algorithm is made unchangeable by making the method, of superclass which has the algorithm, final.<br /><br /><span style="font-weight: bold;">Definition </span>: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.<br /><br /><span style="font-weight: bold;">When to use it</span>: When you have a fixed algorithm to execute from a superclass and which must not be changed in sequence but should behave differently in subclass specific way then its good to use this pattern for<br />1)integrity reason, of the algorithm's sequence<br />2)code reuse, if there are many subclasses which all depend on superclass implementation of certain functions.<br /><br /><span style="font-weight: bold;">How to use it</span> : In this pattern, a superclass will define a method, which has a fixed sequence in calling some methods, we call it template method or an algorithm. Superclass will have certain methods whose implementation is given by superclass itself and some methods which are defined as abstract, which must be implemented by the subclasses. Define this template method as final in superclass so that, subclasses can not change the sequence of algorithm and still contribute subclass-specific way by giving their own implementation of abstract methods. So the client will call the final method of subclass, inherited from superclass, and get the implementation of the algorithm in the subclass-specific way.<br /><br /><span style="font-weight: bold;">Example </span>: The diagram 1.1 shows the initial design where two different classes has an algorithm which has same sequence of methods but each having its implementation of those methods in their own way. Here, the same code is scattered across the classes.<br /><br /><br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuMVPnN1zxQmjvdpIMMF_z_K8_qPzsveNlfnw8V1debI5TdXY5hQIf6Bn-x7wNEEo7CwouO9_iAR0cqZuJnVOJf9y6JxmAYLDA8yyUGAB1sfiBJ-uqHiNm83svQE8e5Xjizt0vIXunxw9v/s1600-h/same.JPG"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 400px; height: 231px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuMVPnN1zxQmjvdpIMMF_z_K8_qPzsveNlfnw8V1debI5TdXY5hQIf6Bn-x7wNEEo7CwouO9_iAR0cqZuJnVOJf9y6JxmAYLDA8yyUGAB1sfiBJ-uqHiNm83svQE8e5Xjizt0vIXunxw9v/s400/same.JPG" alt="" id="BLOGGER_PHOTO_ID_5433285369039643314" border="0" /></a>The diagram 1.2 shows the Template Method Pattern, where we take out the common behavior of subclasses into superclass creating a common point of change which will reflect to all the subclasses. We define the subclass-specific methods in superclass as abstract thus making mandatory for subclass to give implementation. Also, we create a new method, Template Method(on which the name is given to this pattern), by calling the methods in the sequence by which we want our common algorithm to be executed. And , defining it as final so that we are sure that no subclass can change the sequence and just give its own implementation.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9sxSS3s3roLC0aFlpl83jNcelFjA_mmc0_B8QcWz-r_KpEv6-FObrGYteXp3bquYTdwraZan8op14rqjNsF4Qn84__uTkb2FPqQhGU1nbom7n-_csd8wy_420vUFwFKSxBgNJ0VZfShCY/s1600-h/template.JPG"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 400px; height: 282px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9sxSS3s3roLC0aFlpl83jNcelFjA_mmc0_B8QcWz-r_KpEv6-FObrGYteXp3bquYTdwraZan8op14rqjNsF4Qn84__uTkb2FPqQhGU1nbom7n-_csd8wy_420vUFwFKSxBgNJ0VZfShCY/s400/template.JPG" alt="" id="BLOGGER_PHOTO_ID_5433285310222416626" border="0" /></a><br /><style type="text/css"> <!-- @page { margin: 0.79in } P { margin-bottom: 0.08in } --> </style> <p style="margin-bottom: 0in; font-style: normal;"><span style="font-weight: bold;">Conclusion</span> : To conclude, the sequence,which must be same throughout the subclasses, is defined by the superclass and some part of the algorithm is executed by subclasses such design is a very good example of secure algorithm ensuring fixed sequence but with added touch of subclasses who give implementation differently.<br /></p>sandeep.gadhvihttp://www.blogger.com/profile/15389821904029065699noreply@blogger.com0tag:blogger.com,1999:blog-6268767532808851203.post-63696971887164276472010-01-29T05:18:00.000-08:002010-01-29T05:32:50.498-08:00The Adapter Pattern<span style="font-weight: bold;">Note</span>: <span style="font-style: italic;">The word client and caller are used interchangeably in this document which both refer to the class/method which calls the method of another class.</span><br /><br /><span style="font-weight: bold;">Note: CLICK ON IMAGE TO ENLARGE IT.</span><br /><br /><span style="font-weight: bold;">About </span>: Being adaptive is at the core of any human activity which requires a person to become conversant to the changing environment. And surprisingly OOP also comes under the same category!!. Many times it happens that the class or interface we are using is not,for any particular reason , compliant with the one which client expects. In such cases the adapter pattern can come to our rescue. It is recommended that you use this pattern wisely in order to avoid unexpected results. <br /><br /><span style="font-weight: bold;">Definition</span> :converts the interface of a class into another interface the <span style="font-style: italic;">clients expect</span>. Adapter lets classes work together that couldn't otherwise because of incompatible interface.<br /><br /><span style="font-weight: bold;">When to use it</span>: We can use this pattern when, due to compatibility issues the caller does not identify our interface as the one expected by it. Let's say, we have a library provided by our client which expects some interface and your company has ,in past, developed some libraries. Now, in order for our libraries to be used by client-provided libraries they must be compatible. What this pattern does is they <span style="font-weight: bold; font-style: italic;">MAKE the client SEEM</span> that it is compatible.<br /><br /><span style="font-weight: bold;">How to use it</span> : All we need to do is, create a concrete class which implements the interface which client expects and, <span style="font-style: italic; font-weight: bold;">delegate the request</span> to the concrete class which implements the interface, which we need to function in response.<br /><br /><span style="font-weight: bold;">Example</span> : The diagram 1.1 illustrates how the request came for a target interface gets delegated to another interface. Here, the client only <span style="font-style: italic;">“looks”</span> at the interface being implemented.<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhk8WXdRpaSVL3S2jf4ZE4Juztm52vogN83HnSOin8nri3iRcEmcJaET8HnnbYz89wf3FdL8Nch1PprisT5sGigG_k6fIBsx3sSChSsSCvLo0D6RrSdtXBX43cO8qolyQG_cCwFLLCc6xe_/s1600-h/framework.JPG"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 400px; height: 231px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhk8WXdRpaSVL3S2jf4ZE4Juztm52vogN83HnSOin8nri3iRcEmcJaET8HnnbYz89wf3FdL8Nch1PprisT5sGigG_k6fIBsx3sSChSsSCvLo0D6RrSdtXBX43cO8qolyQG_cCwFLLCc6xe_/s400/framework.JPG" alt="" id="BLOGGER_PHOTO_ID_5432153714766174690" border="0" /></a><br /><br /><br /><br />To make the pattern more clear, let's understand it with a real world(confined to Java) example.<br /><br /> We have nice collection interfaces, java.util.Enumeration and java.util.Iterator. Both having methods to iterate through the objects of any of the java.util.Collection subclasses. Now,through the use of Adapter Pattern, we can create an adapter which makes client seem, if not actually make, the objects of java.util.Iterator compatible with objects of java.util.Enumeration. Diagram 1.2 below shows the class diagram for the same.<br /><br /><br /><br /><br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBRFpqnhwtZ8a0hOoEnvbSmScofey2egBXfZZg-mB_1YGla3PWxZOiQnv0_Ck7xqW4Ztz4IZhpUNEJ8Vf3Eg94_Lbj1iGXiOSn22lHlPrHi125IFUlgY5w3veVWVIMM4NX52KH8yTSfBK7/s1600-h/enumerationIterator.JPG"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 400px; height: 231px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBRFpqnhwtZ8a0hOoEnvbSmScofey2egBXfZZg-mB_1YGla3PWxZOiQnv0_Ck7xqW4Ztz4IZhpUNEJ8Vf3Eg94_Lbj1iGXiOSn22lHlPrHi125IFUlgY5w3veVWVIMM4NX52KH8yTSfBK7/s400/enumerationIterator.JPG" alt="" id="BLOGGER_PHOTO_ID_5432151001047835794" border="0" /></a><br /><span style="font-weight: bold;">Conclusion</span> : To summarize, when the “type” expected by the client differs from the one we have, use this pattern. To make components more adaptive,which might not have been otherwise , we can use this pattern to suit to our need.<br /><br /><br />Any suggestions or queries are welcomed.sandeep.gadhvihttp://www.blogger.com/profile/15389821904029065699noreply@blogger.com0tag:blogger.com,1999:blog-6268767532808851203.post-64692249184174021132010-01-28T01:24:00.000-08:002010-01-28T01:35:22.975-08:00The strategy Pattern<span style="font-weight: bold; font-style: italic;">Note</span>: <span style="font-style: italic;">This design pattern and all those which are posted on this blog are written by me after reading the book "Head First Design Patterns" so the definition and understanding of ideas will be from that book. The purpose is to give a concise explanation of the design patterns.For more detailed explanation please refer to the book</span><br /><br /><span style="font-weight: bold;">Note: CLICK ON THE IMAGE TO ENLARGE IT.</span><br /><br /><span style="font-weight: bold;">About</span> : A beautiful design pattern which demonstrates how we can ,through the use of composition( HAS-A relation) over inheritance( IS-A relation) , reduce the harmful effects of change in code and make the design more flexible.<br />We can change behavior at runtime and change the code without affecting the client, which uses them.<br /><br /><span style="font-weight: bold;">Definition</span> : The strategy Pattern defines a family of algorithms, encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.<br /><br /><span style="font-weight: bold;">When to use it</span>: when we have common type of behavior in our various subclasses and they may change in future and if the subclasses are too many then we need to change the the behavior by editing each and every class where change is needed. This happens because our implementation is “locked” inside our subclasses and thus whenever change is required we need to change each and every subclass.<br /><br /><span style="font-weight: bold;">How to use it</span> : Identify aspects of our application that vary and separate them from what stays the same. So, by making a loose coupled design through the interface we make the caller unaware of the implementation and thus implementation can change without touching code at the caller side.<br /><br /><span style="font-weight: bold;">Example</span> : Lets say we have a class which has two behaviors one is common to both the subclasses and other is new behavior which is applicable to one subclass which not to the other. So in this case if we depend on inheritance then we have to give empty implementation of the new behavior in the SubClass2. This design is not loosely coupled, implementation is locked inside subclasses. So in case we have to change the new behavior in any subclasses then we need to make changes there. If there were 20 behaviors and 100 subclasses then the changes would be felt worst.<br /><br /><br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjC6eiwt9P6ANggW5oTZGTVrBdYuQLkK6KxkQCEv6haGjJgfagYBmdJX80jYSmdFIW0VRGkzKPH_He0Kl0qzTtndkCtU3GOdKw38H3n99X2rCkZKGCWf0QMTuXcQrlYBABLpW0GQcbvaCGE/s1600-h/initialWithChange.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 329px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjC6eiwt9P6ANggW5oTZGTVrBdYuQLkK6KxkQCEv6haGjJgfagYBmdJX80jYSmdFIW0VRGkzKPH_He0Kl0qzTtndkCtU3GOdKw38H3n99X2rCkZKGCWf0QMTuXcQrlYBABLpW0GQcbvaCGE/s400/initialWithChange.JPG" alt="" id="BLOGGER_PHOTO_ID_5431720175900818402" border="0" /></a>So we'll try to make our design more flexible by introducing interface and removing the things that changes from subclasses and separating that into interface.This change seems convincing as the behavior is made optional through use of interface. The classes which needs this behavior will implement the interface. But still the change in the subclasses will need us to alter the code in the subclasses. Thus ,this design still depends on the implementation given by the subclasses who implement the interface.<br /><br />Lets improve the design a little bit. This time we'll separate the implementation not through inheritance but through composition.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrG2SFwmuQ1USCIi-BHbSeDJddjaRzOwpDGQvKkJtcPA0vLbT6bZcmNRqKGbqg-6ezU-KPEa7o8u_sL-j092AHJ7HIQuZgvkTR8GALxdWUPqBW00TcJHGCnH6woGRmemGLVfnC2-m9PEsw/s1600-h/withInterface.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 283px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrG2SFwmuQ1USCIi-BHbSeDJddjaRzOwpDGQvKkJtcPA0vLbT6bZcmNRqKGbqg-6ezU-KPEa7o8u_sL-j092AHJ7HIQuZgvkTR8GALxdWUPqBW00TcJHGCnH6woGRmemGLVfnC2-m9PEsw/s400/withInterface.JPG" alt="" id="BLOGGER_PHOTO_ID_5431720115335914354" border="0" /></a><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDKGHU8qIE6zC-sEr9YxZMolF_fXP2olMaRZPCGaa37JHnkv7BJpLQy0Gz7uRFEYKFSZBmdZb6M6sHCJLE5qulC5VkIlzJbZcneRFJxqpl3akkDcuFf-wl3perBv07Vi07y9taU4ECr5yx/s1600-h/untitled.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 270px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDKGHU8qIE6zC-sEr9YxZMolF_fXP2olMaRZPCGaa37JHnkv7BJpLQy0Gz7uRFEYKFSZBmdZb6M6sHCJLE5qulC5VkIlzJbZcneRFJxqpl3akkDcuFf-wl3perBv07Vi07y9taU4ECr5yx/s400/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5431719999458563138" border="0" /></a>Now the implementation is separated from the place where it was used. So, any subclass can use any of the XYZBehavior implementation through the reference it holds of XYZable interface. And so the changes made in XYZBehavior implementation does not affect the caller(i.e. Any subclass ).This makes design more flexible.<br /><br /><br /><span style="font-weight: bold;">Conclusion</span> : Separate the functionalities of your application that changes from what stays the same. And prefer to use composition over inheritance when you need a flexible design because interface gives dynamic allocation of objects and loose coupling while inheritance gives implementation which is static inside the class and makes code hard to manage when exposed to changes.<br /><br />Any suggestions or queries are welcomed.sandeep.gadhvihttp://www.blogger.com/profile/15389821904029065699noreply@blogger.com0tag:blogger.com,1999:blog-6268767532808851203.post-91132409366498486122009-01-11T19:53:00.000-08:002009-01-11T20:58:36.880-08:00New Java ME Confirmation AlertHello All,<br /> This Java ME alert I've created which might be helpful while creating project with large amount of Forms.<br /><br /><span style="font-weight: bold;font-size:130%;" >Description</span> : This example consists of one midlet class , one ConfirmationAlert class and one ConfirmAlertListener interface.<br /><span style="font-size:130%;"><br /></span><span style="font-weight: bold;font-size:130%;" >Classes and Interfaces Used</span>:<br /><span style="font-style: italic;">ConfirmAlertListener</span> : as being interface it has two methods (Contract with implementer class)<br /> 1) yesPressed -called on positive command press<br /> 2) noPressed -called on negative command press<br /><br /> Note: Here positive and negative word means Positive response(like <br /> "Yes","Ok") and Negative response (like "Cancel","Back")<br />ConfirmationAlert : this class "Delegates" call and behaves as a bridge between<br /> CommandListener of lcdui(i.e. itself) and<br /> ConfirmAlertListener(i.e. in this case HelloWorld ).<br /><br /><div style="text-align: left;"><span style="font-style: italic;">HelloMIDlet</span> : this class uses functionality of ConfirmationAlert class and implements <br /> ConfirmAlertListener interface.<br /><span style="font-size:130%;"><br /></span><span style="font-weight: bold;font-size:130%;" >Design Principles Applied</span>:<br /> 1) seperation of concern<br /> 2) program to interface<br /><br /><span style="font-weight: bold;font-size:130%;" >Design Pattern Used</span><span style="font-size:130%;">:</span><br /> 1) Observer Pattern<br /><br /><span style="font-weight: bold;font-size:130%;" >Benifits</span>:<br /> In a project which has many forms whith which user interacts in that case there are frequent ocassions in which you might feel to ask user for confirmation about some action, in that case this code is very helpful to generate "Quickly" an alert with message and two commands(one for positive and another for negative response).<br /> In addition to quickly creating an custom alert this makes design more simple , just create ConfirmAlert and register your class that's it.<br /><br /><span style="font-weight: bold;font-size:130%;" >Drawbacks</span>: <br /> Main drawback is as this is for project with more number of forms , it will create two(one class and one interface) components which will take some amount of memory.<br /> So if you are not concerned too much with memory and target devices are having enough memory this will surely help you.<br /> In case where memory is main concern you can surely have "Inner Version" of this class and interface that might help you.<br /><span style="font-size:130%;"><br /></span><span style="font-weight: bold;"><span style="font-size:130%;">Code</span>:</span><span style="font-family:webdings;"><span style="font-weight: bold;"><br /><br /></span></span></div><span style="font-family:webdings;">package hello;</span><br /><span style="font-family:webdings;">/**</span><br /><span style="font-family:webdings;"> * This listener interface is for alert of type</span><br /><span style="font-family:webdings;"> * confirmations.so the class implementing it is notified when</span><br /><span style="font-family:webdings;"> * specific confirmation response is given by user.</span><br /><span style="font-family:webdings;"> * @author Sandeep Gadhvi</span><br /><span style="font-family:webdings;"> */</span><br /><span style="font-family:webdings;">public interface <span style="font-weight: bold;">ConfirmAlertListener</span> {</span><br /><span style="font-family:webdings;"> /**</span><br /><span style="font-family:webdings;"> * this method is called when "positive" response is given</span><br /><span style="font-family:webdings;"> * to the alert by user.</span><br /><span style="font-family:webdings;"> */</span><br /><span style="font-family:webdings;"> public void <span style="font-weight: bold; color: rgb(0, 0, 0);">yesPressed</span>();</span><br /><span style="font-family:webdings;"> /**</span><br /><span style="font-family:webdings;"> * this method is called when "negative" response is given</span><br /><span style="font-family:webdings;"> * to the alert by user.</span><br /><span style="font-family:webdings;"> */</span><br /><span style="font-family:webdings;"> public void <span style="font-weight: bold;"><span style="color: rgb(0, 0, 0);">noPressed</span>()</span>;</span><br /><span style="font-family:webdings;">}</span><br /><br /><br /><br /><span style="font-family:webdings;">package hello;</span><br /><br /><span style="font-family:webdings;">import javax.microedition.lcdui.Alert;</span><br /><span style="font-family:webdings;">import javax.microedition.lcdui.AlertType;</span><br /><span style="font-family:webdings;">import javax.microedition.lcdui.Command;</span><br /><span style="font-family:webdings;">import javax.microedition.lcdui.CommandListener;</span><br /><span style="font-family:webdings;">import javax.microedition.lcdui.Displayable;</span><br /><br /><span style="font-family:webdings;">/**</span><br /><span style="font-family:webdings;"> * Confirmation alert to get response from user</span><br /><span style="font-family:webdings;"> * in either positive or negative way.</span><br /><span style="font-family:webdings;"> * @author Sandeep Gadhvi</span><br /><span style="font-family:webdings;"> */</span><br /><span style="font-family:webdings;">public class <span style="font-weight: bold;">ConfirmationAlert</span> implements CommandListener{</span><br /><br /><span style="font-family:webdings;"> private Alert alert;</span><br /><span style="font-family:webdings;"> private Command positive;</span><br /><span style="font-family:webdings;"> private Command negative;</span><br /><span style="font-family:webdings;"> private ConfirmAlertListener listener;</span><br /><span style="font-family:webdings;"> /**</span><br /><span style="font-family:webdings;"> * constructor for Confirmation alert</span><br /><span style="font-family:webdings;"> * @param msg message to display in alert</span><br /><span style="font-family:webdings;"> * @param positive positive command in confirmation</span><br /><span style="font-family:webdings;"> * @param negative negative command in confirmation</span><br /><span style="font-family:webdings;"> */</span><br /><span style="font-family:webdings;"> public <span style="font-weight: bold;">ConfirmationAlert</span>(String msg,Command positive,Command negative){</span><br /><span style="font-family:webdings;"> this.positive=positive;</span><br /><span style="font-family:webdings;"> this.negative=negative;</span><br /><span style="font-family:webdings;"> alert=new Alert("Confirm Alert",msg,null,AlertType.WARNING);</span><br /><span style="font-family:webdings;"> alert.addCommand(positive);</span><br /><span style="font-family:webdings;"> alert.addCommand(negative);</span><br /><span style="font-family:webdings;"> alert.setTimeout(Alert.FOREVER);</span><br /><span style="font-family:webdings;"> alert.setCommandListener(this);</span><br /><span style="font-family:webdings;"> }</span><br /><span style="font-family:webdings;"> /**</span><br /><span style="font-family:webdings;"> * registers the listener for confirmation alert</span><br /><span style="font-family:webdings;"> * @param listener listener class implementing ComfirmAlertListener</span><br /><span style="font-family:webdings;"> */</span><br /><span style="font-family:webdings;"> public void <span style="font-weight: bold;">setConfirmAlertListener</span>(ConfirmAlertListener listener){</span><br /><span style="font-family:webdings;"> this.listener=listener;</span><br /><span style="font-family:webdings;"> } </span><br /><span style="font-family:webdings;"> /**</span><br /><span style="font-family:webdings;"> * get the alert reference for this ConfirmationAlert</span><br /><span style="font-family:webdings;"> *@return alert for this class</span><br /><span style="font-family:webdings;"> */</span><br /><span style="font-family:webdings;"> public Alert <span style="font-weight: bold;">getAlert()</span>{ </span><br /><span style="font-family:webdings;"> return alert;</span><br /><span style="font-family:webdings;"> }</span><br /><span style="font-family:webdings;"> /**</span><br /><span style="font-family:webdings;"> * command action for displayables</span><br /><span style="font-family:webdings;"> * @param command command object reference</span><br /><span style="font-family:webdings;"> * @param display displayable</span><br /><span style="font-family:webdings;"> */</span><br /><span style="font-family:webdings;"> public void commandAction(Command command, Displayable display) {</span><br /><span style="font-family:webdings;"> if(command==positive){</span><br /><span style="font-family:webdings;"> if(listener!=null){</span><br /><span style="font-family:webdings;"> // call yes pressed method of ConfirmAlertListener implementer class</span><br /><span style="font-family:webdings;"> listener.yesPressed();</span><br /><span style="font-family:webdings;"> } </span><br /><span style="font-family:webdings;"> }</span><br /><span style="font-family:webdings;"> if(command==negative){</span><br /><span style="font-family:webdings;"> if(listener!=null){</span><br /><span style="font-family:webdings;"> // call no pressed method of ConfirmAlertListener implementer class</span><br /><span style="font-family:webdings;"> listener.noPressed();</span><br /><span style="font-family:webdings;"> } </span><br /><span style="font-family:webdings;"> }</span><br /><span style="font-family:webdings;"> }</span><span style="font-family:webdings;"> </span><br /><span style="font-family:webdings;">}<br /></span><br /><br /><br /><span style="font-family:webdings;">package hello;<br /><br />import javax.microedition.midlet.*;<br />import javax.microedition.lcdui.*;<br /><br />/**<br />* This is test midlet class which shows how to use "ConfirmationAlert Class"<br />* and "ConfirmAlertListener Interface", Its a simple "Hello World" program<br />* but with Logic of Alert and Listener seperated from main code.Thus encapsulating<br />* the code.<br />* @author Sandeep Gadhvi.<br />*/<br />public class <span style="font-weight: bold;">HelloMIDlet </span>extends MIDlet implements CommandListener,<span style="font-weight: bold; color: rgb(255, 0, 0);">ConfirmAlertListener </span>{<br /><br /> private boolean midletPaused = false;<br /><br /> private Command exitCommand;<br /><br /> private Command yesCommand;<br /> private Command noCommand;<br /> private <span style="font-weight: bold; color: rgb(51, 102, 255);">ConfirmationAlert </span><span style="color: rgb(51, 102, 255);">confirmAlert</span>;<br /> private <span style="font-weight: bold;">Alert </span>alert;<br /><br /> private Form form;<br /> private StringItem stringItem;<br /><br /> public HelloMIDlet() {<br /> yesCommand=new Command("Yes",Command.OK,1);<br /> noCommand=new Command("No",Command.CANCEL,1);<br /> // create new ConfirmationAlert object with message to show and<br /> // positive command and negative command for corresponding response.<br /> confirmAlert=new <span style="font-weight: bold; color: rgb(51, 102, 255);">ConfirmationAlert</span>("Do You Really Want To Exit?", yesCommand, noCommand);<br /> //don't forget to registern listener as in every event driven architecture.<br /> <span style="color: rgb(51, 51, 255);"> confirmAlert.</span><span style="font-weight: bold; color: rgb(51, 51, 255);">setConfirmAlertListener</span>(<span style="color: rgb(255, 0, 0);">this</span>);<br /> // get the custom alert<br /> alert=<span style="color: rgb(51, 102, 255);">confirmAlert.</span><span style="font-weight: bold; color: rgb(51, 102, 255);">getAlert</span>();<br /> }<br /><br /> public void startMIDlet() { <br /> switchDisplayable(null, getForm()); <br /> }<br /> public void switchDisplayable(Alert alert, Displayable nextDisplayable) {<br /> <br /> Display display = getDisplay();<br /> if (alert == null) {<br /> display.setCurrent(nextDisplayable);<br /> } else {<br /> display.setCurrent(alert, nextDisplayable);<br /> }<br /> <br /> }<br /> public void commandAction(Command command, Displayable displayable) {<br /> if(displayable == form){<br /> if (command == exitCommand) {<br /> // on exit command show "Alert" to user<br /> getDisplay().setCurrent(alert);<br /> }<br /> } <br /> }<br /> /**<br /> * This will be called when user presses "Yes" command<br /> */<br /> public void <span style="font-weight: bold; color: rgb(255, 0, 0);">yesPressed</span>() {<br /> exitMIDlet();<br /> }<br /> /**<br /> * This will be called when user presses "No" command<br /> */<br /> public void <span style="font-weight: bold; color: rgb(255, 0, 0);">noPressed</span>() {<br /> getDisplay().setCurrent(getForm());<br /> }<br /> public Command getExitCommand() {<br /> if (exitCommand == null) {<br /> exitCommand = new Command("Exit", Command.EXIT, 0);<br /> }<br /> return exitCommand;<br /> }<br /> public Form getForm() {<br /> if (form == null) { <br /> form = new Form("Welcome", new Item[] { getStringItem() });<br /> form.addCommand(getExitCommand());<br /> form.setCommandListener(this); <br /> }<br /> return form;<br /> }<br /> public StringItem getStringItem() {<br /> if (stringItem == null) {<br /> stringItem = new StringItem("Hello", "Hello, World!");<br /> }<br /> return stringItem;<br /> }<br /> public Display getDisplay () {<br /> return Display.getDisplay(this);<br /> }<br /> public void exitMIDlet() {<br /> switchDisplayable (null, null);<br /> destroyApp(true);<br /> notifyDestroyed();<br /> }<br /> public void startApp() {<br /> if (midletPaused) { <br /> } else { <br /> startMIDlet ();<br /> }<br /> midletPaused = false;<br /> }<br /> public void pauseApp() {<br /> midletPaused = true;<br /> }<br /> public void destroyApp(boolean unconditional) { <br /> }<br />}<br /><br />Any Suggestions or Comments regarding this article will be appreciated.<br /><br /><br />Regards,<br />Sandeep Gadhvi.<br /></span>sandeep.gadhvihttp://www.blogger.com/profile/15389821904029065699noreply@blogger.com1tag:blogger.com,1999:blog-6268767532808851203.post-10235080587146931662009-01-10T04:06:00.000-08:002009-01-10T04:15:17.226-08:00First BlogHi All,<br /> Though this is my first blog ,degree of enthusiasm<span style="font-size:130%;"><span style="font-weight: bold; font-style: italic;font-family:verdana;" ></span></span> is not so much higher but still I have much much to write in the future.<br /> For those first time visiting my blog I am sandeep gadhvi , from ahmedabad (basically from gandhidham,kutch) doing job at one middle scale IT company at ahmedabad.<br /> For those who are familiar with me you know i don't have anything to write that's why i am filling up this empty space.<br /> So......... Enough is Enough .......This is me you like or not.<br /> <br /> We'll meet in future with lots of text pouring out of this blog and your brain pouring out of your head......ha ha ha...<br /> <br /> Byee for now "<span style="font-weight: bold; color: rgb(51, 102, 255);font-family:courier new;font-size:100%;" >Hasta La Vista</span>".......sandeep.gadhvihttp://www.blogger.com/profile/15389821904029065699noreply@blogger.com2