PUVARASAN A L GANESAN

All generations.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The DvdName class is a simple class that captures a title name for a DVD.

code to explanationWed, 28 Dec 2022

class TestCommand { public static void main(String[] args) { DvdName jayAndBob = new DvdName("Jay and Silent Bob Strike Back"); DvdName spongeBob = new DvdName("Sponge Bob Squarepants - " + "Nautical Nonsense and Sponge Buddies"); System.out.println("as first instantiated"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); CommandAbstract bobStarsOn = new DvdCommandNameStarsOn(jayAndBob); CommandAbstract bobStarsOff = new DvdCommandNameStarsOff(jayAndBob); CommandAbstract spongeStarsOn = new DvdCommandNameStarsOn(spongeBob); CommandAbstract spongeStarsOff = new DvdCommandNameStarsOff(spongeBob); bobStarsOn.execute(); spongeStarsOn.execute(); System.out.println(" "); System.out.println("stars on"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); spongeStarsOff.execute(); System.out.println(" "); System.out.println("sponge stars off"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); } }

The above code is an example of the Command design pattern.

code to explanationWed, 28 Dec 2022

class TestCommand { public static void main(String[] args) { DvdName jayAndBob = new DvdName("Jay and Silent Bob Strike Back"); DvdName spongeBob = new DvdName("Sponge Bob Squarepants - " + "Nautical Nonsense and Sponge Buddies"); System.out.println("as first instantiated"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); CommandAbstract bobStarsOn = new DvdCommandNameStarsOn(jayAndBob); CommandAbstract bobStarsOff = new DvdCommandNameStarsOff(jayAndBob); CommandAbstract spongeStarsOn = new DvdCommandNameStarsOn(spongeBob); CommandAbstract spongeStarsOff = new DvdCommandNameStarsOff(spongeBob); bobStarsOn.execute(); spongeStarsOn.execute(); System.out.println(" "); System.out.println("stars on"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); spongeStarsOff.execute(); System.out.println(" "); System.out.println("sponge stars off"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); } }

The class TestCommand is the main class to run the code. The main method calls the toString method of JayAndBob and SpongeBob objects created. The execute method of bobStarsOn and spongeStarsOn is called. The execute method of bobStarsOff is called.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The class DvdName represents a movie title. The field titleName is the name of the movie. The class has two constructors, one that takes a String argument and one that does not. The relevant class constructors are as follows: public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); }

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The above class is a simple DvdName class. It has a titleName property, which is set in the constructor, and the setter and getter methods. In addition, it has a setNameStarsOn method, which replaces all spaces in the titleName with a '*' character, and a method which reverses that. Finally, it has a toString method which returns the object as a String.

code to explanationWed, 28 Dec 2022

class TestTitleVisitor { public static void main(String[] args) { AbstractTitleInfo bladeRunner = new DvdInfo("Blade Runner", "Harrison Ford", '1'); AbstractTitleInfo electricSheep = new BookInfo("Do Androids Dream of Electric Sheep?", "Phillip K. Dick"); AbstractTitleInfo sheepRaider = new GameInfo("Sheep Raider"); TitleBlurbVisitor titleLongBlurbVisitor = new TitleLongBlurbVisitor(); } }

The class TestTitleVisitor is the main class where we demonstrate the use of the visitor pattern. Since the code is in Java, the main method is where the execution of the program starts. In the main method we create a new DvdInfo object, bladeRunner, and instantiate it with the name, lead actor and the number of the DVD. Then we create a new BookInfo object, electricSheep, and instantiate it with the name and author of the book. Finally we create a new GameInfo object, sheepRaider, and instantiate it with the name of the game. After we create the three objects, we create a new TitleLongBlurbVisitor object, titleLongBlurbVisitor. And finally we call the method accept on each object, passing the titleLongBlurbVisitor object as an argument. Calling the accept method on an object calls the accept method of the corresponding class in the visitor hierarchy.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The code is a class called DvdName. It has two states titleName, which is a string. It has three behaviours setTitleName, getTitleName, setNameStarsOn.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The class has a single property, titleName, which is the name of the DVD.

