Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit eaffd3d

Browse files
committed
Initial commit.
1 parent 1cf0e11 commit eaffd3d

File tree

18 files changed

+958
-0
lines changed

18 files changed

+958
-0
lines changed

design-patterns [complete]/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.yuelchen</groupId>
4+
<artifactId>design-patterns</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>practice-mathematics</name>
7+
<description>A project for reviewing design patterns.</description>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<configuration>
15+
<source>1.13</source>
16+
<target>1.13</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.13.1</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.yuelchen.command;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class MarketMaker {
7+
8+
/**
9+
* The Singleton object for MarketMaker.
10+
*/
11+
public static MarketMaker marketMaker;
12+
13+
//====================================================================================================
14+
15+
/**
16+
* A queue of order objects.
17+
*/
18+
private Queue<Order> orders;
19+
20+
//====================================================================================================
21+
22+
/**
23+
* Private class constructor.
24+
*/
25+
private MarketMaker() {
26+
this.orders = new LinkedList<>();
27+
}
28+
29+
//====================================================================================================
30+
31+
/**
32+
* Singleton method, returns the MarketMaker object.
33+
*
34+
* @return A MarketMaker object.
35+
*/
36+
public static MarketMaker getInstance() {
37+
if(marketMaker == null) {
38+
marketMaker = new MarketMaker();
39+
}
40+
41+
return marketMaker;
42+
}
43+
44+
//====================================================================================================
45+
46+
/**
47+
* Adds the given order to the queue of orders.
48+
*
49+
* @param order The order to be added.
50+
*/
51+
public void place(Order order) {
52+
this.orders.add(order);
53+
}
54+
55+
//====================================================================================================
56+
57+
/**
58+
* Removes the given order from the queue of orders if it exists and returns true,
59+
* otherwise returns false.
60+
*
61+
* @param order The order to be removed.
62+
*
63+
* @return True if the order was removed, otherwise returns false.
64+
*/
65+
public boolean remove(Order order) {
66+
if(this.orders.contains(order)) {
67+
this.orders.remove(order);
68+
return true;
69+
}
70+
71+
return false;
72+
}
73+
74+
//====================================================================================================
75+
76+
/**
77+
* Executes all orders in queue and clears it.
78+
*/
79+
public void execute() {
80+
for(Order order : this.orders) {
81+
order.execute();
82+
}
83+
84+
this.orders.clear();
85+
}
86+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.yuelchen.command;
2+
3+
public interface Order {
4+
5+
/**
6+
* Executes the order.
7+
*/
8+
public void execute();
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.yuelchen.command;
2+
3+
public class OrderBuy implements Order {
4+
5+
/**
6+
* The trade object for this buy order.
7+
*/
8+
private Trade trade;
9+
10+
//====================================================================================================
11+
12+
/**
13+
* Class constructor.
14+
*
15+
* @param trade The trade object.
16+
*/
17+
public OrderBuy(Trade trade) {
18+
this.trade = trade;
19+
}
20+
21+
//====================================================================================================
22+
23+
/**
24+
* Executes the buy option for trade order.
25+
*/
26+
public void execute() {
27+
this.trade.buy();
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.yuelchen.command;
2+
3+
public class OrderSell implements Order {
4+
5+
/**
6+
* The trade object for this sell order.
7+
*/
8+
private Trade trade;
9+
10+
//====================================================================================================
11+
12+
/**
13+
* Class constructor.
14+
*
15+
* @param trade The trade object.
16+
*/
17+
public OrderSell(Trade trade) {
18+
this.trade = trade;
19+
}
20+
21+
//====================================================================================================
22+
23+
/**
24+
* Executes the sell option for trade order.
25+
*/
26+
public void execute() {
27+
this.trade.sell();
28+
}
29+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.yuelchen.command;
2+
3+
public class Trade {
4+
5+
/**
6+
* The ticker symbol of the stock for this trade.
7+
*/
8+
private String tickerSymbol;
9+
10+
//====================================================================================================
11+
12+
/**
13+
* The number of shares for this trade.
14+
*/
15+
private double shares;
16+
17+
//====================================================================================================
18+
19+
/**
20+
* Class constructor.
21+
*
22+
* @param tickerSymbol The stock ticker symbol.
23+
* @param shares The number of shares.
24+
*/
25+
public Trade(String tickerSymbol, double shares) {
26+
this.tickerSymbol = tickerSymbol;
27+
this.shares = shares;
28+
}
29+
30+
//====================================================================================================
31+
32+
/**
33+
* Returns the ticker symbol of the stock for this trade.
34+
*
35+
* @return The stock ticker symbol.
36+
*/
37+
public String getTickerSymbol() {
38+
return this.tickerSymbol;
39+
}
40+
41+
//====================================================================================================
42+
43+
/**
44+
* Returns the number of shares for this trade.
45+
*
46+
* @return The number of shares.
47+
*/
48+
public double getShares() {
49+
return this.shares;
50+
}
51+
52+
//====================================================================================================
53+
54+
/**
55+
* Executes this trade as a purchase (buy).
56+
*/
57+
public void buy() {
58+
System.out.println(String.format("Buying %d shares of %s", this.shares, this.tickerSymbol));
59+
}
60+
61+
//====================================================================================================
62+
63+
/**
64+
* Executes this trade as a sale (sell).
65+
*/
66+
public void sell() {
67+
System.out.println(String.format("Selling %d shares of %s", this.shares, this.tickerSymbol));
68+
}
69+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.yuelchen.factory;
2+
3+
public class Factory {
4+
5+
/**
6+
* Returns a Shape if the given array of values meets a shape criteria.
7+
*
8+
* Array Length to Shape Name Relations:
9+
* - 1 equals Square
10+
* - 2 equals Rectangle
11+
* - 3 equals Triangle
12+
*
13+
* Otherwise returns null.
14+
*
15+
* @param values The array of values to be assigned to a known Shape.
16+
*
17+
* @return A Shape object if criteria is met, or otherwise returns null.
18+
*/
19+
public static Shape getShape(double[] values) {
20+
if(values.length == 1) {
21+
return new ShapeSquare(values[0]);
22+
} else if(values.length == 2) {
23+
return new ShapeRectangle(values[0], values[1]);
24+
} else if(values.length == 3) {
25+
return new ShapeTriangle(values[0], values[1], values[3]);
26+
}
27+
28+
return null;
29+
}
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.yuelchen.factory;
2+
3+
public interface Shape {
4+
5+
/**
6+
* Determines and returns the perimeter of the Shape.
7+
*
8+
* @return The perimeter of the Shape.
9+
*/
10+
public double getPerimeter();
11+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.yuelchen.factory;
2+
3+
public class ShapeRectangle implements Shape {
4+
5+
/**
6+
* The length of a rectangle.
7+
*/
8+
private double length;
9+
10+
//====================================================================================================
11+
12+
/**
13+
* The width of a rectangle.
14+
*/
15+
private double width;
16+
17+
//====================================================================================================
18+
19+
/**
20+
* Class constructor.
21+
*
22+
* @param length The length of the rectangle.
23+
* @param width The width of the rectangle.
24+
*/
25+
public ShapeRectangle(double length, double width) {
26+
this.length = length;
27+
this.width = width;
28+
}
29+
30+
//====================================================================================================
31+
32+
/**
33+
* Returns the length of the rectangle.
34+
*
35+
* @return The length of the rectangle.
36+
*/
37+
public double getLength() {
38+
return this.length;
39+
}
40+
41+
//====================================================================================================
42+
43+
/**
44+
* Returns the width of the rectangle.
45+
*
46+
* @return The width of the rectangle.
47+
*/
48+
public double getWidth() {
49+
return this.width;
50+
}
51+
52+
//====================================================================================================
53+
54+
/**
55+
* Determines and returns the perimeter of the rectangle.
56+
*
57+
* @return The perimeter of the rectangle.
58+
*/
59+
public double getPerimeter() {
60+
return 2 * (this.length + this.width);
61+
}
62+
63+
//====================================================================================================
64+
65+
/**
66+
* Determines and returns the area if the rectangle.
67+
*
68+
* @return The area of the rectangle.
69+
*/
70+
public double getArea() {
71+
return this.length * this.width;
72+
}
73+
}

0 commit comments

Comments
 (0)