一、设计模式概述
设计模式是在软件开发中常见的一种解决问题的思想模式,可以被认为是对过去解决特定问题经验的提取和抽象。 总体而言,设计模式可以分为创建型、结构型和行为型三种类别。具有典型代表的常用模式有工厂模式、单例模式、适配器模式、装饰者模式、观察者模式等。 下面具体介绍设计模式的部分内容。
二、创建型模式
创建型模式的主要思想是将负责生成对象的过程进行抽象、封装,从而与具体类的实现过程进行解耦。
2.1 单例模式
单例模式意味着在一个应用程序中,某个类只有一个实例。单例模式具有以下特点:
- 私有的构造函数
- 视图防止用户直接new实例
- 静态的实例变量保存类的唯一对象
- 公有的静态方法(工厂方法)用于获取唯一对象
public class Singleton {
private static Singleton instance = null;
private Singleton(){
}
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
2.2 工厂模式
工厂模式是针对创建过程进行封装的一种常用模式。其主要思想是将对象的创建过程进行封装,使得其他部分能够通过调用工厂方法来获取对象。 下面是一个简单的工厂模式的实现(以Pizza为例):
public abstract class Pizza{
public void prepare(){
}
public void bake(){
}
public void cut(){
}
public void box(){
}
}
public class CheesePizza extends Pizza{
public CheesePizza(){
System.out.println("CheesePizza prepare!");
}
}
public class PepperoniPizza extends Pizza{
public PepperoniPizza(){
System.out.println("PepperoniPizza prepare!");
}
}
public class PizzaFactory{
public Pizza createPizza(int type){
Pizza pizza = null;
if(type == 1){
pizza = new CheesePizza();
}else if(type == 2){
pizza = new PepperoniPizza();
}
return pizza;
}
}
三、结构型模式
结构型模式的主要思想是通过组合多个对象形成一个更复杂的结构,以解决复杂问题。
3.1 适配器模式
适配器模式是针对已有代码进行维护、重构时的一种常用模式,其主要思想是通过适配设计接口的方式将原来不兼容的接口进行兼容。 下面是一个简单的适配器模式实现(以电源适配器为例):
public interface PowerSource{
public void charge();
}
public class Power220 implements PowerSource{
public void charge(){
System.out.println("220V");
}
}
public class Power110Adapter implements PowerSource{
private PowerSource power220;
public Power110Adapter(PowerSource power220){
this.power220 = power220;
}
public void charge(){
power220.charge();
}
}
public class Phone{
private PowerSource powerSource;
public Phone(PowerSource powerSource){
this.powerSource = powerSource;
}
public void charge(){
powerSource.charge();
}
}
四、行为型模式
行为型模式的主要思想是将对象之间的交互显式化、解耦。其通过将算法、职责和行为进行封装,使得系统的变化不会对这些组件的协作方式进行影响。
4.1 观察者模式
观察者模式是对象之间的一种多对多关系,其主要思想是将主题与观察者解耦,使得它们能够独立地变化。 下面是一个简单的观察者模式实现:
import java.util.ArrayList;
interface Observer{
void update(float temperature, float humidity, float pressure);
}
interface Subject{
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}
class WeatherData implements Subject{
private ArrayList<Observer> observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData(){
observers = new ArrayList<Observer>();
}
public void registerObserver(Observer observer){
observers.add(observer);
}
public void removeObserver(Observer observer){
int i = observers.indexOf(observer);
if(i >= 0){
observers.remove(i);
}
}
public void notifyObservers(){
for(int i = 0; i < observers.size(); i++){
Observer observer = (Observer) observers.get(i);
observer.update(temperature, humidity, pressure);
}
}
public void measurementsChanged(){
notifyObservers();
}
public void setMeasurements(float temperature, float humidity, float pressure){
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}
class CurrentConditionsDisplay implements Observer{
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData){
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temperature, float humidity, float pressure){
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display(){
System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity");
}
}