Skip to content

Commit db8e6ce

Browse files
committed
Updated Readme, added screenshots & sample outputs
1 parent 2362d34 commit db8e6ce

28 files changed

+295
-7
lines changed

README.md

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Object-oriented design and implementation of a Design Pattern Code Generator.
22
This project is the implementation of a program that generates the implementation code of the 23 Gang of Four(GoF) design patterns, as presented in the book “Design Patterns: Elements of Reusable Object-Oriented Software”.
33

4+
------
5+
46
## Index
57

68
1. About Design Patterns
@@ -45,6 +47,8 @@ The 23 Design Patterns are classified into 3 main categories:
4547
* Template
4648
* Visitor
4749

50+
------
51+
4852
#### 2. About JavaPoet
4953

5054
[JavaPoet](https://github.com/square/javapoet), a successor to [JavaWriter](https://github.com/square/javapoet/tree/javawriter_2), is a Java API for generating .java source files. It can generate primitive types, reference types (like classes, interfaces, enumerated types, anonymous inner classes), fields, methods, parameters, annotations, and Javadocs.
@@ -90,6 +94,8 @@ JavaPoet offers models for classes & interfaces (`TypeSpec`), fields (`FieldSpec
9094

9195
The most recent version of JavaPoet available as of now is 1.12.1.
9296

97+
------
98+
9399
#### 3. Application Design
94100

95101
To implement Design Pattern Code Generator(DePaCoG), I have employed the **Factory Method**, **Template Method** and **Singleton** design patterns.
@@ -244,6 +250,14 @@ public class DePaCoG {
244250
* Sometimes singleton pattern can hide bad code design
245251
* It requires modifications to be used in a multi threaded environment to make sure multiple instances are not created by multiple threads
246252

253+
------
254+
255+
The image below shows a class diagram for the complete application. For simplicity I have selected only 3 design pattern classes (each representing its category of design patterns) out of the 23.
256+
257+
![](C:\Users\Samujjwaal Dey\Desktop\CS 474 OOLE\homework1\screenshots\DePaCoG.png)
258+
259+
The class `DePaCoG` has the main() method in it. It creates an instance of class `Hw1DesignPatternGenerator` (concrete implementation of `DesignPatternGenerator` ). Methods here are for displaying menu of design patterns and getting choice of design pattern from user. Depending on user’s choice the corresponding design pattern generating class (implementation of ``DesignPattern`) is instantiated to generate the design pattern source code.
260+
247261

248262

249263
#### 4. Test Cases
@@ -256,7 +270,7 @@ There are 2 test classes `DesignPatternTest` and `Hw1DesignPatternGeneratorTest`
256270
* Test Case to verify `generateCode()` returns `JavaFile[]` object
257271
* Test Case to verify the generated java files are written successfully in the output folder.
258272

259-
273+
------
260274

261275
#### 5. Instructions to Execute
262276

@@ -270,21 +284,27 @@ For command line execution, locate `hw1-assembly-0.1.jar` at `target\scala-2.13`
270284

271285
On Windows, the command to run the jar file: `java -jar hw1-assembly-0.1.jar mode`
272286

273-
* Executing using default config file, mode = 0:
287+
* Executing using default config file, then `mode = 0`:
288+
274289
* run `java -jar hw1-assembly-0.1.jar 0`
275290

276-
* Executing using custom inputs from user, mode = 1:
291+
![](C:\Users\Samujjwaal Dey\Desktop\CS 474 OOLE\homework1\screenshots\Capture4.PNG)
292+
293+
* Executing using custom inputs from user, then `mode = 1`:
294+
277295
* run `java -jar hw1-assembly-0.1.jar 1`
278296

297+
![](C:\Users\Samujjwaal Dey\Desktop\CS 474 OOLE\homework1\screenshots\Capture2.PNG)
298+
279299
For execution in IntelliJ, use `run 0` or `run 1` for the preferred way of execution (argument passed has similar purpose like in the command line method)
280300

281301
The output java files are saved under the package name provided in the config file (or as custom input) in a folder named `outputs`. The `outputs` folder is created in the current working directory during execution.
282302

283-
303+
------
284304

285305
#### 6. Results of Execution
286306

287-
While executing the Design Pattern Code Generator, the main() method in class DePaCoG is executed along with a command line argument (0 or 1). The command line argument is to select whether inputs will be entered via a configuration file or via inputs through the interactive environment.
307+
While executing the Design Pattern Code Generator, the main() method in class `DePaCoG` is executed along with a command line argument (`0` or `1`). The command line argument is to select whether inputs will be entered via a configuration file or via inputs through the interactive environment.
288308

289309
Here is an example of config values to create Abstract Factory design pattern.
290310

@@ -298,9 +318,17 @@ Here is an example of config values to create Abstract Factory design pattern.
298318
}
299319
```
300320

321+
For above config input, the output files will be saved at `outputs/com/laptopAbstractFactory`.
322+
323+
The classes created are : `Processor.java, Intel.java, AMD.java, OperatingSystem.java, OS.java, ChromeOS.java, Ubuntu.java, LaptopFactory.java, ChromeBookFactory.java,LinuxLaptopFactory.java`
324+
325+
The image below shows the class diagram of the classes generated after executing the config input values.
326+
327+
![](C:\Users\Samujjwaal Dey\Desktop\CS 474 OOLE\homework1\screenshots\laptopFactory.png)
328+
301329
In addition to Abstract Factory, the `default.conf` configuration file also contains inputs for Decorator, Adapter and Observer design patterns.
302330

303-
For instance in above config input, the output files will be saved at `outputs/com/laptopAbstractFactory`. The `outputs` folder is created in the current working directory during execution.
331+
The `outputs` folder is created in the current working directory during execution.
304332

305-
The classes created are : Processor.java, Intel.java, AMD.java, OperatingSystem.java, OS.java, ChromeOS.java, Ubuntu.java, LaptopFactory.java, ChromeBookFactory.java,LinuxLaptopFactory.java
333+
<img src="C:\Users\Samujjwaal Dey\Desktop\CS 474 OOLE\homework1\screenshots\Capture.PNG" style="zoom:75%;" />
306334

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.coffeeDecorator;
2+
3+
/**
4+
* Component, defines interface for new features which will be added dynamically
5+
*/
6+
public interface Beverage {
7+
void operation();
8+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coffeeDecorator;
2+
3+
/**
4+
* ConcreteComponent, define object where new features can be added
5+
*/
6+
public class Coffee implements Beverage {
7+
public void operation() {
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coffeeDecorator;
2+
3+
/**
4+
* Decorator, keep reference to Component object
5+
*/
6+
abstract class CondimentDecorator implements Beverage {
7+
protected Beverage component;
8+
9+
public abstract void operation();
10+
11+
public void setComponent(Beverage component) {
12+
this.component = component;
13+
}
14+
}

outputs/com/coffeeDecorator/Milk.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.coffeeDecorator;
2+
3+
/**
4+
* ConcreteDecoratorA, add features to component
5+
*/
6+
public class Milk extends CondimentDecorator {
7+
private boolean state;
8+
9+
public void operation() {
10+
state = true;
11+
this.component.operation();
12+
}
13+
14+
public boolean isState() {
15+
return state;
16+
}
17+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.coffeeDecorator;
2+
3+
/**
4+
* ConcreteDecoratorB, add features to component
5+
*/
6+
public class WhipCream extends CondimentDecorator {
7+
private boolean behaviorMethodInvoked = false;
8+
9+
public void operation() {
10+
this.component.operation();
11+
addedBehavior();
12+
}
13+
14+
private void addedBehavior() {
15+
behaviorMethodInvoked = true;
16+
}
17+
18+
protected boolean isBehaviorMethodInvoked() {
19+
return behaviorMethodInvoked;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* ProductA2, implements AbstractProductA interface
5+
*/
6+
public class AMD implements Processor {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* ConcreteFactory2, implements creation of the concrete Product2 objects
5+
*/
6+
public class ChromeBookFactory implements Ubuntu {
7+
public Processor createProcessor() {
8+
return new AMD();
9+
}
10+
11+
public OperatingSystem createOperatingSystem() {
12+
return new ChromeOS();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* ProductB1, implements AbstractProductB interface
5+
*/
6+
public class ChromeOS implements OperatingSystem {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* ProductA1, implements AbstractProductA interface
5+
*/
6+
public class Intel implements Processor {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* ConcreteFactory1, implements creation of the concrete Product1 objects
5+
*/
6+
public class LaptopFactory implements Ubuntu {
7+
public Processor createProcessor() {
8+
return new Intel();
9+
}
10+
11+
public OperatingSystem createOperatingSystem() {
12+
return new OS();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* ProductB1, implements AbstractProductB interface
5+
*/
6+
public class OS implements OperatingSystem {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* AbstractProductB defines interface for ProductB objects
5+
*/
6+
public interface OperatingSystem {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* AbstractProductA defines interface for ProductA objects
5+
*/
6+
public interface Processor {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.laptopAbstractFactory;
2+
3+
/**
4+
* Abstract Factory, defines interface for creation of the abstract product objects
5+
*/
6+
public interface Ubuntu {
7+
Processor createProcessor();
8+
9+
OperatingSystem createOperatingSystem();
10+
}

outputs/com/usbAdapter/MicroUSB.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.usbAdapter;
2+
3+
/**
4+
* Adaptee class, interface which will be adapted
5+
*/
6+
public class MicroUSB {
7+
public String specialRequest() {
8+
return "specialRequest";
9+
}
10+
}

outputs/com/usbAdapter/TypeC.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.usbAdapter;
2+
3+
/**
4+
* Target interface, defines domain-specific interface to which Adaptee will be adapted
5+
*/
6+
public interface TypeC {
7+
String request();
8+
}

outputs/com/usbAdapter/USBDongle.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.usbAdapter;
2+
3+
/**
4+
* Adapter class, adapts Adaptee to the Target interface
5+
*/
6+
public class USBDongle implements TypeC {
7+
private MicroUSB adaptee;
8+
9+
public USBDongle(MicroUSB adaptee) {
10+
this.adaptee = adaptee;
11+
}
12+
13+
public String request() {
14+
return adaptee.specialRequest();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.weatherObserver;
2+
3+
/**
4+
* ConcreteObserver maintains a reference to a ConcreteSubject object, stores
5+
* state that should stay consistent with the subject's, implements the Observer
6+
* updating interface to keep its state consistent with the subject's.
7+
*/
8+
public class AndroidPhone implements SmartPhone {
9+
private int observerState;
10+
11+
private DarkSkyServer subject;
12+
13+
public AndroidPhone(DarkSkyServer subject) {
14+
this.subject = subject;
15+
}
16+
17+
public void update() {
18+
observerState = subject.getState();
19+
}
20+
21+
protected int getObserverState() {
22+
return observerState;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.weatherObserver;
2+
3+
/**
4+
* ConcreteSubject stores state of interest to ConcreteObserver objects, sends a notification to its observers
5+
* when its state changes.
6+
*/
7+
public class DarkSkyServer extends WeatherServiceServer {
8+
private int state;
9+
10+
public int getState() {
11+
return state;
12+
}
13+
14+
public void setState(int state) {
15+
this.state = state;
16+
this.notifyObservers();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.weatherObserver;
2+
3+
/**
4+
* Observer defines an updating interface for objects that should be notified of changes in a subject.
5+
*/
6+
public interface SmartPhone {
7+
void update();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.weatherObserver;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
/**
8+
* Subject knows its observers. Any number of Observer objects may observe a subject.
9+
*/
10+
abstract class WeatherServiceServer {
11+
private List<SmartPhone> observers = new ArrayList<SmartPhone>();
12+
13+
public void attach(SmartPhone observer) {
14+
observers.add(observer);
15+
}
16+
17+
public void detach(SmartPhone observer) {
18+
observers.remove(observer);
19+
}
20+
21+
public void notifyObservers() {
22+
(Iterator iterator = observers.iterator(); iterator.hasNext();) {
23+
SmartPhone observer = (SmartPhone) iterator.next();
24+
observer.update();
25+
}
26+
}
27+
}

screenshots/Capture.PNG

11.8 KB
Loading

screenshots/Capture2.PNG

65.4 KB
Loading

screenshots/Capture3.PNG

53 KB
Loading

screenshots/Capture4.PNG

84.9 KB
Loading

screenshots/DePaCoG.png

40.2 KB
Loading

screenshots/laptopFactory.png

22 KB
Loading

0 commit comments

Comments
 (0)