Java Design Pattern


[Adapter pattern]

public interface Print
{
 public abstract void printWeak();
 public abstract void printString();
 
}

public class Banner
{

 private String string;

 public Banner(String string)
 {

  this.string = string;
 }

 public void showWithParen()
 {

  System.out.println("(" + string + ")");
 }
 
 public void showWithAster()
 {
  System.out.println("*" + string +"*");
 }
}

public class PrintBanner extends Banner implements Print
{

 public PrintBanner(String string)
 {

  super(string);
  // TODO Auto-generated constructor stub
 }

 @Override
 public void printWeak()
 {

  // TODO Auto-generated method stub
  showWithParen();
  
 }

 @Override
 public void printString()
 {

  // TODO Auto-generated method stub
  showWithAster();
  
 }

}

public class Main
{

 /**
  * @param args
  */
 public static void main(String[] args)
 {

  // TODO Auto-generated method stub
  Print p = new PrintBanner("Hello");
  p.printWeak();
  p.printString();

 }

}



[Iterator pattern]

public interface Aggregate
{
 public abstract Iterator iterator(); 
}

public interface Iterator
{
 public abstract boolean hasNext();
 public abstract Object next();
}

public class BookShelf implements Aggregate
{
 private Book[] books;
 private int last = 0;
 
 public BookShelf(int maxsize)
 {
  this.books = new Book[maxsize];
 }
 
 public Book getBookAt(int index)
 {
  return books[index];
 }
 
 public void appendBook(Book book)
 {
  this.books[last] = book;
  last++;
 }
 
 public int getLength()
 {
  return last;
 }

 @Override
 public Iterator iterator()
 {

  // TODO Auto-generated method stub
  return new BookShelfIterator(this);
 }

}

public class BookShelfIterator implements Iterator
{
 private BookShelf bookShelf;
 private int index;
 
 public BookShelfIterator(BookShelf bookShelf)
 {
  this.bookShelf = bookShelf;
  this.index = 0;
 }
 @Override
 public boolean hasNext()
 {

  // TODO Auto-generated method stub
  if(index < bookShelf.getLength())
   return true;
  else
   return false;
 }

 @Override
 public Object next()
 {

  // TODO Auto-generated method stub
  Book book = bookShelf.getBookAt(index);
  index++;
  return book;
 }

}
public class Book
{
 
 private String name = "";

 public Book(String name)
 {
  this.name = name;
 }
 public String getName()
 {
 
  return name;
 }

 
 public void setName(String name)
 {
 
  this.name = name;
 }
 
 
}

public class Main
{

 /**
  * @param args
  */
 public static void main(String[] args)
 {

  BookShelf bookShelf = new BookShelf(4);
  bookShelf.appendBook(new Book("Bible"));
  bookShelf.appendBook(new Book("Cinderella"));
  bookShelf.appendBook(new Book("Daddy"));
  
  Iterator it = bookShelf.iterator();
  while(it.hasNext())
  {
   Book book = (Book) it.next();
   System.out.println(""+book.getName());
  }

 }

}


[Template pattern]
public abstract class AbstractDisplay
{
 public abstract void open();
 public abstract void print();
 public abstract void close();
 
 public final void display()
 {
  open();
  
  for(int i=0; i<5; i++)
  {
   print();
  }
  
  close();
 }
}
public class CharDisplay extends AbstractDisplay
{
 private char ch;
 
 public CharDisplay(char ch)
 {
  this.ch = ch;
 }
 @Override
 public void open()
 {

  // TODO Auto-generated method stub
  System.out.print("<<");
  
 }

 @Override
 public void print()
 {

  // TODO Auto-generated method stub
  System.out.print(ch);
 }

 @Override
 public void close()
 {

  // TODO Auto-generated method stub
  System.out.println(">>");
 }

}
public class StringDisplay extends AbstractDisplay
{
 private String string;
 private int width;
 
 public StringDisplay(String string)
 {
  this.string = string;
  this.width = string.getBytes().length;
 }

 @Override
 public void open()
 {

  // TODO Auto-generated method stub
  printLine();
 }

 
 @Override
 public void print()
 {

  // TODO Auto-generated method stub
  System.out.println("|"+string+"|");
 }

 @Override
 public void close()
 {

  // TODO Auto-generated method stub
  printLine();
 }
 
 private void printLine()
 {

  // TODO Auto-generated method stub
  System.out.print("+");
  
        }

public class Main
{

 /**
  * @param args
  */
 public static void main(String[] args)
 {

  // TODO Auto-generated method stub
  AbstractDisplay d1 = new CharDisplay('H');
  
  AbstractDisplay d2 = new StringDisplay("Hello, world.");
  
  AbstractDisplay d3 = new StringDisplay("nice to you.");
  
  d1.display();
  d2.display();
  d3.display();
 }

}


[Factory Method Pattern]
Factory class
public abstract class Factory { public final Product create(String owner) { Product p = createProduct(owner); registerProduct(p); return p; } protected abstract Product createProduct(String owner); protected abstract void registerProduct(Product product); } Product class
public abstract class Product { public abstract void use(); } IDCardFactory class
public class IDCardFactory extends Factory { private Vector owners = new Vector(); @Override protected Product createProduct(String owner) { // TODO Auto-generated method stub return new IDCard(owner); } @Override protected void registerProduct(Product product) { // TODO Auto-generated method stub owners.add(((IDCard)product).getOwner()); } } IDCard class
public class IDCard extends Product { private String owner; IDCard(String owner) { System.out.println("create"+owner+"card"); this.owner = owner; } @Override public void use() { System.out.println("use"+owner+"card"); } public String getOwner() { return owner; } } Main class
public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Factory factory = new IDCardFactory(); Product card1 = factory.createProduct("test"); Product card2 = factory.createProduct("test1"); Product card3 = factory.createProduct("test2"); card1.use(); card2.use(); card3.use(); } }

留言

這個網誌中的熱門文章

Json概述以及python對json的相關操作

遠程控制管理工具ipmitool

從Kubernetes到Cloud Native——雲原生應用之路