design patterns : abstract factory
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
public interface Anchor {
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
import factory_method.after.Ship;
|
||||
import factory_method.after.ShipFactory;
|
||||
|
||||
public class ShipInventory {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ShipFactory shipFactory = new WhiteShipFactory(new WhiteShipPartsFactory());
|
||||
Ship ship = shipFactory.createShip("white ship");
|
||||
System.out.println("ship.getAnchor() = " + ship.getAnchor());
|
||||
System.out.println("ship.getWheel() = " + ship.getWheel());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
public interface ShipPartsFactory {
|
||||
|
||||
Anchor createAnchor();
|
||||
|
||||
Wheel createWheel();
|
||||
}
|
||||
4
design-pattern/gof/src/abstract_factory/after/Wheel.java
Normal file
4
design-pattern/gof/src/abstract_factory/after/Wheel.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
public interface Wheel {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
public class WhiteAnchorPro implements Anchor {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
import factory_method.after.DefaultShipFactory;
|
||||
import factory_method.after.Ship;
|
||||
import factory_method.after.WhiteShip;
|
||||
|
||||
public class WhiteShipFactory extends DefaultShipFactory {
|
||||
|
||||
private final ShipPartsFactory shipPartsFactory;
|
||||
|
||||
public WhiteShipFactory(ShipPartsFactory shipPartsFactory) {
|
||||
this.shipPartsFactory = shipPartsFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ship createShip(String name) {
|
||||
Ship ship = new WhiteShip(name);
|
||||
ship.setAnchor(shipPartsFactory.createAnchor());
|
||||
ship.setWheel(shipPartsFactory.createWheel());
|
||||
return ship;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
import abstract_factory.before.WhiteAnchor;
|
||||
import abstract_factory.before.WhiteWheel;
|
||||
|
||||
public class WhiteShipPartsFactory implements ShipPartsFactory {
|
||||
@Override
|
||||
public Anchor createAnchor() {
|
||||
return new WhiteAnchor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Wheel createWheel() {
|
||||
return new WhiteWheel();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
public class WhiteShipPartsProFactory implements ShipPartsFactory {
|
||||
@Override
|
||||
public Anchor createAnchor() {
|
||||
return new WhiteAnchorPro();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Wheel createWheel() {
|
||||
return new WhiteWheelPro();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package abstract_factory.after;
|
||||
|
||||
public class WhiteWheelPro implements Wheel {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package abstract_factory.before;
|
||||
|
||||
import abstract_factory.after.Anchor;
|
||||
|
||||
public class WhiteAnchor implements Anchor {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package abstract_factory.before;
|
||||
|
||||
import factory_method.after.DefaultShipFactory;
|
||||
import factory_method.after.Ship;
|
||||
import factory_method.after.WhiteShip;
|
||||
|
||||
public class WhiteShipFactory extends DefaultShipFactory {
|
||||
@Override
|
||||
public Ship createShip(String name) {
|
||||
Ship ship = new WhiteShip("white ship");
|
||||
ship.setAnchor(new WhiteAnchor());
|
||||
ship.setWheel(new WhiteWheel());
|
||||
return ship;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package abstract_factory.before;
|
||||
|
||||
import abstract_factory.after.Wheel;
|
||||
|
||||
public class WhiteWheel implements Wheel {
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.after;
|
||||
package factory_method.after;
|
||||
|
||||
public class BlackShip extends Ship{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.after;
|
||||
package factory_method.after;
|
||||
|
||||
public class BlackShipFactory extends DefaultShipFactory{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.after;
|
||||
package factory_method.after;
|
||||
|
||||
public class Client {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.after;
|
||||
package factory_method.after;
|
||||
|
||||
public abstract class DefaultShipFactory implements ShipFactory{
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
package FactoryMethod.before;
|
||||
package factory_method.after;
|
||||
|
||||
import abstract_factory.after.Anchor;
|
||||
import abstract_factory.after.Wheel;
|
||||
|
||||
public class Ship {
|
||||
|
||||
@@ -8,6 +11,26 @@ public class Ship {
|
||||
|
||||
private String logo;
|
||||
|
||||
private Wheel wheel;
|
||||
|
||||
private Anchor anchor;
|
||||
|
||||
public Wheel getWheel() {
|
||||
return wheel;
|
||||
}
|
||||
|
||||
public void setWheel(Wheel wheel) {
|
||||
this.wheel = wheel;
|
||||
}
|
||||
|
||||
public Anchor getAnchor() {
|
||||
return anchor;
|
||||
}
|
||||
|
||||
public void setAnchor(Anchor anchor) {
|
||||
this.anchor = anchor;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.after;
|
||||
package factory_method.after;
|
||||
|
||||
public interface ShipFactory {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.after;
|
||||
package factory_method.after;
|
||||
|
||||
public class WhiteShip extends Ship {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.after;
|
||||
package factory_method.after;
|
||||
|
||||
public class WhiteShipFactory extends DefaultShipFactory {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.before;
|
||||
package factory_method.before;
|
||||
|
||||
public class Client {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.after;
|
||||
package factory_method.before;
|
||||
|
||||
public class Ship {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package FactoryMethod.before;
|
||||
package factory_method.before;
|
||||
|
||||
public class ShipFactory {
|
||||
|
||||
Reference in New Issue
Block a user