Java設計模式是指在編程時,按一定的規則或思想來組織相關類和對象的結構,以解決特定問題的一系列解決方案。Java設計模式是對過去經驗的總結和提煉,它在編程中廣泛應用,有助於提高編程效率和程序可重用性。
一、單例模式
單例模式是一種常見的設計模式,它的功能是確保一個類只有一個實例,並且提供全局訪問點。
單例模式的實例化方式分為餓漢式和懶漢式兩種:
餓漢式:
public class Singleton { // 類初始化時,立即加載這個對象(只會有一個實例) private static Singleton instance = new Singleton(); private Singleton() { } // 方法沒有同步,調用效率高 public static Singleton getInstance() { return instance; } }懶漢式:
public class Singleton { private static Singleton instance; private Singleton() { } // 加入同步鎖,解決線程安全問題,但效率低 public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
餓漢式的優點是線程安全,但在類裝載時就實例化,造成程序啟動較慢。懶漢式需要加同步鎖保證線程安全,但每次獲取實例時都需要同步,效率較低。
二、工廠模式
工廠模式又稱工廠方法模式,是一種常見的面向對象編程設計模式,用於實現解耦,讓對象的創建和實現完全分離。
工廠模式可以分為三種:
簡單工廠模式:
public interface Fruit {
void get();
}
public class Apple implements Fruit {
public void get() {
System.out.println("get an apple");
}
}
public class Orange implements Fruit {
public void get() {
System.out.println("get an orange");
}
}
public class Factory {
public static Fruit create(String type) {
if ("apple".equals(type)) {
return new Apple();
} else if ("orange".equals(type)) {
return new Orange();
} else {
return null;
}
}
}工廠方法模式:
public interface Fruit {
void get();
}
public class Apple implements Fruit {
public void get() {
System.out.println("get an apple");
}
}
public class Orange implements Fruit {
public void get() {
System.out.println("get an orange");
}
}
public interface Factory {
Fruit create();
}
public class AppleFactory implements Factory {
public Fruit create() {
return new Apple();
}
}
public class OrangeFactory implements Factory {
public Fruit create() {
return new Orange();
}
}
public class Main {
public static void main(String[] args) {
Factory appleFactory = new AppleFactory();
Factory orangeFactory = new OrangeFactory();
Fruit apple = appleFactory.create();
Fruit orange = orangeFactory.create();
apple.get();
orange.get();
}
}抽象工廠模式:
public interface Shape {
void draw();
}
public class Circle implements Shape {
public void draw() {
System.out.println("draw a circle");
}
}
public class Square implements Shape {
public void draw() {
System.out.println("draw a square");
}
}
public interface Factory {
Shape createShape();
Color createColor();
}
public class RedCircleFactory implements Factory {
public Shape createShape() {
return new Circle();
}
public Color createColor() {
return new Red();
}
}
public class BlueSquareFactory implements Factory {
public Shape createShape() {
return new Square();
}
public Color createColor() {
return new Blue();
}
}工廠模式可以根據需求的不同,選擇合適的方式來創建對象,實現代碼的靈活和可擴展性。
三、適配器模式
適配器模式是一種結構型設計模式,適用於將一個類的接口轉換成客戶期望的另一個接口,可以協調不兼容接口之間的關係,提高代碼可重用性。
public interface Target {
void request();
}
public class Adaptee {
public void specialRequest() {
System.out.println("special request");
}
}
public class Adapter extends Adaptee implements Target {
public void request() {
specialRequest();
}
}適配器模式通過繼承、組合等方式來實現接口的轉換,可以解決需要在不兼容的接口之間進行通信的情況,提高代碼的可維護性。
四、裝飾器模式
裝飾器模式是一種結構型設計模式,用於在不修改現有代碼的情況下動態擴展對象的功能。
public interface Shape {
void draw();
}
public class Circle implements Shape {
public void draw() {
System.out.println("draw a circle");
}
}
public abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape) {
this.decoratedShape = decoratedShape;
}
public void draw() {
decoratedShape.draw();
}
}
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape) {
System.out.println("set red border");
}
}裝飾器模式可以在不改變原有代碼的情況下,動態地為一個對象添加額外的職責,可以有效地提高代碼的復用性和可維護性。
五、觀察者模式
觀察者模式是一種行為型設計模式,用於在對象間建立一對多的依賴關係,當一個對象的狀態發生改變時,所有依賴它的對象都會得到通知並自動更新。
public interface Observer {
void update(String message);
}
public interface Subject {
void addObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers(String message);
}
public class ConcreteSubject implements Subject {
private List observers = new ArrayList();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
public class ConcreteObserver implements Observer {
private String name;
public ConcreteObserver(String name) {
this.name = name;
}
public void update(String message) {
System.out.println(name + ": " + message);
}
}觀察者模式可以實現對象間的松耦合,提高代碼的可擴展性和可重用性。
原創文章,作者:NQHW,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/132518.html
微信掃一掃
支付寶掃一掃