欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android/Java中創(chuàng)建類實(shí)例的各種模式實(shí)例代碼

 更新時間:2025年09月08日 10:00:24   作者:滬cares  
這篇文章主要介紹了Android/Java中創(chuàng)建類實(shí)例各種模式的相關(guān)資料,包括New關(guān)鍵字、靜態(tài)工廠方法、建造者模式、單例模式、依賴注入、抽象工廠模式、原型模式和Reflection,每個模式有其特點(diǎn)和適用場景,需要的朋友可以參考下

創(chuàng)建型模式總覽

模式名稱核心思想使用頻率難度
new 關(guān)鍵字直接調(diào)用構(gòu)造函數(shù)??????
靜態(tài)工廠方法通過靜態(tài)方法創(chuàng)建實(shí)例??????
建造者模式分步構(gòu)建復(fù)雜對象??????
單例模式確保全局唯一實(shí)例??????
依賴注入外部容器管理依賴????????
抽象工廠模式創(chuàng)建相關(guān)對象家族??????
原型模式通過克隆創(chuàng)建實(shí)例????
反射創(chuàng)建運(yùn)行時動態(tài)創(chuàng)建?????

1. new 關(guān)鍵字 (Direct Instantiation)

名詞解釋

最基礎(chǔ)的實(shí)例創(chuàng)建方式,直接調(diào)用類的構(gòu)造函數(shù)來創(chuàng)建對象實(shí)例。

核心特點(diǎn)

// 基本語法
ClassName object = new ClassName(arguments);

// 示例
User user = new User("Alice", 25);
TextView textView = new TextView(context);

優(yōu)點(diǎn)

  • 簡單直觀:語法簡單,學(xué)習(xí)成本低
  • 性能最佳:沒有額外的開銷,直接調(diào)用構(gòu)造函數(shù)
  • 編譯時檢查:類型安全,編譯時就能發(fā)現(xiàn)錯誤
  • 明確性:代碼意圖清晰,易于理解

缺點(diǎn)

  • 緊耦合:客戶端代碼直接依賴具體實(shí)現(xiàn)類
  • 缺乏靈活性:無法在創(chuàng)建過程中加入額外邏輯
  • 難以測試:難以替換為Mock對象進(jìn)行單元測試
  • 構(gòu)造函數(shù)膨脹:參數(shù)過多時代碼難以維護(hù)

適用場景

  • 簡單的數(shù)據(jù)對象(POJO、DTO)
  • 在類內(nèi)部創(chuàng)建輔助工具對象
  • 性能要求極高的場景
  • 原型開發(fā)或一次性代碼

Android 示例

// 創(chuàng)建基礎(chǔ)UI組件
TextView textView = new TextView(context);
Button button = new Button(context);

// 創(chuàng)建數(shù)據(jù)對象
Intent intent = new Intent(context, MainActivity.class);
Bundle bundle = new Bundle();

2. 靜態(tài)工廠方法 (Static Factory Method)

名詞解釋

通過類的靜態(tài)方法來創(chuàng)建實(shí)例,而不是直接調(diào)用構(gòu)造函數(shù)。

核心特點(diǎn)

public class Connection {
    private String url;
    
    private Connection(String url) {
        this.url = url;
    }
    
    // 靜態(tài)工廠方法
    public static Connection create(String url) {
        validateUrl(url); // 添加驗(yàn)證邏輯
        return new Connection(url);
    }
    
    // 有名稱的工廠方法
    public static Connection createSecureConnection() {
        return new Connection("https://secure.example.com");
    }
}

// 使用
Connection conn = Connection.create("https://api.example.com");

優(yōu)點(diǎn)

  • 有意義的名稱:方法名可以描述創(chuàng)建邏輯
  • 控制實(shí)例化:可以緩存實(shí)例、參數(shù)驗(yàn)證、返回子類
  • 降低耦合:客戶端只需知道工廠方法接口
  • 靈活性:可以返回接口而非具體實(shí)現(xiàn)

缺點(diǎn)

  • 無法繼承:如果類沒有公共構(gòu)造器,則無法被繼承
  • 不易發(fā)現(xiàn):工廠方法與其他靜態(tài)方法混在一起
  • 需要文檔:需要說明哪些是工廠方法

適用場景

  • 需要控制創(chuàng)建邏輯(驗(yàn)證、緩存)
  • 創(chuàng)建過程有名稱區(qū)分不同行為
  • 返回接口而非具體實(shí)現(xiàn)
  • 需要緩存或重用實(shí)例

