#设计模式 使用设计模式的原则
- 开闭原则(Open Close Principle) 说对扩展开放, 对修改关闭, 在程序需要进行拓展的时候, 不能去修改原有的代码.
- 里氏代换原则(Liskov Substitution Principle) 任何基类可以出现的地方, 子类一定可以出现, 只有当衍生类可以替换掉基类, 软件单位的功能不受到影响时, 基类才能真正被复用, 而衍生类也能够在基类的基础上增加新的行为.
- 依赖倒转原则(Dependence Inversion Principle) 开闭原则的基础, 针对接口编程, 依赖于抽象而不依赖于具体.
- 接口隔离原则(Interface Segregation Principle) 使用多个隔离的接口, 优于使用单个接口, 可以降低类之间的耦合度.
- 迪米特法则(Demeter Principle) 又称最少知道原则, 一个实体应当尽量少的与其他实体之间发生相互作用, 使得系统功能模块相对独立.
- 合成复用原则(Composite Reuse Principle) 尽量使用合成和聚合的方式, 而不是使用继承.
##创建型模式 ###工厂模式 轻松方便地构造对象实例, 而不必关心构造对象实例的细节和复杂过程.
工厂模式分为工厂方法模式和抽象工厂模式
####工厂方法模式(Factory Method)
利用工厂方法模式设计水果工厂
package javatest1;
interface Fruit {
String getName();
}
class Apple implements Fruit {
@Override
public String getName() {
return "apple";
}
}
class Banana implements Fruit {
@Override
public String getName() {
return "banana";
}
}
class FruitFactory {
public static Apple createApple() {
return new Apple();
}
public static Banana createBanana() {
return new Banana();
}
}
public class Main {
public static void main(String[] args) {
Fruit apple = FruitFactory.createApple();
Fruit banana = FruitFactory.createBanana();
System.out.println(apple.getName());
System.out.println(banana.getName());
}
}
####抽象工厂模式(Abstract Factory)
利用抽象工厂模式设计水果工厂
package javatest1;
interface Fruit {
String getName();
}
class Apple implements Fruit {
@Override
public String getName() {
return "apple";
}
}
class Banana implements Fruit {
@Override
public String getName() {
return "banana";
}
}
interface FruitFactory {
Fruit create();
}
class AppleFactory implements FruitFactory {
@Override
public Apple create() {
return new Apple();
}
}
class BananaFactory implements FruitFactory {
@Override
public Banana create() {
return new Banana();
}
}
public class Main {
public static void main(String[] args) {
FruitFactory appleFactory = new AppleFactory();
FruitFactory bananaFactory = new BananaFactory();
Fruit apple = appleFactory.create();
Fruit banana = bananaFactory.create();
System.out.println(apple.getName());
System.out.println(banana.getName());
}
}
###单例模式(Singleton) 总有一些类的对象需要是唯一的, 这时可以考虑使用单例模式. 单例模式的优点: 严格控制对象的数目, 访问方式和封装.
地球单例模式
class Earth {
private static Earth earth = new Earth();
private Earth() {}
public static Earth getInstance() {
return earth;
}
}
public class Main {
public static void main(String[] args) {
Earth foo = Earth.getInstance();
Earth bar = Earth.getInstance();
if (foo == bar) {
System.out.println("Singleton");
}
}
}
###建造者模式(Builder)
将复杂对象的创建工作分解成方法, 通常可以设计成连缀方法, 最后返回构造完的对象.
使用建造者模式来建造水果盒子
import java.util.ArrayList;
import java.util.List;
interface Fruit {
String getName();
}
class Apple implements Fruit {
@Override
public String getName() {
return "apple";
}
}
class Banana implements Fruit {
@Override
public String getName() {
return "banana";
}
}
class FruitBox {
private List<Fruit> fruitList = new ArrayList<>();
public void add(Fruit fruit) {
fruitList.add(fruit);
}
public List<Fruit> getFruitList() {
return fruitList;
}
}
class FruitBoxBuilder {
private FruitBox fruitBox = new FruitBox();
public FruitBoxBuilder append(Fruit fruit) {
fruitBox.add(fruit);
return this;
}
public List<Fruit> toList() {
return fruitBox.getFruitList();
}
}
public class Main {
public static void main(String[] args) {
Fruit apple = new Apple();
Fruit banana = new Banana();
FruitBoxBuilder builder = new FruitBoxBuilder();
List<Fruit> fruitList = builder.append(apple).append(banana).toList();
for (Fruit fruit : fruitList) {
System.out.println(fruit.getName());
}
}
}
类库中使用建造者模式的StringBuilder
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
String result = sb.append("hello").append(" java !").toString();
System.out.println(result);
}
}
###原型模式(Prototype) 原型模式可以使用预定义的原型对象来快速完成对象的创建
使用原型模式来创建颜色对象
import java.util.HashMap;
import java.util.Map;
interface ColorPrototype {
void display();
}
class Color implements ColorPrototype {
private int red, green, blue;
public Color(Color color) {
this.red = color.red;
this.green = color.green;
this.blue = color.blue;
}
public Color(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
@Override
public void display() {
String rgb = "RGB(" + this.red + ", " + this.green +
", " + this.blue + ")";
System.out.println(rgb);
}
public int getRed() { return red; }
public void setRed(int red) { this.red = red; }
public int getGreen() { return green; }
public void setGreen(int green) { this.green = green; }
public int getBlue() { return blue; }
public void setBlue(int blue) { this.blue = blue; }
}
class ColorManager {
Map<String, Color> colors = new HashMap<>();
public void addPrototype(String key, Color color) {
colors.put(key, color);
}
public Color getPrototype(String key) {
Color color = colors.get(key);
Color colorCopy = new Color(color);
return colorCopy;
}
}
public class Main {
public static void main(String[] args) {
ColorManager colorManager = new ColorManager();
colorManager.addPrototype("red", new Color(255, 0, 0));
colorManager.addPrototype("green", new Color(0, 255, 0));
colorManager.addPrototype("blue", new Color(0, 0, 255));
Color colorYellow = colorManager.getPrototype("red");
colorYellow.setGreen(255);
colorYellow.display();
}
}