今天给大家介绍创建型模式中的其中3个:工厂模式,建造者模式和单例模式
双重校验单例模式
体来说设计模式分为三大类:
- 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
- 结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
- 行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
单例模式相信大家很熟悉,包括懒汉恶汉和双重校验模式.这里就给大家介绍双重校验模式,先上才艺.
public class BuilderPatternManger {
//这里我就偷个懒,就不重新写啦,这是创建者模式中使用的单例模式!
private BuilderPatternManger(){
}
private static volatile BuilderPatternManger mBuilderPatternManger;
public static BuilderPatternManger getInstance(){
if (mBuilderPatternManger == null) {
synchronized (BuilderPatternManger.class){
mBuilderPatternManger = new BuilderPatternManger(); }
}
return mBuilderPatternManger;
}
}
注意事项:
- 由于单例模式只需要创建一次实例,如果后面再次调用getInstance()方法时,则直接返回之前创建的实例,因此大部分时间不需要执行同步方法里面的代码,大大提高了性能。如果不加第一次校验的话,那跟懒汉模式没什么区别,每次都要去竞争锁。
- 第二次校验:如果没有第二次校验,结果就会导致创建多个实例。所以需要在同步代码里面进行第二次校验,如果实例为空,则进行创建。
- 需要加 volatile 关键字.
- 需要私有化无参构造
工厂模式
工厂方法模式分为三种:
- 1、普通工厂模式,就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。
- 2.多个工厂方法模式,是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象
- 3、静态工厂方法模式,将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。
public interface FactoryMethodInterface {
//创建一个共同接口用来声明自己的
public String Send();
}
//工厂A 实现接口FactoryMethodInterface
public class FactoryMethodA implements FactoryMethodInterface {
@Override
public String Send() {
return "我是FactoryMethodA";
}
}
//工厂B 实现接口FactoryMethodInterface
public class FactoryMethodB implements FactoryMethodInterface {
@Override
public String Send() {
return "我是FactoryMethodB";
}
}
//工厂C 实现接口FactoryMethodInterface
public class FactoryMethodC implements FactoryMethodInterface {
@Override
public String Send() {
return "我是FactoryMethodC";
}
}
//创建一个工厂管理利器
public class FactoryMethodMessage {
private FactoryMethodMessage(){}
private static volatile FactoryMethodMessage mFactoryMethodMessage = null;
public static FactoryMethodMessage getInstance(){
if (mFactoryMethodMessage == null) {
synchronized (FactoryMethodMessage.class){
if (mFactoryMethodMessage == null) {
mFactoryMethodMessage = new FactoryMethodMessage();
}
}
}
return mFactoryMethodMessage;
}
//通过调用此方法,来获取对应的值
public String getFactryMethod(String type){
if (type.equals("a")) {
return new FactoryMethodA().Send();
}else if(type.equals("b")){
return new FactoryMethodB().Send();
}else if (type.equals("c")){
return new FactoryMethodC().Send();
}
return "没有此类型";
}
}
String b = FactoryMethodMessage.getInstance().getFactryMethod("b");
//这里显示结果为 我是FactoryMethodB
建造者模式
直接上才艺!
创建汽车:
public class BuilderPatternBean {
public CarColor mColor;
public CarWeight mCarWeight;
public CarName mCarName;
public CarColor getmColor() {
return mColor;
}
public void setmColor(CarColor mColor) {
this.mColor = mColor;
}
public CarWeight getmCarWeight() {
return mCarWeight;
}
public void setmCarWeight(CarWeight mCarWeight) {
this.mCarWeight = mCarWeight;
}
public CarName getmCarName() {
return mCarName;
}
public void setmCarName(CarName mCarName) {
this.mCarName = mCarName;
}
public BuilderPatternBean(CarColor mColor, CarWeight mCarWeight, CarName mCarName) {
this.mColor = mColor;
this.mCarWeight = mCarWeight;
this.mCarName = mCarName;
}
}
创建汽车样式:
//创建车的颜色
public class CarColor {
public String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
//创建车的名字
public class CarName {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//创建车的宽度
public class CarWeight {
public int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
//创建"组装"车的方法
public interface BuilderPatternInterface {
public BuilderPatternImpl createColor(String color);
public BuilderPatternImpl createWeight(int weight);
public BuilderPatternImpl createName(String name);
public BuilderPatternBean build();
}
实现BuilderPatternInterface接口,实现组装车:
public class BuilderPatternImpl implements BuilderPatternInterface {
private final BuilderPatternBean bmBuilderPatternBean;
private final CarColor mCarColor;
private final CarWeight mCarWeight;
private final CarName mCarName;
public BuilderPatternImpl(){
mCarColor = new CarColor();
mCarWeight = new CarWeight();
mCarName = new CarName();
bmBuilderPatternBean = new BuilderPatternBean(mCarColor, mCarWeight, mCarName);
}
@Override
public BuilderPatternImpl createColor(String color) {
mCarColor.setColor(color);
bmBuilderPatternBean.setmColor(mCarColor);
return this;
}
@Override
public BuilderPatternImpl createWeight(int weight) {
mCarWeight.setWeight(weight);
bmBuilderPatternBean.setmCarWeight(mCarWeight);
return this;
}
@Override
public BuilderPatternImpl createName(String name) {
mCarName.setName(name);
bmBuilderPatternBean.setmCarName(mCarName);
return this;
}
@Override
public BuilderPatternBean build() {
return bmBuilderPatternBean;
}
}
创建汽车管理类:
public class BuilderPatternManger {
private BuilderPatternManger(){
}
private static volatile BuilderPatternManger mBuilderPatternManger;
public static BuilderPatternManger getInstance(){
if (mBuilderPatternManger == null) {
synchronized (BuilderPatternManger.class){
mBuilderPatternManger = new BuilderPatternManger();
}
}
return mBuilderPatternManger;
}
public BuilderPatternBean createCar(BuilderPatternImpl mBuilderPatternImpl){
mBuilderPatternImpl.createName("兰博基尼").
createWeight(200).
createColor("红色");
BuilderPatternBean build = mBuilderPatternImpl.build();
return build;
}
}
//最后在代码中调用即可:
BuilderPatternBean car = BuilderPatternManger.getInstance().createCar(new BuilderPatternImpl());
Toast.makeText(this, car.mCarName.name+" \n "+car.mCarWeight.weight+"\n"+car.mColor.color, Toast.LENGTH_SHORT).show();