You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
The front controller design pattern is used to provide a centralized request handling mechanism so that all requests will be handled by a single handler. This handler can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers. Following are the entities of this type of design pattern.
Front Controller - Single handler for all kinds of requests coming to the application (either web based/ desktop based).
Dispatcher - Front Controller may use a dispatcher object which can dispatch the request to corresponding specific handler.
View - Views are the object for which the requests are made.
Example:
We are going to create aFrontController andDispatcher to act as Front Controller and Dispatcher correspondingly.HomeView andStudentView represent various views for which requests can come to front controller.
FrontControllerPatternDemo, our demo class, will use FrontController to demonstrate Front Controller Design Pattern.
Step 1Create Views.
HomeView.java
publicclassHomeView {publicvoidshow(){System.out.println("Displaying Home Page"); }}
1. Eager initialization: In eager initialization, the instance of Singleton Class is created at the time of class loading.
Example:
publicclassEagerInitializedSingleton {privatestaticfinalEagerInitializedSingletoninstance =newEagerInitializedSingleton();// private constructor to avoid client applications to use constructorprivateEagerInitializedSingleton(){}publicstaticEagerInitializedSingletongetInstance(){returninstance; }}
2. Static block initialization Static block initialization implementation is similar to eager initialization, except that instance of class is created in the static block that provides option for exception handling.
Example:
publicclassStaticBlockSingleton {privatestaticStaticBlockSingletoninstance;privateStaticBlockSingleton (){}// static block initialization for exception handlingstatic{try{instance =newStaticBlockSingleton (); }catch(Exceptione){thrownewRuntimeException("Exception occured in creating Singleton instance"); } }publicstaticStaticBlockSingletongetInstance(){returninstance; }}
3. Lazy Initialization Lazy initialization method to implement Singleton pattern creates the instance in the global access method.
4. Thread Safe Singleton The easier way to create a thread-safe singleton class is to make the global access method synchronized, so that only one thread can execute this method at a time.
5. Bill Pugh Singleton Implementation Prior to Java5, memory model had a lot of issues and above methods caused failure in certain scenarios in multithreaded environment. So, Bill Pugh suggested a concept of inner static classes to use for singleton.
Adapter design pattern is one of the structural design pattern and its used so that two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter.
Example:
we have two incompatible interfaces:MediaPlayer andMediaPackage. MP3 class is an implementation of the MediaPlayer interface and we have VLC and MP4 as implementations of the MediaPackage interface. We want to use MediaPackage implementations as MediaPlayer instances. So, we need to create an adapter to help to work with two incompatible classes.
A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.
classGetPlanFactory {// use getPlan method to get object of type PlanpublicPlangetPlan(StringplanType){if(planType ==null){returnnull; }if(planType.equalsIgnoreCase("DOMESTICPLAN")) {returnnewDomesticPlan(); }elseif(planType.equalsIgnoreCase("COMMERCIALPLAN")){returnnewCommercialPlan(); }elseif(planType.equalsIgnoreCase("INSTITUTIONALPLAN")) {returnnewInstitutionalPlan(); }returnnull; } }
GenerateBill.java
importjava.io.*;classGenerateBill {publicstaticvoidmain(Stringargs[])throwsIOException {GetPlanFactoryplanFactory =newGetPlanFactory();System.out.print("Enter the name of plan for which the bill will be generated: ");BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));StringplanName=br.readLine();System.out.print("Enter the number of units for bill will be calculated: ");intunits=Integer.parseInt(br.readLine());Planp =planFactory.getPlan(planName);// call getRate() method and calculateBill()method of DomesticPaln.System.out.print("Bill amount for "+planName+" of "+units+" units is: ");p.getRate();p.calculateBill(units); } }
Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.
Example: Simple Shopping Cart where we have two payment strategies – using Credit Card or using PayPal.