Android 示例

// Intent 工廠方法
Intent chooserIntent = Intent.createChooser(shareIntent, "Share via");

// Bitmap 工廠方法
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

// Uri 解析
Uri uri = Uri.parse("content://com.example.provider/data");

3. 建造者模式 (Builder Pattern)

名詞解釋

通過一個建造者類來分步構(gòu)建復(fù)雜對象,特別適合參數(shù)多的場景。

核心特點(diǎn)

public class AlertDialogConfig {
    private final String title;
    private final String message;
    private final boolean cancelable;
    
    private AlertDialogConfig(Builder builder) {
        this.title = builder.title;
        this.message = builder.message;
        this.cancelable = builder.cancelable;
    }
    
    public static class Builder {
        private String title;
        private String message;
        private boolean cancelable = true;
        
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
        
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }
        
        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }
        
        public AlertDialogConfig build() {
            return new AlertDialogConfig(this);
        }
    }
}

// 使用
AlertDialogConfig config = new AlertDialogConfig.Builder()
    .setTitle("Warning")
    .setMessage("Are you sure?")
    .setCancelable(false)
    .build();

優(yōu)點(diǎn)

  • 極佳的可讀性:鏈?zhǔn)秸{(diào)用清晰表達(dá)意圖
  • 參數(shù)靈活性:處理多個可選參數(shù)
  • 不可變對象:適合創(chuàng)建不可變對象
  • 參數(shù)驗(yàn)證:在build()方法中集中驗(yàn)證
  • 分步構(gòu)建:可以分多個步驟構(gòu)建

缺點(diǎn)

  • 代碼冗余:需要編寫大量的樣板代碼
  • 創(chuàng)建開銷:需要先創(chuàng)建Builder對象
  • 學(xué)習(xí)成本:對新手可能不太直觀

適用場景

  • 具有多個可選參數(shù)的對象(4個或更多)
  • 需要創(chuàng)建不可變對象
  • 參數(shù)配置復(fù)雜且需要良好可讀性
  • 需要分步驟構(gòu)建的復(fù)雜對象

Android 示例

// Notification 建造者
Notification notification = new NotificationCompat.Builder(context, "channel_id")
    .setContentTitle("Title")
    .setContentText("Message")
    .setSmallIcon(R.drawable.ic_notification)
    .build();

// Retrofit 建造者
Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.example.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

4. 單例模式 (Singleton Pattern)

名詞解釋

確保一個類只有一個實(shí)例,并提供全局訪問點(diǎn)。

核心特點(diǎn)

// 雙重檢查鎖實(shí)現(xiàn)
public class AppManager {
    private static volatile AppManager instance;
    private final Context appContext;
    
    private AppManager(Context context) {
        this.appContext = context.getApplicationContext();
    }
    
    public static AppManager getInstance(Context context) {
        if (instance == null) {
            synchronized (AppManager.class) {
                if (instance == null) {
                    instance = new AppManager(context);
                }
            }
        }
        return instance;
    }
}

// 靜態(tài)內(nèi)部類實(shí)現(xiàn)(推薦)
public class DatabaseHelper {
    private DatabaseHelper() {}
    
    private static class Holder {
        static final DatabaseHelper INSTANCE = new DatabaseHelper();
    }
    
    public static DatabaseHelper getInstance() {
        return Holder.INSTANCE;
    }
}

優(yōu)點(diǎn)

  • 全局唯一訪問點(diǎn):確保整個應(yīng)用中使用同一個實(shí)例
  • 節(jié)省資源:避免重復(fù)創(chuàng)建昂貴對象
  • 延遲初始化:支持按需創(chuàng)建
  • 全局狀態(tài)管理:方便管理應(yīng)用級狀態(tài)

缺點(diǎn)

  • 全局狀態(tài):可能導(dǎo)致隱藏的耦合
  • 線程安全問題:需要小心處理多線程環(huán)境
  • 測試?yán)щy:全局狀態(tài)使得單元測試復(fù)雜
  • 內(nèi)存泄漏風(fēng)險:可能持有Context導(dǎo)致內(nèi)存泄漏

適用場景

  • 全局配置管理
  • 資源密集型對象(數(shù)據(jù)庫連接、網(wǎng)絡(luò)客戶端)
  • 需要嚴(yán)格單例的系統(tǒng)服務(wù)
  • 應(yīng)用級別的狀態(tài)管理