code to explanationWed, 28 Dec 2022

class TestTitleVisitor { public static void main(String[] args) { AbstractTitleInfo bladeRunner = new DvdInfo("Blade Runner", "Harrison Ford", '1'); AbstractTitleInfo electricSheep = new BookInfo("Do Androids Dream of Electric Sheep?", "Phillip K. Dick"); AbstractTitleInfo sheepRaider = new GameInfo("Sheep Raider"); TitleBlurbVisitor titleLongBlurbVisitor = new TitleLongBlurbVisitor(); } }

The main method in test class TestTitleVisitor contains an instance of all the concrete classes. The method also contains an instance of TitleBlurbVisitor.

code to explanationWed, 28 Dec 2022

public class DvdCommandNameStarsOff extends CommandAbstract { private DvdName dvdName; public DvdCommandNameStarsOff(DvdName dvdNameIn) { this.dvdName = dvdNameIn; } public void execute() { this.dvdName.setNameStarsOff(); } }

The class DvdCommandNameStarsOff extends the class CommandAbstract. The constructor DvdCommandNameStarsOff takes the argument dvdNameIn of type DvdName and assigns that to the field dvdName. The function execute calls the function setNameStarsOff on the object referenced by dvdName.

code to explanationWed, 28 Dec 2022

class TestDvdObserver { public static void main(String[] args) { DvdReleaseByCategory btvs = new DvdReleaseByCategory("Buffy the Vampire Slayer"); DvdReleaseByCategory simpsons = new DvdReleaseByCategory("The Simpsons"); DvdReleaseByCategory sopranos = new DvdReleaseByCategory("The Sopranos"); DvdReleaseByCategory xfiles = new DvdReleaseByCategory("The X-Files"); DvdSubscriber jsopra = new DvdSubscriber("Junior Soprano"); DvdSubscriber msimps = new DvdSubscriber("Maggie Simpson"); DvdSubscriber rgiles = new DvdSubscriber("Rupert Giles"); DvdSubscriber smulde = new DvdSubscriber("Samantha Mulder"); DvdSubscriber wrosen = new DvdSubscriber("Willow Rosenberg"); btvs.addSubscriber(rgiles); btvs.addSubscriber(wrosen); simpsons.addSubscriber(msimps); sopranos.addSubscriber(jsopra); xfiles.addSubscriber(smulde); xfiles.addSubscriber(wrosen); DvdRelease btvsS2 = new DvdRelease("DVDFOXBTVSS20", "Buffy The Vampire Slayer Season 2", 2002, 06, 11); DvdRelease simpS2 = new DvdRelease("DVDFOXSIMPSO2", "The Simpsons Season 2", 2002, 07, 9); DvdRelease soprS2 = new DvdRelease("DVDHBOSOPRAS2", "The Sopranos Season 2", 2001, 11, 6); DvdRelease xfilS5 = new DvdRelease("DVDFOXXFILES5", "The X-Files Season 5", 2002, 04, 1); btvs.newDvdRelease(btvsS2); simpsons.newDvdRelease(simpS2); sopranos.newDvdRelease(soprS2); xfiles.newDvdRelease(xfilS5); xfiles.removeSubscriber(wrosen); xfilS5.updateDvdReleaseDate(2002, 5, 14); xfiles.updateDvd(xfilS5); } }

The above code was from http://www.higherpass.com/javapatterns/JavaDesignPatternsObserver/java-observer.php. This code shows the observer pattern in java. The DvdReleaseByCategory is the subject and DvdSubscriber is the observer. In this example it is used to keep track of new dvd releases. When someone creates a new DvdReleaseByCategory object they are basically creating a dvd category. For example, if one were to create the object, btvs, they would be creating a tv category of Buffy the Vampire Slayer. Now if someone wanted to become a subscriber to that category they would need to use the addSubscriber method. The addSubscriber method takes an object of type DvdSubscriber. Once the subscriber has been added, whenever there is a new DvdRelease, that subscriber will be notified.

