工厂模式
作用:为创建对象提供过度接口,将创建对象的具体过程屏蔽,提高灵活性
-
简单工厂模式/静态工厂方法模式:
- 创建对象的接口
- 组成:抽象产品,具体产品,工厂
- 问题:
- 增加新产品,重写工厂类,违反开闭原则
-
UML图
- 代码
public interface Product { void productPhone(); } public class ConcreteProduct implements Product { public void productPhone() { System.out.println("生成第一个种类的手机"); } } public class ConcreteProduct2 implements Product { public void productPhone() { System.out.println("生成第二种手机"); } } public class Creator { public void create(String name) { if("1".equals(name)) { Product product = new ConcreteProduct(); product.productPhone(); }else if("2".equals(name)) { Product product = new ConcreteProduct2(); product.productPhone(); } } } public class User { public static void main(String[] args) { // TODO Auto-generated method stub Creator creator = new Creator(); creator.create("1"); creator.create("2"); } }
-
工厂方法模式:
- 除去简单工厂模式中的工厂方法静态属性(上一个中的name参数)
- 组成:抽象工厂角色,具体工厂角色,抽象产品角色,具体产品角色
- 弥补:生成新产品的工厂实现抽象工厂,不用重写工厂
- 情况:
- 客户程序不需要知道使用的对象创建过程
- 客户程序使用对象存在变动情况或不知道使用哪一个具体对象
- 代码:
public interface Product { void productPhone(); } public class ConcreteProduct implements Product { public void productPhone() { System.out.println("生成第一个种类的手机"); } } public class ConcreteProduct2 implements Product { public void productPhone() { System.out.println("生成第二种手机"); } } public interface Creator { Product creatorProduct(); } public class ConcreteCreator2 implements Creator { public Product creatorProduct() { Product product = new ConcreteProduct2(); return product; } } public class ConcreteCreator implements Creator { public Product creatorProduct() { Product product = new ConcreteProduct(); return product; } } public class User { public static void main(String[] args) { Creator creator = new ConcreteCreator2(); Product product = creator.creatorProduct(); product.productPhone(); } }
-
抽象工厂模式:
作用:给客户端提供一个接口,以创建多个产品族中的产品对象
角色:抽象工厂角色,具体工厂角色,抽象产品角色,具体产品角色
-
UML
代码
public class User { public static void main(String[] args) { Creator creator = new CreatorA(); Product product = creator.create(); product.pro(); creator = new CreatorB(); product = creator.create2(); product.pro(); } } public interface Creator { Product create(); Product create2(); } public class CreatorA implements Creator { public Product create() { Product product = new ProductA(); return product; } public Product create2() { Product product = new Product2A(); return product; } } public class CreatorB implements Creator { public Product create() { Product product = new ProductB(); return product; } public Product create2() { Product product = new Product2B(); return product; } } public interface Product { void pro(); } public class ProductA implements Product { public void pro() { System.out.println("Product A 1"); } } public class ProductB implements Product { public void pro() { System.out.println("Product B 1"); } } public interface Product2 { void pro(); } public class Product2A implements Product { public void pro() { System.out.println("Product A 2"); } } public class Product2B implements Product { public void pro() { System.out.println("Product B 2"); } }
单例模式
定义:保证一个类只有一个实例,并提供一个访问它的全局访问点
-
懒汉式(线程不安全)
- 不支持多线程,没有加锁
- 代码
public class Singleton1 { private static Singleton1 singleton; private Singleton1() {} public static Singleton1 getInstance() { if(singleton == null) { singleton = new Singleton1(); } return singleton; } public static void show() { System.out.println("懒汉式,线程不安全"); } }
-
懒汉式(线程安全)
- 能在多线程运行,效率低
- 代码:
public class Singleton2 { private static Singleton2 singleton; private Singleton2() {} public static synchronized Singleton2 getInstance() { if(singleton == null) { singleton = new Singleton2(); } return singleton; } public static void show() { System.out.println("懒汉式,线程安全"); } }
-
饿汉式
- 不加锁,效率高;无论是否使用该类,加载时就创建单例对象
- 代码:
public class Singleton3 { private static Singleton3 singleton = new Singleton3(); private Singleton3() {} public static synchronized Singleton3 getInstance() { return singleton; } public static void show() { System.out.println("饿汉式"); } }
-
双检锁/双重校验锁(DCL)
- 效率高,安全,多线程下仍然高性能
- 代码
public class Singleton4 { private static Singleton4 singleton; private Singleton4() {} public static Singleton4 getInstance() { if(singleton == null) { synchronized(Singleton4.class) { if(singleton == null) { singleton = new Singleton4(); } } } return singleton; } public static void show() { System.out.println("双重锁"); } }
-
静态内部类
- 理解:当加载这个类时并不进行初始化,只有第一次通过方法获取实例时,才第一次加载内部类创建对象
- 代码
public class Singleton5 { private Singleton5() {} public Singleton5 getInstance() { return Inner.singleton; } private static class Inner{ private static final Singleton5 singleton= new Singleton5(); } }
-
枚举:
- 默认枚举类是线程安全且在任何情况下都是单例
- 枚举类隐藏了私有构造器,其域对应一个实例对象
- 代码:
public enum Singleton6{ INSTANCE; }
建造模式
将一个复杂对象的构建与他的表示分离,似的同样的构建过程可以创建不同的表示(将构造复杂对象的过程和组成对象的部件解耦)
组成:抽象建造者角色,具体建造者角色,指导者角色,产品角色
-
UML
代码
public class Product {
public static void main(String[] args) {
// TODO Auto-generated method stub
Builder builder = new ConcreteBuilder();
Director dir = new Director(builder);
dir.build1();
dir.build2();
}
}
public interface Builder {
void part1();
void part2();
void part3();
}
public class ConcreteBuilder implements Builder{
public void part1() {
System.out.println("第一部分");
}
public void part2() {
System.out.println("第二部分");
}
public void part3() {
System.out.println("第三部分");
}
}
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void build1() {
builder.part1();
builder.part3();
}
public void build2() {
builder.part2();
builder.part1();
}
}
原型模式
- 是一种对象创建模式,用原型实例指定创建对象的种类,通过拷贝这些原型创建新的对象
- 角色:客户角色(适用对象,生成对象原型和克隆),抽象原型角色,具体原型角色
-
UML
- 代码
public class Resume implements Cloneable {
private String des;
private int id;
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
public String toString() {
return "Resume [des=" + des + ", id=" + id + "]";
}
}
public class Client {
private int i=0;
private Resume res = new Resume();
public Resume cloneRes() {
System.out.println(i++);
return (Resume)res.clone();
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Client client = new Client();
client.cloneRes();
client.cloneRes();
}
}
适配器模式
- 讲一个类的接口转换成客户希望的另一个接口
- 角色:目标角色,被适配角色,适配器角色
-
UML
- 代码:
public interface A {
void play();
}
public class ConcreteA implements A {
public void play() {
System.out.println("A");
}
}
public interface B {
void action();
}
public class AdapterB implements B {
private A a;
public AdapterB(A a) {
this.a = a;
}
public void action() {
a.play();
}
}
public class Main {
public static void main(String[] args) {
A a = new ConcreteA();
B b = new AdapterB(a);
b.action();
}
}