Giter VIP home page Giter VIP logo

social-media's Introduction

Advanced Clean Architecture for Social Media App

Embark on a journey through refined code as we revisit the Social Media app, now re-architected with the robustness of Clean Architecture and the timelessness of SOLID principles.

Leveraging Clean Architecture to Uphold SOLID Principles

Clean Architecture isn't just a structural blueprint for our apps; it's a commitment to quality, scalability, and maintainability. By intertwining Clean Architecture with SOLID principles, we've crafted a codebase that is resilient to change and open to extension.

๐Ÿ“‘ Single Responsibility Principle (SRP)

Our components are laser-focused, each with a singular purpose. Take a glimpse into our RepositoryImp class, a testament to SRP:

class RepositoryImp @Inject constructor(
    private var database: Database
) : Repository {
    override suspend fun getPosts(): List<Post> = database.getAllPosts()
    override fun getUser(): User { /* Simplified for brevity */ }
}

Here, each function has one responsibility: fetching posts and user data, respectively.

๐Ÿ”จ ๐Ÿ”ง Open/Closed Principle (OCP)

We build for the future, ensuring our components are extendable without modification. Our database interface is a perfect example:

interface Database {
    suspend fun getAllPosts(): List<Post>
    // Other abstract methods
}

class DatabaseFromFirebase @Inject constructor(
    private var databaseRef: DatabaseReference
) : Database {
    override suspend fun getAllPosts(): List<Post> {
        // Implementation details...
    }
}

Extensions are made in child classes like DatabaseFromFirebase, keeping the parent interface intact.

โ™ป๏ธ Liskov Substitution Principle (LSP)

Our architecture allows for base classes to be substituted with their derived classes seamlessly:

@Singleton
@Provides
fun provideMainRepository(
    database: Database 
): Repository = RepositoryImp(database)

The RepositoryImp can replace its parent Repository type without altering the expected behavior.

๐Ÿ™†โ€โ™€๏ธ Interface Segregation Principle (ISP)

We adhere to focused interfaces rather than monolithic ones, which aligns with the Interface Segregation Principle (ISP). Our design philosophy avoids large, cumbersome interfaces that burden classes with unnecessary implementations. Instead, we advocate for creating separate interfaces, each tailored for specific client entities. This strategic partitioning ensures that a class only implements the interfaces whose methods are required for its unique functionality.

For cases where it's necessary to provide common functionality across all implementing classes, we utilize the default body approach in Kotlin. This allows us to define default implementations for interface methods:

interface RepositoryAuth {
    fun notImportantForAll() {
        // Default implementation
    }
    // Other essential abstract methods...
}

This method is part of the RepositoryAuth interface, but it doesn't force all implementing classes to override it if the functionality is not required, adhering to ISP while maintaining flexibility.

This prevents classes from being forced to ๐Ÿ’ช implement methods that they don't need.

๐Ÿ”Œ Dependency Inversion Principle (DIP)

Our components depend on abstractions, not on concrete implementations, allowing for flexible data sources:

class RepositoryImp @Inject constructor(
    private var database: Database //This can be injected as firebase_implementation or Api_implementation
) : Repository {
    // Utilizes Database abstraction, not a specific implementation
    // The Change will be just in DataSource(Authenticator or our Database)
}

This design allows us to switch from Firebase to a custom API with minimal friction.

โœจ Wrapping Up

We've gone the extra mile to ensure each SOLID principle is not just met but exemplified within our Clean Architecture implementation. From SRP to DIP, our code demonstrates the harmony between architecture and design principles, resulting in a codebase that's both elegant and robust.

โญ Support and Contributions

Each star โญ on our repository fuels our passion for sharing knowledge and developing open-source projects. Your support encourages more articles and continuous improvement.

We invite you to delve into the code, contribute, and join us in refining the art of Android development.

social-media's People

Contributors

kareemaboelatta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

social-media's Issues

Discussion about DI

image
Above i think return type of first method should not be Task<Any> as i think Task is related to Firebase itself (if not so no problem with that).

as we do abstraction layer so it is preferred to not use a source/lib specific return type so it can be used with any source/lib.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.