Java复习之知识点整理(十四)----设计模式之单例模式,适配器模式,装饰模式,工厂模式,构建器模式

一、单例模式
------------------------------------------------
1.类有且只有一个对象

public class Ts01 {

	public static void main(String[] args) {
		tsSingle1();
	}
	
	@Test
	public void tsSingle()
	{
		GarbageBox gar1 = GarbageBox.getInstance();
		System.out.println(gar1.hashCode());
		GarbageBox gar2 = GarbageBox.getInstance();
		System.out.println(gar2.hashCode());
	}
	
	public static void tsSingle1()
	{
		new Thread()
		{
			public void run()
			{							
				GarbageBox gar1 = GarbageBox.getInstance();
				System.out.println(gar1);			
			}
		}.start();

		new Thread()
		{
			public void run(){				
				GarbageBox gar1 = GarbageBox.getInstance();
				System.out.println(gar1);			
			}
			
		}.start();		
	}
	
	
}

/**
 * 单例模式
 * @author Administrator
 *
 */
class GarbageBox{
	
	private static GarbageBox ins;
	private static final Object lock = new Object();

	
	/**
	 * 私有构造
	 */
	private GarbageBox()
	{
		
	}
	
	public static GarbageBox getInstance()
	{		
		if(ins == null)
		{			
			synchronized(GarbageBox.class){	
				if(ins == null)
				{						
					ins = new GarbageBox();
				}
			}			
		}
		return ins;							
	}	
}

class GarbageBox1{
	
	private static GarbageBox1 ins = new GarbageBox1();

	
	/**
	 * 私有构造
	 */
	private GarbageBox1()
	{
		
	}
	
	public static GarbageBox1 getInstance()
	{		
//		synchronized(ins)
//		{				
			return ins;	
//		}							
	}	
}



二、适配器模式
---------------------------------------
场景: A类不想继承父类和实现接口,但是又想具有接口BB的功能bb();
1.定义一个BB接口的预实现类Adapter,适配器类
2.在Adapter类中,重写接口的所有方法,但是方法体全是空的
3.在A类中,创建bb(Adapter adapter)方法,参数就是适配器类,方法内部调用adapter.bb()方法;
4.这样在使用A类对象的时候,A类对象可以调用bb()方法,传入一个适配器就行了(注意因为适配器方法是空的,这个时候需要重写方法)

public class Ts02 {

	public static void main(String[] args) {
		Window w = new Window();
		w.setWindowListener(new WindowAdapter()
		{
			public void onMax() {
				
				System.out.println("hello");
				
			}
		}
				
		);
	}
	
}


//预实现
class WindowAdapter implements WindowListener{
	
	public void onMin() {
	}

	public void onMax() {
			
	}

	public void onClose() {
	}

	public void onResize() {
	}
	
}

/**
 * 窗口类 
 */
class Window {
	
	private int size;
	private int location;

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getLocation() {
		return location;
	}

	public void setLocation(int location) {
		this.location = location;
	}
	
	public void setWindowListener(WindowListener l){
		l.onMax();
	}
}


interface WindowListener {
	public void onMin();
	public void onMax();
	public void onClose();
	public void onResize();
}




三、装饰模式
--------------------------------------
class A{
public void aa(){
...
}
}
class B extends A{
private A a;
public B(A a){
this.a = a;
}
public void aa(){
System.out.println("我是为了丰富A.aa()方法的");
...
a.aa();
...
System.out.println("我叫锦上添花");
}
}




四、工厂模式
--------------------------------------
1.频繁的创建主板为三星,名称为XXX,价格为5000的电视机的时候,使用工厂模式
2.创建一个工厂类,静态方法生产电视机,传入参数
3.这样就避免了每次创建对象都要进行名称,主板和价格的赋值操作
4.工厂生产的东西都是一样的,一定要注意!
5.大大的增加了代码的可维护性

public class Ts04 {

	public static void main(String[] args) {
		TVSet tv = TVSetFactory.productTV();
		System.out.println(tv.getName());
	}	
}

class TVSetFactory {
	
	public static TVSet productTV(){
		
		TVSet tv = new TVSet();
		tv.setBrand("三星");
		tv.setName("xxx");
		tv.setPrice(5000f);
		return tv;
		
	}
}


class TVSet {

	private String name;
	private String brand;
	private float price;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}	
	
}




五、构建器模式Builder
----------------------------------------
1.set方法返回自身
2.可以通过构造链进行创建对象


public class Ts05 {

	public static void main(String[] args) {
		
		Phone p = new Phone()
					.setName("a")
					.setBrand("b")
					.setProductArea("c")
					.setPrice(500f);
//		p.setName("a");
//		p.setProductArea("b");		
//		p.setBrand("c");
	}
	
}



class Phone{
	
	private String name;
	private String brand;
	private String productArea;
	private float price;
	
	public String getName() {
		
		return name;
	}
	public Phone setName(String name) {
		this.name = name;
		return this;
	}
	public String getBrand() {
		return brand;
	}
	public Phone setBrand(String brand) {
		this.brand = brand;
		return this;
	}
	public String getProductArea() {
		return productArea;
	}
	public Phone setProductArea(String productArea) {
		this.productArea = productArea;
		return this;
	}
	public float getPrice() {
		return price;
	}
	public Phone setPrice(float price) {
		this.price = price;
		return this;
	}	
}



猜你喜欢

转载自blog.csdn.net/xcvbxv01/article/details/80921178