Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Design Patterns & Principles Lab Manual

Design Patterns & Principles Lab Manual

Published by Teamlease Edtech Ltd (Amita Chitroda), 2022-08-30 10:18:05

Description: Design Patterns & Principles Lab Manual

Search

Read the Text Version

JAVA Design Patterns & FULL STACK Principles DEVELOPMENT

Objectives of this Lab Manual This lab manual includes - Exercises for hands on practice Guidelines to complete the exercise Learning Outcome

Creating an Abstract Factory design pattern

Problem What will you do? Statement Create Abstract Factory Design Pattern for the below-mentioned problem. There must be bike factories at each location. →AsiaBikeFactory, CanadaBikeFactory, and DefaultBikeFactory. Our application should be capable of identifying the location where it is being used. Therefore, we should be able to select the appropriate bike factory implementation even without knowing the implementation. Having the correct factory for a particular location also saves us from incorrect calls. Designing Global Bike Factory. Basically, we need another layer of abstraction to identify the location and internally use the correct car factory implementation without giving the user any hints. An abstract factory pattern solves this exact problem.

Solution What will be the steps Steps to get the output? • Step-1: Firstly, do coding for specifying the bike’s type, model, and location. • Step-2: Then, do coding for specifying Location which is Asia, Canada, and Default. • Step-3: At the end, do coding for specifying bike factories at each location and achieve the required output.

Code Let's write all the bike factories for each location separately. Bike: public abstract class Bike { public Bike(BikeType model, Location location){ this = model; this = location; } protected abstract void construct(); private BikeType model = null; private Location location = null; public BikeType getModel() { return model; } public void setModel(BikeType model) { this = model; } public Location getLocation() { return location; } public void setLocation(Location location) { this = location;

Code Bike: @Override public String toString() { return "Model- "+model + " built in "+location; } } This adds extra work of creating another enum for storing different locations. AsiaBikeFactory: public class AsiaBikeFactory { public static bike buildbike(BikeType model) { Bike bike = null; switch (model) { case lowbudget: bike = new lowbudgetBike(Location); break; case CRUISER: bike = new CruiserBike(Location); break; case LUXURY: bike = new LuxuryBike(Location); break; default: //throw some exception break; } return bike; } } } return bike; } }

Code Location: public enum Location { DEFAULT, Canada, ASIA } All bike types will also have additional location property. I am writing only for a luxury Bike. The same follows for low budget and cruiser also. LuxuryBike: public class LuxuryBike extends Bike { public LuxuryBike(Location location) { super(BikeType, location); construct(); } @Override protected void construct() { System.out("Building luxury bike"); //add accessories } } So for we have created basic classes. Now lets have different car factories.

Code The 3 different bike factories are now all in place. The next step is to abstract the way these factories are accessed. Let's take a look at how? public class BikeFactory { private BikeFactory() { //Prevent instantiation } public static Bike buildBike(BikeType type) { Bike bike = null; Location location = Location; //Read location property somewhere from configuration //Use location specific bike factory switch(location) { case CANADA: bike = CANADABikeFactory(type); break; case ASIA: bike = AsiaBikeFactory(type); break; default: bike = DefaultBikeFactory(type); } return bike; } } We are done with writing code. Now let’s test what we have written till now. public class TestFactoryPattern { public static void main(String[] args) { System.out(BikeFactory(BikeType)); System.out(BikeFactory(BikeType)); System.out(BikeFactory(BikeType)); } }

Output Output for the Adedxetercxitsheewreil:l- be :- Output: (Default location is Asia) Building lowbudget bike Model- LOWBUDGET built in ASIA Building cruiser bike Model- CRUISER built in ASIA Building luxury bike Model- LUXURY built in ASIA

Learning Outcome After Performing this Add teExxt ehrecries:e- You will be able to apply the Abstract Factory pattern.

Happy Learning!


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook