Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Inversion Principle Concerns #1

Open
lborja04 opened this issue Nov 7, 2023 · 0 comments
Open

Dependency Inversion Principle Concerns #1

lborja04 opened this issue Nov 7, 2023 · 0 comments

Comments

@lborja04
Copy link

lborja04 commented Nov 7, 2023

=== THIS ISSUE IS POSTED AS A COLLEGE ASSIGNMENT ON SOLID DESIGN PRINCIPLES, PLEASE CLOSE THE
ISSUE IF YOU FIND IT TO BE USELESS. ===

  1. src/com/jpexs/jbomutils/DumpBom.java

Concern: In this code, the BomPaths, BomPointer, and BomTree classes are low-level modules, while DumpBom is high-level. However, DumpBom depends directly on these low-level classes, which could be considered a violation of the DIP principle.

Solution: To apply the DIP principle, one could introduce an interface or abstract class that acts as an abstraction for BomPaths, BomPointer, and BomTree. DumpBom would then depend on this abstraction instead of directly depending on concrete implementations.

public interface BomData {
    //Common methods that should be implemented by concrete classes
}

public class BomPaths implements BomData {
    // Specific implementation for BomPaths
}

public class BomPointer implements BomData {
    // Specific implementation for BomPointer.
}

public class BomTree implements BomData {
    // Specific implementation for BomTree.
}

public class DumpBom {
    public static void print_paths(BomData paths, byte[] buffer, List<BomData> block_table, int id) {
        // Rest of the code...
    }

    public static void print_tree(BomData tree, byte[] buffer, List<BomData> block_table) {
        // Rest of the code...
    }

    // Rest of the code...
}

DIP 1

  1. src/com/jpexs/jbomutils/Tools.java ; src/com/jpexs/jbomutils/BomVar.java ; src/com/jpexs/jbomutils/BomFile.java ; src/com/jpexs/jbomutils/BomInfoEntry.java ; src/com/jpexs/jbomutils/BomPaths.java ; src/com/jpexs/jbomutils/BomTree.java ; src/com/jpexs/jbomutils/BomPathInfo2.java

Concern: BomInputStream and BomOutputStream are used extensively in the code, it might be beneficial to introduce interfaces for them and ensure that all the classes using them depend on those interfaces rather than concrete implementations.

Solution: Create interfaces like InputStreamProvider and OutputStreamProvider that define the necessary BomInputStream and BomOutputStream methods that the code uses, respectively. Then, adjust BomInfoEntry's dependencies to depend on these interfaces instead of the concrete implementations. This will make testing easier and allow for more flexibility in the future if in need to change the underlying BomInputStream or BomOutputStream implementation. Additionally, this abstraction will make BomInfoEntry more independent of the concrete implementations of the input and output classes.

import java.io.InputStream;

public interface InputStreamProvider {
    InputStream provideInputStream(byte[] data);
    InputStream provideInputStream(byte[] data, int offset);
    InputStream provideInputStream(byte[] data, int offset, int len);
}

import java.io.OutputStream;

public interface OutputStreamProvider {
    OutputStream provideOutputStream();
}

//example of the class Tool implementing the interfaces
public class Tools {

    public static final int sizeof_uint32_t = 4;
    public static final int sizeof_uint16_t = 2;

    public static String getline(InputStreamProvider inputStreamProvider, int delim) throws IOException {
        // Rest of the code...
    }

    public static String getline(InputStreamProvider inputStreamProvider) throws IOException {
        // Rest of the code...
    }

    public static BomInputStream getBIS(InputStreamProvider inputStreamProvider, byte data[]) {
        // Rest of the code...
    }

    public static BomInputStream getBIS(InputStreamProvider inputStreamProvider, byte data[], int offset) {
        // Rest of the code...
    }

    public static BomInputStream getBIS(InputStreamProvider inputStreamProvider, byte data[], int offset, int len) {
        // Rest of the code...
    }

    public static byte[] getBytes(OutputStreamProvider outputStreamProvider, WritableTo w) {
        // Rest of the code...
    }
}

DIP 2

  1. src/com/jpexs/jbomutils/Tools.java ; src/com/jpexs/jbomutils/BomInputStream.java ; src/com/jpexs/jbomutils/LsBom.java ; src/com/jpexs/jbomutils/Crc32.java ; src/com/jpexs/jbomutils/MkBom.java

Concern: Since several classes depend on InputStream, you might consider introducing interfaces to decouple these dependencies and allow for greater flexibility in implementations.

Solution: You could define an interface, for example StreamProvider, that has methods to get stream instances (InputStream and OutputStream). You can then make classes depend on this interface instead of depending directly on InputStream. This way, you could provide different StreamProvider implementations depending on the specific needs of each class.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

interface StreamProvider {
    InputStream getInputStream();
    OutputStream getOutputStream();
}

public class Tools {

    // Rest of the code...

    public static String getline(StreamProvider streamProvider, int delim) throws IOException {
        // Implementation of the getline using streamProvider.getInputStream()
    }

    public static String getline(StreamProvider streamProvider) throws IOException {
        return getline(streamProvider, '\n');
    }

    public static BomInputStream getBIS(byte data[]) {
        return getBIS(data, 0, data.length);
    }

    // Rest of the code...
}

DIP 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant