饿汉式----单例模式
public class Eager
{
  private static final Eager eager=new Eager();
  private Eager(){}
  public static Eager getInstance()
  {
    return eager;
  }
}

懒汉式----单例模式
public class Lazy
{
 private static Lazy lazy=null;
 private Lazy(){}
 public static Lazy getInstance()
 {
  if(lazy==null)
    lazy=new Lazy();
  return lazy;
 }
}

简单工厂模式
public interface Fruit
{
 public abstract void wash();
 public abstract void eat();
}
public class apple implement Fruit
{
 public void wash(){System.out.println("wash apple!");}
 public void eat(){System.out.println("eat apple");}
}
public class orange implement Fruit
{
 public void wash(){System.out.println("wash orange!");}
 public void eat(){System.out.println("eat orange");}
}
public class Factory
{
 public Fruit operater(string fruit)
 {
   switch(fruit)
    {
     case "apple":return new apple(); break; 
     case "orange":return new apple();break;
     default:break;
    }
 }
}
public class Client
{
 public static void main(String[] args)
 {
   Factory factory=new Factory();
   Fruit fruit=factory.operater("apple");
   fruit.wash();
   fruit.eat();
 }
}
工厂方法模式
interface Factory
{
 public abstract Fruit getFruit();
}
interface Fruit
{
 public abstract void wash();
 public abstract void eat();
}
public class Apple implements Fruit
{
 public abstarct void wash()
 {
   System.out.println("wash apple!");
 }
 public abstract void eat()
 {
  System.out.println("eat apple!");
 }
}
public class Orange implements Fruit
{
 public void wash()
 {
  System.out.println("wash orange!");
 }
 public void eat()
 {
  System.out.println("eat orange!");
 }
}
public calss AppleFactory implements Factory
{
 public Fruit getFruit();
 {
   return new Apple();
 }
}
public class OrangeFactory implements Factory
{
 public Fruit getFruit();
 {
  return new Orange();
 }
}
public class Client
{
 public static void main(String[] args)
 {
  Factory factory=new AppleFactory();
  Fruit fruit=factory.getFruit();
  fruit.wash();
  fruit.eat();
 }
}
抽象工厂模式
interface Tree
{
 public abstract void plant();
 public abstract void cut();
}
public class BigTree implements Tree
{
 public void plant()
 {
  System.out.println("plant bigtree!");
 }
 public void cut()
 {
  System.out.println("cut bigtree!");
 }
}
public class SmallTree implements Tree
{
 public void plant()
 {
  System.out.println("plant SmallTree!");
 }
 public void cut()
 {
  System.out.println("cut SmallTree!");
 }
}
interface Fruit
{
 public abstract void wash();
 public abstract void eat();
}
public class Apple implements Fruit
{
 public abstract void wash()
 {
  System.out.println("wash apple!");
 }
 public abstract void eat()
 {
  System.out.println("eat apple!");
 }
}
public class Orange implements Fruit
{
 public abstract void wash()
 {
  System.out.println("wash orange!");
 }
 public abstract void eat()
 {
  System.out.println("eat orange!");
 }
}
interface Factory
{
 public abstract Tree getTree();
 public abstract Fruit getFruit();
}
public class Factory1 implements Factory
{
 public Tree getTree()
 {
  new BigTree();
 }
 public Fruit getFruit()
 {
  new Apple();
 }
}
public class Factory2 implements Factory
{
  public Tree getTree()
  {
    new SmallTree();
  }
  public Fruit getFruit()
  {
    new Orange();
  }
}
public class Client
{
  public static void main(String[] args)
  {
   Factory factory=new Factory1();
   Tree tree=factory.getTree();
   Fruit fruit=factory.getFruit();
   tree.plant();
   tree.cut();
   fruit.wash();
   fruit.eat();
  }
}
建造者模式
将内部表象与构造过程分开,已建造汽车为例
public class Car
{
  public string cardoor;
  public string carwheel;
  public string engine;
  public void go()
  {
   System.out.println("go over!");
  }
}
interface Creator
{
 public abstract void AddDoor();
 public abstract void AddWheel();
 public abstract void AddEngine();
 public abstract Car getInstance();
}
public class CreateCar implements Creator
{
 public Car car;
 public CreateCar(Car car)
 {
  this.car=car;
 }
 public void AddDoor()
 {
   car.cardoor="door";
 }
 public void AddWheel()
 {
   car.carwheel="wheel";
 }
 public void AddEngine()
 {
   car.engine="engine";
 }
 public Car getInstance()
 {
  return car;
 }
}
public class Director
{
  public Creator create=new CreateCar();
  public Car sendOrder()
  {
    return create.getInstance();
  } 
}
public class Client
{
 public static void main(String[] args)
 {
  Director director=new Director();
  Car car=director.SendOrder();
  car.go();
 }
}
原型模式
允许一个对象复制一个定制的对象
interface Protery extends Cloneable
{
 public abstract Protery clone();
}
public class Pro extends protery
{
 public Object clone()
 {
   try
    {
      return super.clone();
    }
    catch(CloneNotSupportedException e)
    {
      return null;
    }
 }
}
public class Client
{
 public Protery operator(Protery example)
 {
  return (Protery)example.clone();
 }
}
public class Test
{
 public static void main(String[] args)
 {
   Client client=new Client();
   Protery protery=new Pro();
   client.operator(protery);
 }
}

posted on 2011-01-12 10:25  魔战  阅读(208)  评论(0编辑  收藏  举报