Skip to main content

Prototype Design Pattern

Prototype Design Pattern is a creational design pattern which will be used when there is a need to create multiple object with the same class and will be involved in lot of configurations. By using Prototype Design pattern we can clone a existing object with same configurations values, with same property values into a different memory location, that will exclude the costly operations. This will save us both time and resources.


When to use? and Why?

  • Creating new objects by cloning other objects.
  • Allows for adding any subclass instance of a known super class at run-time
  • when there are numerous potential classes that you want to only use if needed at run-time.
  • Reduce the need for creating sub classes



public interface Shape extends Cloneable {
           public Shape makeCopy();
}

public class Circle implements Shape {
           public Circle() {
                      System.out.println("Circle is made");
           }
           public Shape makeCopy() {
                      System.out.println("Circle is being made");
                      Circle circle = null;
                      try {
                                circle = (Circle)super.clone();
                      } catch (CloneNotSupportedException e) {
                                e.printStackTrace();
                      }
                      return circle;
           }
}

 public class TestPrototype {
            public static void main(String args[]) {
                 Circle circle = new Circle();
                 Circle clonedCircle = (Circle)circle.makeCopy();
                 System.out.println(circle);
                 System.out.println(clonedCircle);
                 System.out.println("Circle hashcode: " + System.identityHashCode(circle));
                System.out.println("Cloned Circle hashcode: " + System.identityHashCode(clonedCircle));
           }
}


 Output :
               Circle is made
               Circle is being made
               Prototype.Circle@2d98a335
               Prototype.Circle@16b98e56
               Circle hashcode: 764977973
               Cloned Circle hashcode: 381259350

 As you can see only one instance of a object is created. and other is just copied with same fields to different location . instead of creating a new object.

Advantages of Prototype Design Pattern


  • Adding and removing products at run-time – Prototypes let you incorporate a new concrete product class into a system simply by registering a prototypical instance with the client. That’s a bit more flexible than other creational patterns, because a client can install and remove prototypes at run-time.
  • Specifying new objects by varying values – Highly dynamic systems let you define new behavior through object composition by specifying values for an object’s variables and not by defining new classes.
  • Specifying new objects by varying structure – Many applications build objects from parts and subparts. For convenience, such applications often let you instantiate complex, user-defined structures to use a specific subcircuit again and again.
  • Reduced subclassing – Factory Method often produces a hierarchy of Creator classes that parallels the product class hierarchy. The Prototype pattern lets you clone a prototype instead of asking a factory method to make a new object. Hence you don’t need a Creator class hierarchy at all.

Disadvantages of Prototype Design Pattern


  • Overkill for a project that uses very few objects and/or does not have an underlying emphasis on the extension of prototype chains.
  • It also hides concrete product classes from the client
  • Each subclass of Prototype must implement the clone() operation which may be difficult, when the classes under consideration already exist. Also implementing clone() can be difficult when their internals include objects that don’t support copying or have circular references.

Reference : https://www.geeksforgeeks.org/prototype-design-pattern/

Comments

Popular posts from this blog

Why S.O.L.I.D

Why S.O.L.I.D SOLID principles are design principles that enable us manage most of the software design problems. Objective of SOLID is to make software design more understandable, flexible and maintainable. Why SOLID was promoted Rigidity - Every change affects many other parts Fragility - Things breaks in unrelated places Immobility - Cannot reuse the code outside of its origin context Because of these we ended up with these  End up with tight or strong coupling of the code with many other modules. Tight coupling causes time to implement any new requirements, bug fixes and some times creates unknown issues End up with code which is not testable End up with duplication of code End up creating new bugs when changing the code If followed SOLID Achieve reduction in complexity of the code Increase readability, extensible and maintainability Reduce errors and implement re-usability Reduce tight coupling S.O.L.I.D Principles Single Responsibility...

Redirect to a url after login - Spring Security

If you wa nt the browser to remember the url that you enter before login and redirect to that url after you login to the application.  XML Configuration - spring-security.xml   What happens after login will be handled by AuthenticationSuccessHandler SavedRequestAwareAuthenticationSuccessHandler will use the saved request stored in the session. After the successfull login the user will be redirected to the original request url.