Solved: Java OOP Design Patterns 

0 Comments

Part I:

Refactoring

Since the Class Log itself has an attachment of the Class File, all its Subclasses (TextLog, AudioLog, VideoLog and ImageLog) have their corresponding one. The problem is that the Subclasses of both are tightly coupled, so the correspondence of types must be validated.

Decouple the classes by generalizing the Class Log as a one and single concrete Class, and delegate the type validation to the File Class and Subclasses.

Simple Builder Pattern

Implement a Builder for the class Log to reduce complexity and gain flexibility on passing parameters on creation.

Change the Main class accordingly.

Singleton Pattern

The Class Log includes a Method for generating the shortcode on creation. The problem is that even though they are randomly generated, because the maximum number of possible combinations are on high demand, the shortcodes may be duplicated. Delegate that task to a new Class named ShortCodeGenerator in order to ensure the codes are unique.

Make sure the new generator is thread safe.

Part II:

Factory Method

When creating the Files to be used as attachments, in the way it is right now, the Main class must determine which type of object has to be created in compile-time. This reduces flexibility because in a real case scenario, a stream of submissions would come. Therefore, the controller receiving the submissions (Servlet) would not know what type to choose. Implement a Factory for the media Files used for the attachments so its creation can be freely done in run-time.

Change the Main class accordingly.

Get Homework Help Now

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts