-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
192 lines (154 loc) · 4.87 KB
/
Main.java
File metadata and controls
192 lines (154 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Software Design and Architecture - Tutorial 1
Java code made by Bralyn Loach-Perry (Student ID: 100867075)
CRN 45740
Java code creates examples using each of the main 5 principles by Martin to understand how they work
The Other classes and objects are located below the main class
*/
public class Main {
public static void main(String[] args) {
// 1. Single Responsibility Principle
Book book = new Book("A Thousand Splendid Suns", "Khaled Hosseini");
BookPrinter bookPrinter = new BookPrinter();
bookPrinter.printBookDetails(book);
// 2. Open-Closed Principle
Payment payment = new CreditCardPayment();
payment.makePayment(100.0);
payment = new PayPalPayment();
payment.makePayment(200.0);
// 3. Liskov Substitution Principle
Bird sparrow = new Sparrow();
sparrow.fly();
try {
Bird ostrich = new Ostrich();
ostrich.fly(); // Will throw an exception
} catch (UnsupportedOperationException e) {
System.out.println("Ostrich cannot fly.");
}
NonFlyingBird ostrichCorrect = new CorrectedOstrich();
ostrichCorrect.fly(); // Now correctly shows that ostrich can't fly
// 4. Interface Segregation Principle
Restaurant dineIn = new DineInRestaurant();
dineIn.prepareMeal();
OnlineRestaurant online = new OnlineRestaurant();
online.prepareMeal();
online.deliverMeal();
// 5. Dependency Inversion Principle
DataReader fileReader = new FileReader();
DataProcessor fileProcessor = new DataProcessor(fileReader);
fileProcessor.process();
DataReader dbReader = new DatabaseReader();
DataProcessor dbProcessor = new DataProcessor(dbReader);
dbProcessor.process();
}
}
// 1. Single Responsibility Principle (SRP)
class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
}
// Class responsible for printing book details
class BookPrinter {
public void printBookDetails(Book book) {
System.out.println("Book: " + book.getTitle() + " by " + book.getAuthor());
}
}
// 2. Open-Closed Principle (OCP)
abstract class Payment {
public abstract void makePayment(double amount);
}
class CreditCardPayment extends Payment {
@Override
public void makePayment(double amount) {
System.out.println("Payment of $" + amount + " made with Credit Card.");
}
}
class PayPalPayment extends Payment {
@Override
public void makePayment(double amount) {
System.out.println("Payment of $" + amount + " made with PayPal.");
}
}
// 3. Liskov Substitution Principle (LSP)
class Bird {
public void fly() {
System.out.println("Flying...");
}
}
class Sparrow extends Bird {
// Sparrow can still fly, adhering to Liskov Substitution Principle
}
class Ostrich extends Bird {
@Override
public void fly() {
throw new UnsupportedOperationException("Ostriches cannot fly");
}
}
// Correcting LSP
class NonFlyingBird extends Bird {
@Override
public void fly() {
System.out.println("Cannot fly.");
}
}
class CorrectedOstrich extends NonFlyingBird {
// Now an ostrich does not violate LSP by correctly inheriting from NonFlyingBird
}
// 4. Interface Segregation Principle (ISP)
interface Restaurant {
void prepareMeal();
}
interface DeliveryService {
void deliverMeal();
}
class DineInRestaurant implements Restaurant {
@Override
public void prepareMeal() {
System.out.println("Meal is prepared for dine-in.");
}
}
class OnlineRestaurant implements Restaurant, DeliveryService {
@Override
public void prepareMeal() {
System.out.println("Meal is prepared for delivery.");
}
@Override
public void deliverMeal() {
System.out.println("Meal is delivered.");
}
}
// 5. Dependency Inversion Principle (DIP)
interface DataReader {
String readData();
}
class FileReader implements DataReader {
@Override
public String readData() {
return "Reading data from file...";
}
}
class DatabaseReader implements DataReader {
@Override
public String readData() {
return "Reading data from database...";
}
}
class DataProcessor {
private DataReader dataReader;
public DataProcessor(DataReader dataReader) {
this.dataReader = dataReader;
}
public void process() {
System.out.println(dataReader.readData());
}
}