2. Programming Assignment: Design 4 classes: MyController to control the executi
ID: 3747005 • Letter: 2
Question
2. Programming Assignment: Design 4 classes: MyController to control the execution of the example, EampleFactory (a singleton) to create a Service object, . a Service class interface named IAService, a Service class named AService the implements IAService. Have MyController construct the ExampleFactory as a singleton as in program 1 Have MyController use the ExampleFactory method getService() to create an instance of AService. Have MyController use the method ProvideService() in AService to print "T'm now providing the service for MyController"Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
IAService.java
--------------
public interface IAService {
public void provideService();
}
AService.java
------------
public class AService implements IAService {
@Override
public void provideService() {
System.out.println("I am now porviding the service for MyController");
}
}
ExampleFactory.java
--------
public class ExampleFactory {
private static ExampleFactory instance = null;
public static ExampleFactory getInstance(){
if(instance == null){
System.out.println("Constructing ExampleFactory");
instance = new ExampleFactory();
}
else {
System.out.println("ExampleFactory already exists");
}
return instance;
}
public IAService getService(){
return new AService();
}
}
MyController.java
--------
public class MyController {
public static void main(String[] args) {
IAService service = ExampleFactory.getInstance().getService();
service.provideService();
}
}
output
------
Constructing ExampleFactory
I am now porviding the service for MyController
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.