Service Layer Example

This program demonstrates service layer in architecture.

Problem Statement

Write a Java program where ServiceLayer mediates between presentation and data layers.

Source Code

1 class DataLayer {
2 String getData(){ return "Data from DB"; }
3 }
4 class ServiceLayer {
5 DataLayer dl=new DataLayer();
6 String process(){ return "Processed: "+dl.getData(); }
7 }
8 class PresentationLayer {
9 ServiceLayer sl=new ServiceLayer();
10 void show(){ System.out.println(sl.process()); }
11 }
12 public class ServiceLayerExample {
13 public static void main(String[] args) {
14 System.out.println("Eduinq Service Layer Example");
15 PresentationLayer pl=new PresentationLayer();
16 pl.show();
17 }
18 }

Program Output

Eduinq Service Layer Example
Processed: Data from DB

Explanation

Service layer acts as mediator between presentation and data. - Presentation interacts only with Service, not directly with Data. - This separation improves modularity, scalability, and testability in layered architecture. Conclusion: Shows how to implement service layer in architecture.

Quick Links to Explore