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

Implement Design Patterns to Get Better Structure #477

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

prakrutsuthar
Copy link

@prakrutsuthar prakrutsuthar commented Apr 12, 2022

Implement Design Patterns to Get Better Structure

1. Singleton:
Singleton design pattern is defined as single class has single responsibility. This pattern makes use of a single class that is in responsible of producing an object while assuring that only one is produced. This class has an object-access function that may be used without initially constructing the class.

Roles – Singleton can be applied on Utils.java class
(Utils.java, private Utils() and static Utils getInstance()) can be implemented.

Location: src/main/java/technology/tabula/Utils.java
src/main/java/technology/tabula/Page.java
src/main/java/technology/tabula/TextStripper.java

I have implemented this design pattern for part 2 please check out the UML diagram provided there for better understanding.

Implemented by these steps:
So here for tabula-java project there is one class called Utils which contains all the utilities in the program. We can implement the singleton for this class by

  1. Making the class [Utils.java] private constructor in Utils. [ private Utils(){} ]
  2. Then, creating a static construction technique that is like a function Object.
  3. Behind the scenes, this method calls the private function Object to construct an object and saves it in a static field. [public static Utils getInstance(){} ]
  4. The cached object will be returned in all subsequent calls to this method. [public static Utils getInstance(){} ]
  5. The code will have access to the Singleton class Utils.java, then it’s able to call the Singleton’s static method [static Utils getInstance()]. So whenever that method is called, the same object is always returned.

2. Factory Design Pattern:
The Factory pattern to create objects without revealing the creation code to the client, and we refer to newly produced objects using a common interface. This type of design pattern is characterised as a creational pattern since it provides one of the best methods to build an object.

Roles – CommandLineApp and TestWriters may have factory pattern for writers by implementing from the Writer.java.
(CommandLineApp.java, public void Writetabels(),
Calling two different writers CSVWriter and JSONWriter)) can be used by the Factory Design pattern.
I will implement class FactoryWriter.java for it.
I have implemented this design pattern for part 2 please check out the UML diagram provided there for better understanding.

Location:

src/main/java/technology/tabula/writers/Writer.java
src/main/java/technology/tabula/writers/JSONWriter.java
src/main/java/technology/tabula/writers/JSONWriter.java
src//main/java/technology/tabula/CommandLineApp.java

Can be implemented by these steps:

  1. Creating an interface and concrete classes implementing the interface. Interface which is already exist in the writer directory of the project. [Writer.java]
  2. A factory class is defined as a next step. Create a Factory to generate object of concrete class based on given information. [WriterFactory.java]
  3. To derive these methods in subclasses overriding the factory method in the subclasses and extract the appropriate bits of construction code from the base method.
  4. The respective choice will be called in used classes. E.g., here CSVWriter and JSONWriter are called in the CommandLineApp by if conditions so we can call factory to define it rather than calling by declaring the object.
    [FactoryWriter.getInstance(“Which ever functionality”)]

3. Prototype Design Pattern:
By implementing a prototype interface for TabulaInterface, then we can create a clone of the current object. Then this prototype will be used in the place of creating new object which will help creating the clone of that object. A prototype is a cloneable object. When the objects contain dozens of fields and hundreds of possible configurations, cloning them may be a better alternative than subclassing.

Roles – Prototype can be applied on Multiple places for example Line.java and TextChunk.java
(Line.java, TextChunk.java) can be used by the prototyping it.

Location:

src/main/java/technology/tabula/Line.java
src/main/java/technology/tabula/TextChunk.java

Implemented by these steps:

  1. Creating the prototype interface here I will make TabulaInterface and declare the clone method in it. [TabulaInterface.java]
  2. A prototype class will define an alternate function Object that takes as an argument an object of that class. A subclass must use the parent function Object to allow the superclass to manage the cloning of its private fields. [makeDuplicate()]
  3. This function should look for a prototype depending on search parameters passed to it by the client code. The criterion might be as basic as a string tag or as sophisticated as a collection of search parameters. [newDuplicate()]
  4. So the newDuplicate will be called by the Line.java and TextChunk.java which will access the CloneFactoryTabula and it will refer the the TabulaInterface in this case.

Thank you for giving me the opportunity to contribute to this project I would be glad if you accept this pull request. Please contact me if you have any questions or suggestions or any additions to this pull request. Looking forward to hearing back.

  • Prakrut

@prakrutsuthar prakrutsuthar changed the title Implement Design Patterns to Get Implement Design Patterns to Get Better Structure Apr 12, 2022
@oswardlx
Copy link

what are you doing?
where is your Singleton's double lock?
you use Singleton but Utils' method is still static
Java's Singleton means there is only one instance of one specific class in their JVM.
it is unnecessary to invoke static method with instance

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

Successfully merging this pull request may close these issues.

2 participants