两种设计模式(观察者模式与单例模式)

      设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。

 一、观察者模式

有时又被称为

       发布-订阅<Publish/Subscribe>模式、模型-视图<Model/View>模式、源-收听者<Source/Listener>模式或从属者<Dependents>模式)这是软件设计模式的一种。

       观察者模式(Observer)完美的将观察者和被观察的对象分离开。

       此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。

       (1)这通常透过呼叫各观察者所提供的方法来实现。

       (2)此种模式通常被用来实作事件处理系统。

       (3) 有多个观察者时,不可以依赖特定的通知次序。

       (4)Swing大量使用观察者模式,许多GUI框架也是如此。

         下边举例一个气象站检测天气的例子。

气象站:

public class WeatherStation {
	
	private String weather;
	
	String[] weathers = {"下雨","下雪","下冰雹","出太阳"};
	
	static	List<BookWeather> list = new ArrayList<BookWeather>();
	
	Random random = new Random();
	
	
	public void startWork(){
		
		new Thread(){
			
			@Override
			public void run() {
				while(true){
					updateWeather();
					try {
						Thread.sleep(random.nextInt(1000)+500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
	public void updateWeather(){
		weather = weathers[random.nextInt(4)];
		System.out.println("天气:"+ weather);
	}		
	
	
	public String getWeather() {
		return weather;
	}

人:

public class Person implements BookWeather {
	
	String name;
	
	public  Person(String name){
		this.name = name;
	}
	
	private WeatherStation station ;
	
	public Person(String name,WeatherStation station){
		this(name);
		this.station = station;
	}
	
	//下雨","下雪 ","下冰雹","出太阳"
	@Override
	public void notifyWeather() {
		String weather = station.getWeather();
		if("下雨".equals(weather)){
			System.out.println(name+"打着雨伞上班");
		}else if("下雪".equals(weather)){
			System.out.println(name+"溜冰 上班");
		}else if("下冰雹".equals(weather)){
			System.out.println(name+"带着头盔 上班");
		}else if("出太阳".equals(weather)){
			System.out.println(name+"嗮着太阳 上班");
		}
	}
		
}

测试类:

public class Test {
	
	public static void main(String[] args) throws InterruptedException {
		WeatherStation station = new WeatherStation();
		station.startWork();
		
		Person p1 = new Person("小明",station);
		while(true){
			p1.notifyWeather();
			Thread.sleep(2000);
		}
	}

问题:天气变化两三次,小明才知道一次。

解决方案 :

package cn.itcast.test;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class WeatherStation {
	
	private String weather;
	
	String[] weathers = {"下雨","下雪","下冰雹","出太阳"};
	
	private static	List<BookWeather> list = new ArrayList<BookWeather>();
	
	Random random = new Random();
	
	public void addListaner(BookWeather e){
		list.add(e);
	}
	
	public void startWork(){
		
		new Thread(){
			
			@Override
			public void run() {
				while(true){
					updateWeather();
					try {
          Thread.sleep(random.nextInt(1000)+500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
	public void updateWeather(){
		weather = weathers[random.nextInt(4)];
		System.out.println("天气:"+ weather);
		for(BookWeather item : list){
			item.notifyWeather(weather);
		}
	}		
	
	
	public String getWeather() {
		return weather;
	}

人:

public class Person implements BookWeather {
	
	String name;
	
	public  Person(String name){
		this.name = name;
	}
	
	private WeatherStation station ;
	
	public Person(String name,WeatherStation station){
		this(name);
		this.station = station;
	}
	
	//下雨","下雪 ","下冰雹","出太阳"
	@Override
	public void notifyWeather(String weather) {
		if("下雨".equals(weather)){
			System.out.println(name+"打着雨伞上班");
		}else if("下雪".equals(weather)){
			System.out.println(name+"溜冰 上班");
		}else if("下冰雹".equals(weather)){
			System.out.println(name+"带着头盔 上班");
		}else if("出太阳".equals(weather)){
			System.out.println(name+"嗮着太阳 上班");
		}
	}
}
		

接口:

public interface BookWeather {
	
	public void notifyWeather(String weather);
}
public class Test {
	
	public static void main(String[] args) throws InterruptedException {
		WeatherStation station = new WeatherStation();
		station.startWork();
		
		Person p1 = new Person("小明");
		Person p2 = new Person("小红");
		Person p3 = new Person("小青 ");
		station.addListaner(p1);
		station.addListaner(p2);
		station.addListaner(p3);
		
	}
}

二、单例模式

 Singleton

是指只能拥有一个实例的类就是单例类。

私有构造方法。

获取方式

通过公共的静态方法创建单一的实例。

两种模式

懒汉模式 – 通常被称为延迟加载。注意存在线程安全问题.

饿汉模式

懒汉式的单例模式线程安全问题的解决方案:

class Single{
	
	//声明本类的一个私有的成员变量
	private static Single single;
	
	//第一步 : 私有化构造方法
	private Single(){
		
	}
	 //  第三步:提供一个公共的方法获取该类的实例对象
	public static Single getInstance(){
		if(single==null){
		synchronized (single) {			
				if(single==null){
					 single = new Single();
				}
			}
		}
		return single;
	}
}

参考自刘意JAVA,侵删。 

 

 

 

 

 

 

  

  

  

  

  


  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值