一、設計模式概述
設計模式是在軟體開發中常見的一種解決問題的思想模式,可以被認為是對過去解決特定問題經驗的提取和抽象。
總體而言,設計模式可以分為創建型、結構型和行為型三種類別。具有典型代表的常用模式有工廠模式、單例模式、適配器模式、裝飾者模式、觀察者模式等。
下面具體介紹設計模式的部分內容。
二、創建型模式
創建型模式的主要思想是將負責生成對象的過程進行抽象、封裝,從而與具體類的實現過程進行解耦。
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 observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData(){
observers = new ArrayList();
}
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");
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/239403.html
微信掃一掃
支付寶掃一掃