This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AnishCustomer implements Observer{ | |
| @Override | |
| public void update(String status) { | |
| if(status.equals("Available")) | |
| System.out.println("Anish: Yeah!!!! Going to buy!!!"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ObserverPatternTest { | |
| public static void main(String[] args) { | |
| Observer akhil = new AkhilCustomer(); | |
| Observer anish = new AnishCustomer(); | |
| FlipkartMINote5Pro flipkart = new FlipkartMINote5Pro(); | |
| flipkart.setProductStatus("Out of Stock"); | |
| flipkart.registerObserver(akhil); | |
| flipkart.registerObserver(anish); | |
| flipkart.setProductStatus("Available"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface Iterator { | |
| public boolean hasNext(); | |
| public Object next(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface Container { | |
| public Iterator getIterator(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class IPODMini implements Container{ | |
| public String tracks[] = {"Chaar Bottle Vodka" , "Badtameez Dil" ,"Kal Ho Naa Ho" , "Chaiyya Chaiyya"}; | |
| @Override | |
| public Iterator getIterator() { | |
| return new TrackIterator(); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class IteratorPatternTest { | |
| public static void main(String[] args) { | |
| IPODMini myIPOD = new IPODMini(); | |
| Iterator trackIterator = myIPOD.getIterator(); | |
| while(trackIterator.hasNext()) { | |
| System.out.println("Playing ... "+ (String)trackIterator.next()); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class ChessPiece implements Cloneable { | |
| protected String currentPosition; | |
| protected String pieceType; | |
| abstract void moveTo(String position); | |
| public Object clone() | |
| { | |
| Object clone = null; | |
| try | |
| { | |
| clone = super.clone(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Bishop extends ChessPiece{ | |
| public Bishop() { | |
| this.pieceType="Bishop"; | |
| } | |
| @Override | |
| void moveTo(String position) { | |
| this.currentPosition = position; | |
| System.out.println(pieceType+" Piece Moved to - "+ position); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class King extends ChessPiece{ | |
| public King() { | |
| this.pieceType="King"; | |
| } | |
| @Override | |
| void moveTo(String position) { | |
| this.currentPosition= position; | |
| System.out.println(pieceType+" Piece Moved to - "+ position); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Pawn extends ChessPiece{ | |
| public Pawn() { | |
| this.pieceType="Pawn"; | |
| } | |
| @Override | |
| void moveTo(String position) { | |
| this.currentPosition = position; | |
| System.out.println(pieceType+" Piece Moved to - "+ position); | |
| } | |
| } |