1.单例模式
1.懒汉模式
真正使用时再实例化
1.双重校验
2.voliate禁止重排
public class Learn {
private volatile static Learn l ;
private Learn(){
}
private static Learn getInstance() {
if(l == null){
synchronized (Learn.class){
if(l == null){
l = new Learn();
}
}
}
return l;
}
}
2.饿汉模式
类加载的初始化阶段就完成了实例化
public class Learn {
private static Learn l = new Learn();
private Learn(){
}
private static Learn getInstant() {
return l;
}
}
3.静态内部类模式
调用getInstance,静态内部类初始化
public class Learn {
private static class LearnHolder{
private static Learn l = new Learn();
}
private Learn(){
}
private static Learn getInstant() {
return LearnHolder.l;
}
}
2.工厂方法模式
实例化延迟到子类实现
public class Product {
class ProductDemo{
}
public class ProductA extends ProductDemo{
}
public class ProductB extends ProductDemo{
}
abstract class FactoryProduct{
abstract ProductDemo create();
ProductDemo getObject(){
return create();
}
}
class FactoryA extends FactoryProduct{
@Override
ProductDemo create() {
return new ProductA();
}
}
class FactoryB extends FactoryProduct{
@Override
ProductDemo create() {
return new ProductB();
}
}
public void main(String[] args) {
FactoryProduct demo = new FactoryA();
ProductDemo proA = demo.getObject();
}
}
3.抽象工厂模式
创建一系列互相相关或者依赖的接口,而无需指定具体的实现类(个人感觉跟工厂模式差不多,只不过不止用来实例化对象,可能有更多的功能接口)
public class Product {
interface ProductDemo{
Create createPro();
Destory destoryPro();
}
interface Create{
void ex();
}
interface Destory{
void ex();
}
class CreateA implements Create{
@Override
public void ex() {
System.out.println("生产产品A");
}
}
class CreateB implements Create{
@Override
public void ex() {
System.out.println("生产产品A");
}
}
class DestoryA implements Destory{
@Override
public void ex() {
System.out.println("开始销毁产品B");
}
}
class DestoryB implements Destory{
@Override
public void ex() {
System.out.println("开始销毁产品B");
}
}
class ProduceA implements ProductDemo{
@Override
public Create createPro() {
return new CreateA();
}
@Override
public Destory destoryPro() {
return new DestoryA();
}
}
class ProduceB implements ProductDemo{
@Override
public Create createPro() {
return new CreateB();
}
@Override
public Destory destoryPro() {
return new DestoryB();
}
}
public void main(String[] args) {
ProductDemo demo = new ProduceA(); // new ProductB()
demo.createPro();
demo.destoryPro();
}
}
4.建造者模式
建造者经常与不可变对象组合使用
生成对象的属性互相依赖(比如有顺序要求)
package com.example.demo;
class Product {
private final String name;
private final String no;
Product(String name, String no){
this.name = name;
this.no = no;
}
static class ProductBuilder{
private String name;
private String no;
ProductBuilder name(String name){
this.name = name;
return this;
}
ProductBuilder no(String no){
this.no = no;
return this;
}
Product build(){
return new Product(this.name,this.no);
}
}
}
public class Productor {
public void main(String[] args) {
Product.ProductBuilder demo = new Product.ProductBuilder(); // new ProductB()
Product build = demo.name("name").no("no").build();
}
}
5.适配器模式
当现有的类,与其他代码不兼容时。或希望使用一些子类,但子类的方法不能添加到超类时
class Adaptee {
Adaptee(){
}
public Integer out22A() {
return 22;
}
}
class Adaptor implements Plug {
private Adaptee adaptee;
Adaptor(Adaptee adaptee){
this.adaptee = adaptee;
}
@Override
public Integer out5A() {
Integer old = this.adaptee.out22A();
// 经过一系列转换
return 5;
}
}
interface Plug{
public Integer out5A();
}
public class Adapter {
public static void main(String[] args) {
Adaptor adaptor = new Adaptor(new Adaptee());
adaptor.out5A();
}
}