Android 示例

// Application 類本身就是單例
public class MyApp extends Application {
    private static MyApp instance;
    
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }
    
    public static MyApp getInstance() {
        return instance;
    }
}

// 使用單例
ImageLoader.getInstance().loadImage(url, imageView);

5. 依賴注入 (Dependency Injection)

名詞解釋

對象的依賴由外部容器提供,而不是自己創(chuàng)建,實(shí)現(xiàn)控制反轉(zhuǎn)。

核心特點(diǎn)

// 手動依賴注入
public class UserRepository {
    private final ApiService apiService;
    
    public UserRepository(ApiService apiService) {
        this.apiService = apiService; // 依賴注入
    }
}

// 使用 Dagger/Hilt
@Module
@InstallIn(SingletonComponent.class)
public class NetworkModule {
    @Provides
    @Singleton
    public Retrofit provideRetrofit() {
        return new Retrofit.Builder()
            .baseUrl("https://api.example.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    }
}

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
    @Inject
    Retrofit retrofit; // 自動注入
}

優(yōu)點(diǎn)

  • 極致解耦:組件間不直接依賴
  • 易于測試:可以輕松注入Mock對象
  • 生命周期管理:框架管理對象的創(chuàng)建和銷毀
  • 代碼復(fù)用:依賴項(xiàng)可在多處共享
  • 配置集中化:依賴配置集中在模塊中

缺點(diǎn)

  • 學(xué)習(xí)曲線陡峭:需要理解復(fù)雜的概念
  • 編譯時開銷:注解處理增加編譯時間
  • 調(diào)試?yán)щy:錯誤信息可能不直觀
  • 過度工程:小項(xiàng)目可能過于復(fù)雜

適用場景

  • 中大型項(xiàng)目,需要良好的架構(gòu)
  • 需要高度可測試性的項(xiàng)目
  • 復(fù)雜的依賴關(guān)系圖
  • 團(tuán)隊(duì)協(xié)作開發(fā),需要統(tǒng)一架構(gòu)

Android 示例

// Hilt 注入 ViewModel
@HiltViewModel
public class MainViewModel extends ViewModel {
    private final UserRepository repository;
    
    @Inject
    public MainViewModel(UserRepository repository) {
        this.repository = repository;
    }
}

// Activity 中使用
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
    @Inject
    MainViewModel viewModel;
}

6. 抽象工廠模式 (Abstract Factory Pattern)

名詞解釋

創(chuàng)建相關(guān)或依賴對象的家族,而不需要指定具體類。

核心特點(diǎn)

public interface ThemeFactory {
    Button createButton();
    TextView createTextView();
    Dialog createDialog();
}

public class LightThemeFactory implements ThemeFactory {
    @Override
    public Button createButton() {
        Button button = new Button(context);
        button.setBackgroundColor(Color.WHITE);
        return button;
    }
    // 其他方法...
}

public class DarkThemeFactory implements ThemeFactory {
    @Override
    public Button createButton() {
        Button button = new Button(context);
        button.setBackgroundColor(Color.BLACK);
        return button;
    }
    // 其他方法...
}

優(yōu)點(diǎn)

  • 產(chǎn)品族一致性:確保創(chuàng)建的對象相互兼容
  • 開閉原則:易于添加新的產(chǎn)品族
  • 客戶端解耦:客戶端與具體實(shí)現(xiàn)解耦
  • 統(tǒng)一接口:提供統(tǒng)一的創(chuàng)建接口

缺點(diǎn)

  • 復(fù)雜度高:需要定義大量接口和類
  • 難以擴(kuò)展:添加新產(chǎn)品需要修改工廠接口
  • 過度設(shè)計(jì):簡單場景下顯得過于復(fù)雜

適用場景

  • 需要創(chuàng)建相關(guān)或依賴的對象家族
  • 系統(tǒng)需要獨(dú)立于產(chǎn)品的創(chuàng)建、組合和表示
  • 需要提供多個產(chǎn)品族,但只使用其中一族
  • GUI 主題系統(tǒng)、跨平臺UI組件

7. 原型模式 (Prototype Pattern)

名詞解釋

通過克隆現(xiàn)有對象來創(chuàng)建新實(shí)例,避免昂貴的初始化過程。

核心特點(diǎn)

public class UserProfile implements Cloneable {
    private String username;
    private Map<String, Object> preferences;
    
