Java設計模式——單例,裝飾者,觀察者

單例模式

基於static:只允許生成一個對象

package pattern.singleton;

class Singletone {//餓漢模式
	private static Singletone s = new Singletone();//必須在這裏實例化

	private Singletone() {
	}

	public static Singletone getSingleton() {
		return s;
	}
}

class Singletonb {//飽漢模式
	private String name;
	private static Singletonb s;

	private Singletonb(String name) {
		this.name = name;
	}

	public static Singletonb getSingleton(String name) {
		if (s == null) {//效率,只要判斷一次
			synchronized ("lock") {//加鎖,線程安全
				if (s == null) {
					s = new Singletonb(name);
				}
			}
		}
		return s;
	}
}
class Singleton{
	public static void main(String[] args) {
		String name="hah";
		Singletone se1=Singletone.getSingleton();
		Singletone se2=Singletone.getSingleton();
		Singletonb sb1=Singletonb.getSingleton(name);
		Singletonb sb2=Singletonb.getSingleton(name);
		System.out.println(se1==se2);
		System.out.println(sb1==sb2);
	}
}

基於多態的模式:

 

裝飾者模式

package pattern.decorate;

class Work{
//	String can;
//	public Work(String can){
//		this.can=can;
//	}
	public void Do(){
		
	};
}
class Paint extends Work{
    Work work;
	
    public Paint(Work work) {
//		super(work.can);
    	this.work = work;
	}

	@Override
	public void Do() {
		work.Do();
		System.out.println("我能畫畫");
//		this.work.can=work.can+"我能畫畫";// TODO Auto-generated method stub
		
	}
	
}
class Mood extends Work{
	Work work;
	
	public Mood(Work work) {
//		super(work.can);
		this.work = work;
	}

	@Override
	public void Do() {
		work.Do();
		System.out.println("我會做木工");
//		this.work.can=work.can+"我會做木工";// TODO Auto-generated method stub
		
	}
	
}
class Paste extends Work{
	Work work;
	
	public Paste(Work work) {
//		super(work.can);
		this.work = work;
	}

	@Override
	public void Do() {
		work.Do();
		System.out.println("我能貼畫");
//		this.work.can=work.can+"我能貼畫";
		
	}
	
}
public class Decorate {
	public static void main(String[] args) {
		Paste father=new Paste(new Mood(new Paint(new Work())));
		father.Do();
//		System.out.println(father.can);
	}
}

觀察者模式

 

 

參考:https://www.cnblogs.com/luohanguo/p/7825656.html
 
package pattern.observe;

public class Observe {

	public static void main(String[] args) {
		WeatherStation ws=new WeatherStation();
		Student s1=new Student();
		Student s2=new Student();
		ws.add(s1);
		ws.add(s2);
		ws.startt();
	}

}

 


 

 

package pattern.observe;

public interface Personaction {
	public void notifyWeather(String weather);
}
 
package pattern.observe;

public class Student implements Personaction{

	@Override
	public void notifyWeather(String weather) {
		switch(weather){
		case "晴":System.out.println("qin");break;
		case "雨":System.out.println("yu");break;
		case "霧霾":System.out.println("wumai");break;
		case "雪":System.out.println("xue");break;
		case "多雲":System.out.println("duoyun");break;
		}
		
	}

}
 
 
package pattern.observe;

import java.util.ArrayList;
import java.util.Random;

public class WeatherStation {
	String weather;
	String[] weathers={"晴","雨","多雲","霧霾","雪"};
	ArrayList<Personaction> list=new ArrayList<Personaction>();
	public void weathergeneration(){		
		Random random=new Random();
		int index=random.nextInt(weathers.length);
		weather=weathers[index];
		System.out.println("當前天氣是:"+weather);
	}
	public void add(Personaction e){
		list.add(e);
	}
	public void startt(){
		while(true){
			weathergeneration();
			for(Personaction lis:list){
				lis.notifyWeather(weather);
			}
			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace(); 
			}			
			
		}
	}
	public static void main(String[] args){
		new WeatherStation().startt();
	}

}


工廠模式

參考:

https://blog.csdn.net/zxt0601/article/details/52798423

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章