Skip to content

Commit bdb7e9f

Browse files
committed
Add Builder, Prototype, Command, Observer, Adapter, Filter and modify AbstractFactory, Factory, FactoryMethod
1 parent eda9882 commit bdb7e9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+812
-300
lines changed

.classpath

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" output="target/classes" path="src/main/java">
4-
<attributes>
5-
<attribute name="optional" value="true"/>
6-
<attribute name="maven.pomderived" value="true"/>
7-
</attributes>
8-
</classpathentry>
9-
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10-
<attributes>
11-
<attribute name="optional" value="true"/>
12-
<attribute name="maven.pomderived" value="true"/>
13-
</attributes>
14-
</classpathentry>
15-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
16-
<attributes>
17-
<attribute name="maven.pomderived" value="true"/>
18-
</attributes>
19-
</classpathentry>
20-
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21-
<attributes>
22-
<attribute name="maven.pomderived" value="true"/>
23-
</attributes>
24-
</classpathentry>
25-
<classpathentry kind="output" path="target/classes"/>
26-
</classpath>
3+
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
5+
<classpathentry kind="output" path="target/classes"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
7+
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
8+
</classpath>

.project

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>designpatterns</name>
4-
<comment></comment>
5-
<projects>
6-
</projects>
7-
<buildSpec>
8-
<buildCommand>
9-
<name>org.eclipse.jdt.core.javabuilder</name>
10-
<arguments>
11-
</arguments>
12-
</buildCommand>
13-
<buildCommand>
14-
<name>org.eclipse.m2e.core.maven2Builder</name>
15-
<arguments>
16-
</arguments>
17-
</buildCommand>
18-
</buildSpec>
19-
<natures>
20-
<nature>org.eclipse.jdt.core.javanature</nature>
21-
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22-
</natures>
23-
</projectDescription>
3+
<name>designpatterns</name>
4+
<comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
5+
<projects/>
6+
<buildSpec>
7+
<buildCommand>
8+
<name>org.eclipse.jdt.core.javabuilder</name>
9+
</buildCommand>
10+
<buildCommand>
11+
<name>org.eclipse.m2e.core.maven2Builder</name>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
17+
</natures>
18+
</projectDescription>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.tctam.designpatterns.behavioral.command;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Broker {
7+
private List<Command> commands = new ArrayList<Command>();
8+
9+
public void addComamnd(Command c) {
10+
commands.add(c);
11+
}
12+
13+
public void execute() {
14+
for (Command c : commands) {
15+
c.execute();
16+
}
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.tctam.designpatterns.behavioral.command;
2+
3+
public class BuyCommand implements Command {
4+
5+
private Request request;
6+
7+
public BuyCommand(Request r) {
8+
request = r;
9+
}
10+
11+
public void execute() {
12+
request.buy();
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.tctam.designpatterns.behavioral.command;
2+
3+
public class Client {
4+
public static void main(String[] args) {
5+
Request r = new Request();
6+
7+
Command buy = new BuyCommand(r);
8+
Command sell = new SellCommand(r);
9+
10+
Broker b = new Broker();
11+
b.addComamnd(buy);
12+
b.addComamnd(sell);
13+
14+
b.execute();
15+
}
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.tctam.designpatterns.behavioral.command;
2+
public interface Command {
3+
public void execute();
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.tctam.designpatterns.behavioral.command;
2+
3+
public class Request {
4+
5+
public void buy() {
6+
System.out.println("BUY");
7+
}
8+
9+
public void sell() {
10+
System.out.println("SELL");
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.tctam.designpatterns.behavioral.command;
2+
3+
public class SellCommand implements Command {
4+
5+
private Request request;
6+
7+
public SellCommand(Request r) {
8+
request = r;
9+
}
10+
11+
public void execute() {
12+
request.sell();
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.tctam.designpatterns.creational.abstractfactory;
2+
3+
public interface AbstractFactory {
4+
public Shape getShape(String name);
5+
6+
public Color getColor(String name);
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.tctam.designpatterns.creational.abstractfactory;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class AbstractFactoryProducer {
7+
public static final String NAME = AbstractFactoryProducer.class.getName();
8+
9+
private static Map<String, AbstractFactory> factories = new HashMap<String, AbstractFactory>();
10+
11+
static {
12+
factories.put(ShapeFactory.NAME, new ShapeFactory());
13+
factories.put(ColorFactory.NAME, new ColorFactory());
14+
}
15+
16+
public static AbstractFactory getFactory(String name) {
17+
return factories.get(name);
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.tctam.designpatterns.creational.abstractfactory;
2+
3+
public class BlueColor implements Color {
4+
public static final String NAME = BlueColor.class.getName();
5+
6+
public void fill() {
7+
System.out.println(NAME);
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.tctam.designpatterns.creational.abstractfactory;
2+
3+
public class Circle implements Shape {
4+
public static final String NAME = Circle.class.getName();
5+
6+
public void draw() {
7+
System.out.println(NAME);
8+
}
9+
}
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package org.tctam.designpatterns.creational.abstractfactory;
22

33
public class Client {
4-
public static void main(String args[]) {
5-
ProductFactory productAFactory = ProductFactoryProducer.getProductFactory(ProductAFactory.NAME);
6-
ProductA productA1 = productAFactory.getProductA(ProductA1.NAME);
7-
productA1.operationA();
8-
ProductA productA2 = productAFactory.getProductA(ProductA2.NAME);
9-
productA2.operationA();
10-
11-
ProductFactory productBFactory = ProductFactoryProducer.getProductFactory(ProductBFactory.NAME);
12-
ProductB productB1 = productBFactory.getProductB(ProductB1.NAME);
13-
productB1.operationB();
14-
ProductB productB2 = productBFactory.getProductB(ProductB2.NAME);
15-
productB2.operationB();
16-
}
4+
public static void main(String args[]) {
5+
AbstractFactory colorFactory = AbstractFactoryProducer.getFactory(ColorFactory.NAME);
6+
Color blue = colorFactory.getColor(BlueColor.NAME);
7+
System.out.println(blue.toString());
8+
9+
AbstractFactory shapeFactory = AbstractFactoryProducer.getFactory(ShapeFactory.NAME);
10+
Shape circle = shapeFactory.getShape(Circle.NAME);
11+
System.out.println(circle.toString());
12+
}
1713
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.tctam.designpatterns.creational.abstractfactory;
2+
3+
public interface Color {
4+
public abstract void fill();
5+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.tctam.designpatterns.creational.abstractfactory;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class ColorFactory implements AbstractFactory{
7+
public static final String NAME = ColorFactory.class.getName();
8+
9+
private Map<String, Class> colors = new HashMap<String, Class>();
10+
11+
{
12+
colors.put(RedColor.NAME, RedColor.class);
13+
colors.put(GreenColor.NAME, GreenColor.class);
14+
colors.put(BlueColor.NAME, BlueColor.class);
15+
}
16+
17+
public Color getColor(String name) {
18+
Color color = null;
19+
Class c = colors.get(name);
20+
try {
21+
color = (Color) c.newInstance();
22+
} catch (InstantiationException e) {
23+
24+
} catch (IllegalAccessException e) {
25+
26+
}
27+
return color;
28+
}
29+
30+
public Shape getShape(String name) {
31+
return null;
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.tctam.designpatterns.creational.abstractfactory;
2+
3+
public class GreenColor implements Color {
4+
public static final String NAME = GreenColor.class.getName();
5+
6+
public void fill() {
7+
System.out.println(NAME);
8+
}
9+
}

src/main/java/org/tctam/designpatterns/creational/abstractfactory/ProductA.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/java/org/tctam/designpatterns/creational/abstractfactory/ProductA1.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/org/tctam/designpatterns/creational/abstractfactory/ProductA2.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/org/tctam/designpatterns/creational/abstractfactory/ProductAFactory.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/java/org/tctam/designpatterns/creational/abstractfactory/ProductB.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/java/org/tctam/designpatterns/creational/abstractfactory/ProductB1.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/org/tctam/designpatterns/creational/abstractfactory/ProductB2.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)