常见模式例子

 

工厂模式

package login.sj;
class Factory{
	public static Object getObject(Class<?> class1){
		try {
			Object object = class1.newInstance();
			return object;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
abstract class AbsFactory{
	abstract Object create(Class<?> class1);
}
public class FactoryTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Book book = (Book) Factory.getObject(Book.class);
		book.setName("xxxxx");
	}

}
 

单例模式

 

package login.sj;

class Singleton {
	private String name;
	
	public String getName() {
		return name;
	}

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

	private Singleton() {
	}

	// 在自己内部定义自己一个实例,是不是很奇怪?
	// 注意这是private 只供内部调用

	private static Singleton instance = new Singleton();

	// 这里提供了一个供外部访问本class的静态方法,可以直接访问  
	public static Singleton getInstance() {
		return instance;
	}	
	/*
	 * private static Singleton instance = null;
	 * 
	 *   public static synchronized Singleton getInstance() {
	 * 
	 *   //这个方法比上面有所改进,不用每次都进行生成对象,只是第一次        //使用时生成实例,提高了效率!   if
	 * (instance==null)     instance=new Singleton();   return instance;   }
	 */

}

public class SingletonTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Singleton singleton = Singleton.getInstance();
		singleton.setName("test1");
		System.out.println(singleton.getName());
		
		Singleton singleton2 = Singleton.getInstance();
		singleton2.setName("test2");
		System.out.println(singleton.getName());

	}

}
 

代理模式
package login.sj;

import java.lang.reflect.InvocationTargetException;


class People{
	void say(){
		System.out.println("say");
	}
}
abstract class MyProxy{

	/**
	 * @param args
	 */
	private Class<?> class1;
	private String methodName;
	
	public Class<?> getClass1() {
		return class1;
	}
	public void setClass1(Class<?> class1) {
		this.class1 = class1;
	}
	public String getMethodName() {
		return methodName;
	}
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	public MyProxy(Class<?> class1,String methodName) {
		// TODO Auto-generated constructor stub
		this.class1 = class1;
		this.methodName = methodName;
	}
	public void invoke(){

		try {
			Object object = class1.newInstance();
			object.getClass().getDeclaredMethod(methodName).invoke(object);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public abstract void after();
	public abstract void before();
}
public class ProxyTest extends MyProxy{
	public ProxyTest(Class<?> class1, String methodName) {
		super(class1, methodName);
		// TODO Auto-generated constructor stub
	}


	@Override
	public void after() {
		// TODO Auto-generated method stub
		System.out.println("方法已经被执行");
	}

	@Override
	public void before() {
		// TODO Auto-generated method stub
		System.out.println("方法执行前");
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ProxyTest proxyTest = new ProxyTest(People.class, "say");
		proxyTest.before();
		proxyTest.invoke();
	}
}
观察者模式
package login.sj;

import java.util.Observable;
import java.util.Observer;
class Book extends Observable{
	private String name;
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
		setChanged();
		notifyObservers(name);
	}
}
class ObserverT implements Observer{

	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		System.out.println(o+"----------"+arg);
		if(o instanceof Book){
			System.out.println("book name set :"+arg);
		}
	}
	
}
public class ObserverTest extends Observable{

	/**
	 * @param args
	 */
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Book book = new Book();
		ObserverT observerT = new ObserverT();
		book.addObserver(observerT);
		book.setName("find");
	}

}
 

 

 

你可能感兴趣的:(例子)