    public UserProfile(String username) {
        this.username = username;
        this.preferences = loadPreferences(); // 耗時操作
    }
    
    @Override
    public UserProfile clone() {
        try {
            UserProfile cloned = (UserProfile) super.clone();
            cloned.preferences = new HashMap<>(this.preferences); // 深拷貝
            return cloned;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}

// 使用
UserProfile original = new UserProfile("user123");
UserProfile copy = original.clone(); // 比 new 快很多

優(yōu)點(diǎn)

  • 性能優(yōu)化:避免昂貴的初始化過程
  • 簡化創(chuàng)建:簡化復(fù)雜對象的創(chuàng)建過程
  • 動態(tài)配置:可以在運(yùn)行時克隆配置好的對象

缺點(diǎn)

  • 深拷貝復(fù)雜:需要小心處理所有引用類型
  • 克隆方法濫用:可能被用于規(guī)避構(gòu)造器邏輯
  • 內(nèi)存占用:可能占用更多內(nèi)存

適用場景

  • 創(chuàng)建對象成本很高(需要大量計(jì)算或IO)
  • 需要創(chuàng)建相似但略有不同的對象
  • 系統(tǒng)需要獨(dú)立于如何創(chuàng)建、組合產(chǎn)品
  • 游戲開發(fā)中的對象池

8. 反射創(chuàng)建 (Reflection)

名詞解釋

在運(yùn)行時動態(tài)創(chuàng)建對象實(shí)例,通過類名等信息來實(shí)例化對象。

核心特點(diǎn)

public class ObjectFactory {
    public static <T> T createInstance(String className) {
        try {
            Class<?> clazz = Class.forName(className);
            return (T) clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("創(chuàng)建實(shí)例失敗", e);
        }
    }
}

// 使用
String className = "com.example.MyClass";
MyClass obj = ObjectFactory.createInstance(className);

優(yōu)點(diǎn)

  • 極度靈活:可以在運(yùn)行時動態(tài)創(chuàng)建任何類的實(shí)例
  • 實(shí)現(xiàn)插件系統(tǒng):可以加載并實(shí)例化未知的類
  • 解耦:客戶端不需要知道具體類名

缺點(diǎn)

  • 性能差:比直接調(diào)用構(gòu)造器慢很多
  • 安全性問題:可以繞過訪問控制
  • 編譯時檢查缺失:錯誤只能在運(yùn)行時發(fā)現(xiàn)
  • 代碼可讀性差:難以理解和維護(hù)

適用場景

  • 框架開發(fā)(如依賴注入容器)
  • 動態(tài)加載類(插件系統(tǒng))
  • 序列化/反序列化庫
  • 配置驅(qū)動的對象創(chuàng)建

總結(jié)對比表

模式優(yōu)點(diǎn)缺點(diǎn)適用場景使用頻率
new 關(guān)鍵字簡單、高性能、明確緊耦合、缺乏靈活性簡單對象、內(nèi)部使用?????
靜態(tài)工廠有名稱、可控制、可緩存不能繼承、不易發(fā)現(xiàn)需要控制創(chuàng)建邏輯????
建造者參數(shù)靈活、可讀性好代碼冗余、創(chuàng)建開銷多參數(shù)對象、配置復(fù)雜???
單例全局唯一、節(jié)省資源全局狀態(tài)、測試?yán)щy全局管理、資源密集型????
依賴注入解耦、易測試、生命周期管理學(xué)習(xí)曲線陡、調(diào)試復(fù)雜中大型項(xiàng)目、需要架構(gòu)????
抽象工廠產(chǎn)品族一致性、開閉原則復(fù)雜度高、難以擴(kuò)展創(chuàng)建相關(guān)對象家族??
原型性能優(yōu)化、避免昂貴初始化深拷貝復(fù)雜、可能濫用創(chuàng)建成本高的相似對象?
反射極度靈活、動態(tài)創(chuàng)建性能差、安全問題框架開發(fā)、插件系統(tǒng)?

Android 開發(fā)建議

  1. 簡單場景:優(yōu)先使用 new 和靜態(tài)工廠方法
  2. UI 組件:使用建造者模式(AlertDialog、Notification)
  3. 業(yè)務(wù)邏輯:使用依賴注入(Dagger/Hilt)
  4. 全局服務(wù):謹(jǐn)慎使用單例模式,注意內(nèi)存泄漏
  5. 性能敏感:考慮原型模式避免重復(fù)昂貴操作
  6. 框架開發(fā):在必要時使用反射和抽象工廠

到此這篇關(guān)于Android/Java中創(chuàng)建類實(shí)例的各種模式的文章就介紹到這了,更多相關(guān)Java創(chuàng)建類實(shí)例模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot如何獲取Kafka的Topic列表