code to explanationWed, 28 Dec 2022

public class DvdCommandNameStarsOn extends CommandAbstract { private DvdName dvdName; public DvdCommandNameStarsOn(DvdName dvdNameIn) { this.dvdName = dvdNameIn; } public void execute() { this.dvdName.setNameStarsOn(); } }

The DvdCommand class is a command for the DvdName class. The DvdName class implements the DvdNameInterface class. The DvdCommandNameStarsOn class is a subclass of the CommandAbstract class. The execute method calls the setNameStarsOn method.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The above class is the DvdName class. This class shows the title of the DVD stored in a String. There are setter and getter methods to manipulate the title name. There are also methods that replace spaces with stars, and vice-versa.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The DvdName class represents a name of a DVD title. A DvdName object is composed of a single String titleName. The DvdName class defines a constructor that takes a String titleNameIn. The DvdName class implements setTitleName to validate the titleNameIn. The DvdName class implements getTitleName which returns the titleName. The DvdName class implements setNameStarsOn which replaces all spaces in titleName with *. The DvdName class implements setNameStarsOff which replaces all * in titleName with spaces. The DvdName class implements toString which returns the DVD name.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The class DvdName has two public methods getTitleName() and setTitleName(). The method getTitleName returns the data for the DVD title. The method setTitleName sets the DVD title. The title is stored in a private final String called titleName. The constructor DvdName() is used to set the initial value of the titleName. The first method setNameStarsOn() replaces all the spaces in the titleName with an asterisk (*). The second method setNameStarsOff() replaces all the asterisks in the titleName with a space. The toString() method is used to display the DVD title.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

This class encapsulates the title name, and has methods to extract the title name, replace spaces with asterisks, and replace asterisks with spaces.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The DvdName class is a simple class that contains a titleName property. The class has standard accessors for the property and a couple of methods for manipulating the name, e.g. setting the name all to uppercase, or replacing spaces with asterisks.

code to explanationWed, 28 Dec 2022

class TestCommand { public static void main(String[] args) { DvdName jayAndBob = new DvdName("Jay and Silent Bob Strike Back"); DvdName spongeBob = new DvdName("Sponge Bob Squarepants - " + "Nautical Nonsense and Sponge Buddies"); System.out.println("as first instantiated"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); CommandAbstract bobStarsOn = new DvdCommandNameStarsOn(jayAndBob); CommandAbstract bobStarsOff = new DvdCommandNameStarsOff(jayAndBob); CommandAbstract spongeStarsOn = new DvdCommandNameStarsOn(spongeBob); CommandAbstract spongeStarsOff = new DvdCommandNameStarsOff(spongeBob); bobStarsOn.execute(); spongeStarsOn.execute(); System.out.println(" "); System.out.println("stars on"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); spongeStarsOff.execute(); System.out.println(" "); System.out.println("sponge stars off"); System.out.println(jayAndBob.toString()); System.out.println(spongeBob.toString()); } }

This example includes a number of classes to illustrate the command pattern. The DvdName object has the behavior of displaying its name in the requested format. The DvdCommandNameStarsOn and DvdCommandNameStarsOff objects have the behavior of calling the starsOn and starsOff methods on the DvdName object they were instantiated with. The CommandAbstract is the abstract class with the execute method. The execute method is called on the concrete command objects.

code to explanationWed, 28 Dec 2022

public class DvdName { private String titleName; public DvdName(String titleName) { this.setTitleName(titleName); } public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } public void setNameStarsOn() { this.setTitleName(this.getTitleName().replace(' ','*')); } public void setNameStarsOff() { this.setTitleName(this.getTitleName().replace('*',' ')); } public String toString() { return ("DVD: " + this.getTitleName()); } }

The class DvdName uses a property pattern. The DvdName class has a getter (getTitleName()) and a setter (setTitleName()) for the titleName property. The setter method sets the titleName property. The setter method also allows for validation. The property titleName is a private field. Using a property pattern is a good way to create a class.

Questions about programming?Chat with your personal AI assistant