public class ShapeFactory implements AbstractFactory {
@Override public Shape getShape(int shapeType, float... args) { if (shapeType == Shape.SHAPE_RECTANGLE) { float width = 0, height = 0; if (args != null && args.length == 2) { width = args[0]; height = args[1]; } return new RectangleShape(width, height); } else if (shapeType == Shape.SHAPE_SQUARE) { float length = 0; if (args != null && args.length == 1) { length = args[0]; } return new SquareShape(length); } else if (shapeType == Shape.SHAPE_CIRCLE) { float radius = 0; if (args != null && args.length == 1) { radius = args[0]; } return new CircleShape(radius); } return null; }
@Override public Color getColor(String colorType) { return null; } }
颜色工厂实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public class ColorFactory implements AbstractFactory { @Override public Shape getShape(int shapeType, float... args) { return null; }
@Override public Color getColor(String colorType) { if (Color.COLOR_RED.equals(colorType)) { return new Red(); } else if (Color.COLOR_BLACK.equals(colorType)) { return new Black(); } else if (Color.COLOR_WHITE.equals(colorType)) { return new White(); } return null; } }
通过工厂生成器来创建其他工厂
1 2 3 4 5 6 7 8 9 10 11 12 13
public class FactoryProducer { public static final String FACTORY_SHAPE = "shape_factory"; public static final String FACTORY_COLOR = "color_factory";
public static AbstractFactory getFactory(String factoryType) { if (FACTORY_SHAPE.equals(factoryType)) { return new ShapeFactory(); } else if (FACTORY_COLOR.equals(factoryType)) { return new ColorFactory(); } return null; } }
public interface DrawAPI { void drawCircle(float x, float y, float radius); }
public class DrawBlueCircle implements DrawAPI { @Override public void drawCircle(float x, float y, float radius) { System.out.println("draw blue circle"); } }
public class DrawRedCircle implements DrawAPI { @Override public void drawCircle(float x, float y, float radius) { System.out.println("draw red circle"); } }
public abstract class Shape { protected DrawAPI mDrawAPI;
public Shape(DrawAPI mDrawAPI) { this.mDrawAPI = mDrawAPI; }
public abstract void draw(); }
public class Circle extends Shape { private float x; private float y; private float radius;
public interface Strategy { float operator(float a, float b); }
public class AddStrategy implements Strategy { @Override public float operator(float a, float b) { return a + b; } }
public class MultiplyStrategy implements Strategy { @Override public float operator(float a, float b) { return a * b; } }
public class SubstractStrategy implements Strategy { @Override public float operator(float a, float b) { return a - b; } }
public class Context implements Strategy { private Strategy strategy;
public void setStrategy(Strategy strategy) { this.strategy = strategy; }
@Override public float operator(float a, float b) { if (strategy == null) { throw new RuntimeException("no strategy set"); } return strategy.operator(a, b); } }
public class AObserver implements IObserver { @Override public void update() { System.out.println("A observer update"); } }
public class BObserver implements IObserver { @Override public void update() { System.out.println("B observer update"); } }
public class Test { public static void main(String[] args) { Subject subject = new Subject(); subject.setState(0); subject.addObserver(new AObserver()); subject.addObserver(new BObserver()); subject.setState(1); } }
输出结果:
1 2 3 4 5 6 7
subject change no observer add observer: AObserver add observer: BObserver subject change A observer update B observer update
public class Test { public static void main(String[] args) { Filter filter = new RemoveBlankFilter(); filter.setNextFilter(new RemoveBottomLineFilter()).setNextFilter(new ToUppFilter()); System.out.println("Filter result: " + filter.filter("abc de_fGHIJK")); } }
输出结果:
1 2 3 4 5 6 7
Before RemoveBlankFilter: abc de_fGHIJK After RemoveBlankFilter: abcde_fGHIJK Before RemoveBottomLineFilter: abcde_fGHIJK After RemoveBottomLineFilter: abcdefGHIJK Before ToUppFilter: abcdefGHIJK After ToUppFilter: ABCDEFGHIJK Filter result: ABCDEFGHIJK
// 支持的请求处理逻辑 public class Request { public int sum(int... array) { int result = 0; if (array != null) { for (int i : array) { result += i; } } return result; }
public int mul(int... array) { int result = 1; if (array != null) { for (int i : array) { result *= i; } } return result; } }
// 命令接口 public interface Order { int ORDER_SUM = 0; int ORDER_MUL = 1;
int getOrderType();
int execute(int... array); }
// 累加命令 public class SumOrder implements Order { private Request request;
public SumOrder(Request request) { this.request = request; }
@Override public int getOrderType() { return Order.ORDER_SUM; }
@Override public int execute(int... array) { return request.sum(array); } }
// 累乘命令 public class MulOrder implements Order { private Request request;
public MulOrder(Request request) { this.request = request; }
@Override public int getOrderType() { return Order.ORDER_MUL; }
@Override public int execute(int... array) { return request.mul(array); } }
// 命令使用者 public class User { Map<Integer, Order> orderMap = new HashMap<>();
public void takeOrder(Order order) { orderMap.put(order.getOrderType(), order); }
public int executeOrder(int orderType, int... array) { Order order = orderMap.get(orderType); if (order != null) { return order.execute(array); } else { throw new RuntimeException("you has not " + orderType + " order"); } } }
// 测试命令使用者使用命令 public class Test { public static void main(String[] args) { Request request = new Request();
User user = new User(); user.takeOrder(new MulOrder(request)); user.takeOrder(new SumOrder(request));
// 定义状态接口 public interface State { void start(Context context);
void stop(Context context); }
// 定义使用状态的主体 public class Context { private State state;
public Context() { this.state = null; }
public State getState() { return state; }
public void setState(State state) { this.state = state; }
public void start() { getState().start(this); }
public void stop() { getState().stop(this); } }
// 开始状态可以切换至结束状态 public class StartState implements State { @Override public void start(Context context) { System.out.println("current state is start"); }
@Override public void stop(Context context) { context.setState(new StopState()); System.out.println("change to stop state"); } }
// 结束状态可以切换至开始状态 public class StopState implements State { @Override public void start(Context context) { context.setState(new StartState()); System.out.println("change to start state"); }
@Override public void stop(Context context) { System.out.println("current state is stop"); } }
// 测试状态切换 public class Test { public static void main(String[] args) { Context context = new Context(); context.setState(new StopState()); context.start(); context.stop(); context.stop(); context.start(); } }
输出结果:
1 2 3 4
current state is start change to stop state current state is stop change to start state
// 定义访问者管理类,设置对应身份访问的处理类型 public class Computer { public static final int SUM_VISITOR = 0; public static final int SUB_VISITOR = 1; public static final int MUL_VISITOR = 2;
Map<Integer, Calculator> calculatorMap = new HashMap<>();
public Computer() { calculatorMap.put(SUM_VISITOR, new Sum()); calculatorMap.put(SUB_VISITOR, new Sub()); calculatorMap.put(MUL_VISITOR, new Mul()); }
public int visit(int visitorType, int a, int b) { Calculator calculator = calculatorMap.get(visitorType); if (calculator != null) { return calculator.calculator(a, b); } else { throw new RuntimeException("unknown visitor type: " + visitorType); } } }
// 计算接口 public interface Calculator { int calculator(int a, int b); }
// 相加 public class Sum implements Calculator { @Override public int calculator(int a, int b) { return a + b; } }
// 相减 public class Sub implements Calculator { @Override public int calculator(int a, int b) { return a - b; } }
// 相乘 public class Mul implements Calculator { @Override public int calculator(int a, int b) { return a * b; } }
// 定义整除接口 public interface IDivisible { boolean divisible(int number); }
public class FiveDivisible implements IDivisible { @Override public boolean divisible(int number) { if (number % 5 == 0) { return true; } return false; } }
public class SevenDivisible implements IDivisible { @Override public boolean divisible(int number) { if (number % 7 == 0) { return true; } return false; } }
public class ElevenDivisible implements IDivisible { @Override public boolean divisible(int number) { if (number % 11 == 0) { return true; } return false; } }
// 找出一段区间内同时能被 5,7,11 整除的数 public class Solution { private IDivisible fiveDivisible = new FiveDivisible(); private IDivisible sevenDivisible = new SevenDivisible(); private IDivisible elevenDivisible = new ElevenDivisible();
public List<Integer> solution(int start, int end) { List<Integer> result = new ArrayList<>(); for (int i = start; i <= end; i++) { if (divisible(i)) { result.add(i); } } return result; }