    SpringBoot如何獲取Kafka的Topic列表

    這篇文章主要介紹了SpringBoot如何獲取Kafka的Topic列表問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • SpringBoot全局配置long轉(zhuǎn)String丟失精度的問題解決

    SpringBoot全局配置long轉(zhuǎn)String丟失精度的問題解決

    web項(xiàng)目中,Java后端傳過來的Long/long類型,前端JS接收會丟失精度。那么應(yīng)該如何解決,本文就來介紹一下幾種方法,感興趣的可以了解一下
    2021-08-08
  • Java多線程案例實(shí)戰(zhàn)之定時器的實(shí)現(xiàn)

    Java多線程案例實(shí)戰(zhàn)之定時器的實(shí)現(xiàn)

    在Java中可以使用多線程和定時器來實(shí)現(xiàn)定時任務(wù),下面這篇文章主要給大家介紹了關(guān)于Java多線程案例之定時器實(shí)現(xiàn)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • 解決String字符串轉(zhuǎn)JSONObject順序不對的問題

    解決String字符串轉(zhuǎn)JSONObject順序不對的問題

    這篇文章主要介紹了解決String字符串轉(zhuǎn)JSONObject順序不對的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 使用Java動態(tài)創(chuàng)建Flowable會簽?zāi)P偷氖纠a

    使用Java動態(tài)創(chuàng)建Flowable會簽?zāi)P偷氖纠a

    動態(tài)創(chuàng)建流程模型,尤其是會簽(Parallel Gateway)模型,是提升系統(tǒng)靈活性和響應(yīng)速度的關(guān)鍵技術(shù)之一,本文將通過Java編程語言,深入探討如何在運(yùn)行時動態(tài)地創(chuàng)建包含會簽環(huán)節(jié)的Flowable流程模型,需要的朋友可以參考下
    2024-05-05
  • Eclipse項(xiàng)目有紅感嘆號的解決方法

    Eclipse項(xiàng)目有紅感嘆號的解決方法

    這篇文章主要為大家詳細(xì)介紹了Eclipse項(xiàng)目有紅感嘆號的解決方法,給出了Eclipse項(xiàng)目有紅感嘆號的原因,以及如何解決?,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • IDEA 開發(fā)多項(xiàng)目依賴的方法(圖文)

    IDEA 開發(fā)多項(xiàng)目依賴的方法(圖文)

    這篇文章主要介紹了IDEA 開發(fā)多項(xiàng)目依賴的方法(圖文),本文講一下關(guān)于使用IntelliJ IDEA基于Maven創(chuàng)建多模塊項(xiàng)目的實(shí)際開發(fā),非常具有實(shí)用價值,需要的朋友可以參考下
    2018-10-10
  • SpringBoot 對象存儲 MinIO的詳細(xì)過程

    SpringBoot 對象存儲 MinIO的詳細(xì)過程

    MinIO 是一個基于 Go 實(shí)現(xiàn)的高性能、兼容 S3 協(xié)議的對象存儲,它適合存儲海量的非結(jié)構(gòu)化的數(shù)據(jù),這篇文章主要介紹了SpringBoot 對象存儲 MinIO,需要的朋友可以參考下
    2023-07-07
  • 基于LinkedHashMap實(shí)現(xiàn)LRU緩存

    基于LinkedHashMap實(shí)現(xiàn)LRU緩存

    LinkedHashMap是Java集合中一個常用的容器,它繼承了HashMap, 是一個有序的Hash表。那么該如何基于LinkedHashMap實(shí)現(xiàn)一個LRU緩存呢?本文將介紹LinkedHashMap的實(shí)現(xiàn)原理,感興趣的同學(xué)可以參考一下
    2023-05-05
  • Java設(shè)置PDF有序和無序列表的知識點(diǎn)總結(jié)

    Java設(shè)置PDF有序和無序列表的知識點(diǎn)總結(jié)

    在本篇文章中小編給大家整理了關(guān)于Java設(shè)置PDF有序和無序列表的知識點(diǎn),需要的朋友們參考下。
    2019-03-03

最新評論