设计模式
Java 的设计模式(自然是基于 OO)可以分为两种:一是 GoF 所总结的设计模式;二是 Java 的一些基本的设计模式,有简单工厂模式(静态工厂模式)、多例模式(多个实例)、缺省适配模式、不变模式。以下模式皆仅为示例,原理请参照菜鸟教程。
其中四人帮设计模式有 23 种,分为创建型、结构型、行为型:
1、创建型:
工厂方法模式:
接口(抽象):
package com.qfedu.MyBoot.pattern; public interface Shape { void draw(); }
实现类(具体):
package com.qfedu.MyBoot.pattern; public class Square implements Shape { @Override public void draw() { System.out.println("Square's draw"); } } package com.qfedu.MyBoot.pattern; public class Circle implements Shape { @Override public void draw() { System.out.println("Circle's draw"); } }
定义工厂类:
package com.qfedu.MyBoot.pattern; public class ShapeFactory { public Shape getShape(String shapeType) { if(shapeType.equals("Circle")) { return new Circle(); } else if(shapeType.equals("Square")) { return new Square(); } else { return null; } } }
然后就可以使用对象工厂获取对象了:
package com.qfedu.MyBoot.pattern; public class FactoryPatternDemo { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); Shape circle = shapeFactory.getShape("Circle"); circle.draw(); Shape square = shapeFactory.getShape("Square"); square.draw(); } }
抽象工厂模式:
第一种接口:
package com.qfedu.MyBoot.pattern; public interface Shape { void draw(); }
其实现类:
package com.qfedu.MyBoot.pattern; public class Square implements Shape { @Override public void draw() { System.out.println("Square's draw"); } } package com.qfedu.MyBoot.pattern; public class Circle implements Shape { @Override public void draw() { System.out.println("Circle's draw"); } }
第二种接口:
package com.qfedu.MyBoot.pattern; public interface Color { void fill(); }
其实现类:
package com.qfedu.MyBoot.pattern; public class Red implements Color { @Override public void fill() { System.out.println("Red's fill"); } } package com.qfedu.MyBoot.pattern; public class Blue implements Color { @Override public void fill() { System.out.println("Blue's fill"); } }
抽象工厂:
package com.qfedu.MyBoot.pattern; public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape); }
两种具体的工厂:
package com.qfedu.MyBoot.pattern; public class ShapeFactory extends AbstractFactory { @Override Color getColor(String color) { return null; } @Override Shape getShape(String shape) { if(shape.equals("Circle")) { return new Circle(); } else if(shape.equals("Square")) { return new Square(); } else { return null; } } } package com.qfedu.MyBoot.pattern; public class ColorFactory extends AbstractFactory { @Override Color getColor(String color) { if(color.equals("Red")) { return new Red(); } else if(color.equals("Blue")) { return new Blue(); } else { return null; } } @Override Shape getShape(String shape) { return null; } }
工厂提供者(工厂的工厂):
package com.qfedu.MyBoot.pattern; public class FactoryProducer { public static AbstractFactory getFactory(String factory) { if(factory.equals("Shape")) { return new ShapeFactory(); } else if(factory.equals("Color")) { return new ColorFactory(); } else { return null; } } }
选择使用工厂:
package com.qfedu.MyBoot.pattern; public class AbstractFactoryPatternDemo { public static void main(String[] args) { AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape"); Shape circle = shapeFactory.getShape("Circle"); circle.draw(); Shape square = shapeFactory.getShape("Square"); square.draw(); AbstractFactory colorFactory = FactoryProducer.getFactory("Color"); Color red = colorFactory.getColor("Red"); red.fill(); Color blue = colorFactory.getColor("Blue"); blue.fill(); } }
单例模式:
建造者模式: