design patterns : abstract factory

This commit is contained in:
haerong22
2021-11-30 02:09:20 +09:00
parent 79edb155af
commit bcf5a61879
23 changed files with 150 additions and 11 deletions

View File

@@ -0,0 +1,4 @@
package abstract_factory.after;
public interface Anchor {
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,8 @@
package abstract_factory.after;
public interface ShipPartsFactory {
Anchor createAnchor();
Wheel createWheel();
}

View File

@@ -0,0 +1,4 @@
package abstract_factory.after;
public interface Wheel {
}

View File

@@ -0,0 +1,4 @@
package abstract_factory.after;
public class WhiteAnchorPro implements Anchor {
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -0,0 +1,4 @@
package abstract_factory.after;
public class WhiteWheelPro implements Wheel {
}

View File

@@ -0,0 +1,6 @@
package abstract_factory.before;
import abstract_factory.after.Anchor;
public class WhiteAnchor implements Anchor {
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,6 @@
package abstract_factory.before;
import abstract_factory.after.Wheel;
public class WhiteWheel implements Wheel {
}

View File

@@ -1,4 +1,4 @@
package FactoryMethod.after;
package factory_method.after;
public class BlackShip extends Ship{

View File

@@ -1,4 +1,4 @@
package FactoryMethod.after;
package factory_method.after;
public class BlackShipFactory extends DefaultShipFactory{

View File

@@ -1,4 +1,4 @@
package FactoryMethod.after;
package factory_method.after;
public class Client {

View File

@@ -1,4 +1,4 @@
package FactoryMethod.after;
package factory_method.after;
public abstract class DefaultShipFactory implements ShipFactory{

View File

@@ -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;
}

View File

@@ -1,4 +1,4 @@
package FactoryMethod.after;
package factory_method.after;
public interface ShipFactory {

View File

@@ -1,4 +1,4 @@
package FactoryMethod.after;
package factory_method.after;
public class WhiteShip extends Ship {

View File

@@ -1,4 +1,4 @@
package FactoryMethod.after;
package factory_method.after;
public class WhiteShipFactory extends DefaultShipFactory {

View File

@@ -1,4 +1,4 @@
package FactoryMethod.before;
package factory_method.before;
public class Client {

View File

@@ -1,4 +1,4 @@
package FactoryMethod.after;
package factory_method.before;
public class Ship {

View File

@@ -1,4 +1,4 @@
package FactoryMethod.before;
package factory_method.before;
public class ShipFactory {