Skip to content

Instantly share code, notes, and snippets.

View techindepth's full-sized avatar
👋

ANISH ANTONY techindepth

👋
View GitHub Profile
public class AnishCustomer implements Observer{
@Override
public void update(String status) {
if(status.equals("Available"))
System.out.println("Anish: Yeah!!!! Going to buy!!!");
}
}
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");
}
public interface Iterator {
public boolean hasNext();
public Object next();
}
public interface Container {
public Iterator getIterator();
}
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();
}
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());
}
}
}
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();
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);
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);
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);
}
}