---
title: "设计模式"
author: "sylixj2009"
date: "2010-10-23T22:09:34Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-10-23T22:31:12Z"
id: 792303
url: "https://springcher.iteye.com/blog/792303"
---

一 单例模式
1.
public class CPUQueueA {
	private static CPUQueueA q=null;
	
	private CPUQueueA(){
		
	}
	
	public synchronized static CPUQueueA getQueue(){
		if(q==null){
			q=new CPUQueueA();
		}
		return q;
	}
	

}


2.
public class CPUQueue {
	private static final CPUQueue q=new CPUQueue();
	
	private CPUQueue(){
		
	}
	
	public static CPUQueue getQueue(){
		return q;
	}
}
二 观察者模式
账户类
import java.util.HashSet;

public class Account {
	private String name;
	private long id;
	private double money;
	private HashSet<Observer> allObservers;
	
	public Account(String name, long id, double money) {
		super();
		this.name = name;
		this.id = id;
		this.money = money;
		allObservers=new HashSet<Observer>();
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
		
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public void addObserver(Observer ob){
		allObservers.add(ob);
	}
	
	public void operation(AccountOperation op){
		for(Observer ob:allObservers){
			ob.update(op);
		}
	}

}
账户操作类
public class AccountOperation {
	private String type;
	private double money;
	private Account acc;
	public AccountOperation(String type, double money, Account acc) {
		super();
		this.type = type;
		this.money = money;
		this.acc = acc;
	}
	public Account getAcc() {
		return acc;
	}
	public void setAcc(Account acc) {
		this.acc = acc;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
}
观察接口
public interface Observer {
	public void update(AccountOperation op);

}
实现类1
public class MailObserver implements Observer{

	public void update(AccountOperation op) {
		System.out.println("向用户发送邮件提醒！");
	}

}

实现类2
public class ShortMessageObserver implements Observer{

	public void update(AccountOperation op) {
		System.out.println("向用户发送短信提醒:");
	}

}
应用类
public class Application {
	public static void main(String args) {
		Account a=new Account("test",1234567890,10000.00);
		a.addObserver(new MailObserver());
		a.addObserver(new ShortMessageObserver());
		a.setMoney(5000.00);
		AccountOperation op = new AccountOperation  ("1",200.00,a);
		a.operation(op);
	}

}

三 工厂模式
public abstract class Car {
	public abstract void run();
}
public class BMW extends Car{
	public void run(){
		System.out.println("BMW Running!");
	}
}
public class Benz extends Car{
	public Benz(String model){	
	}
	
	public void run(){
		System.out.println("Benz Running!");
	}
}
public class CarFactory {
	public static Car createCar(int type){
		switch(type){
		case 0:return new Benz("S600");
		case 1: return new BMW();
		}
		return null;
	}
}
public class Application {
	public static void main(String args) {
		Car c1=CarFactory.createCar(0);
		Car c2=CarFactory.createCar(1);
		c1.run();
		c2.run();
	}

}