单例模式
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
// 饿汉模式
public class Wife {
// 一开始就新建一个实例
private static final Wife wife = new Wife();
// 默认构造方法
private Wife() {}
// 获得实例的方法
public static Wife getWife() {
return wife;
}
}
// 懒汉模式
public class Wife {
//一开始没有新建实例
private static Wife wife;
private Wife() { }
// 需要时再新建
public static Wife getWife() {
if (wife == null) {
wife = new Wife();
}
return wife;
}
}
// 双重检验锁
public class Wife {
private volatile static Wife wife;
private Wife() { }
public static Wife getWife() {
if (wife == null) {
synchronized(Wife.class) {
if (wife == null) {
wife = new Wife();
}
}
}
return wife;
}
}
工厂模式
- 普通工厂模式
// 二者共同的接口
public interface Human{
public void eat();
public void sleep();
public void beat();
}
// 创建实现类 Male
public class Male implements Human{
public void eat(){
System.out.println("Male can eat.");
}
public void sleep(){
System.out.println("Male can sleep.");
}
public void beat(){
System.out.println("Male can beat.");
}
}
//创建实现类 Female
public class Female implements Human{
public void eat(){
System.out.println("Female can eat.");
}
public void sleep(){
System.out.println("Female can sleep.");
}
public void beat(){
System.out.println("Female can beat.");
}
}
// 创建普通工厂类
public class HumanFactory{
public Human createHuman(String gender){
if( gender.equals("male") ){
return new Male();
}else if( gender.equals("female")){
return new Female();
}else {
System.out.println("请输入正确的类型!");
return null;
}
}
}
// 工厂测试类
public class FactoryTest {
public static void main(String[] args){
HumanFactory factory = new HumanFactory();
Human male = factory.createHuman("male");
male.eat();
male.sleep();
male.beat();
}
}
- 多个工厂方法模式
// 多个工厂方法
public class HumanFactory{
public Male createMale() {
return new Male();
}
public Female createFemale() {
return new Female();
}
}
// 工厂测试类
public class FactoryTest {
public static void main(String[] args){
HumanFactory factory = new HumanFactory();
Human male = factory.createMale();
male.eat();
male.sleep();
male.beat();
}
}
- 静态工厂方法模式
// 多个工厂方法
public class HumanFactory{
public static Male createMale() {
return new Male();
}
public static Female createFemale() {
return new Female();
}
}
// 工厂测试类
public class FactoryTest {
public static void main(String[] args){
Human male = HumanFactory.createMale();
male.eat();
male.sleep();
male.beat();
}
}
抽象工厂
工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码。因为抽象工厂不太好理解,我们先看看图,然后就和代码,就比较容易理解。
public interface Sender {
public void Send();
}
public class MailSender implements Sender {
@Override
public void Send() {
System.out.println("this is mailsender!");
}
}
public class SmsSender implements Sender {
@Override
public void Send() {
System.out.println("this is sms sender!");
}
}
public class SendMailFactory implements Provider {
@Override
public Sender produce(){
return new MailSender();
}
}
public class SendSmsFactory implements Provider{
@Override
public Sender produce() {
return new SmsSender();
}
}
public interface Provider {
public Sender produce();
}
public class Test {
public static void main(String[] args) {
Provider provider = new SendMailFactory();
Sender sender = provider.produce();
sender.Send();
}
}
建造者模式
/**
* 构建器Builder模式
* Android中的AlertDialog的构建器模式
*/
public class Lunch {
private String cake;
private String meat;
private String milk;
private String drink;
public static class Builder{
private String meat; //必须要初始化的参数
private String cake;
private String milk;
private String drink;
public Builder(String meat){
this.meat = meat;
}
public Builder addCake(String cake){
this.cake = cake;
return this;
}
public Builder addMilk(String milk){
this.milk = milk;
return this;
}
public Builder addDrink(String drink){
this.drink = drink;
return this;
}
public Lunch create(){
return new Lunch(this);
}
}
private Lunch(Builder builder){
this.meat = builder.meat;
this.cake = builder.cake;
this.milk = builder.milk;
this.drink = builder.drink;
}
@Override
public String toString() {
return "Lunch [cake=" + cake + ", drink=" + drink + ", meat=" + meat
+ ", milk=" + milk + "]";
}
public static void main(String[] args) {
Lunch.Builder builder = new Lunch.Builder("meat");
Lunch lunch = builder.addCake("cake")
.addDrink("drink")
//.addMilk("milk")
.create();
System.out.println(lunch.toString());
}
}
原型模式
- 浅复制:将一个对象复制后,基本数据类型的变量都会重新创建,而引用类型,指向的还是原对象所指向的。
- 深复制:将一个对象复制后,不论是基本数据类型还有引用类型,都是重新创建的。简单来说,就是深复制进
public class Prototype implements Cloneable {
public Object clone() throws CloneNotSupportedException {
Prototype proto = (Prototype) super.clone();
return proto;
}
}
public class Prototype implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
private String string;
private SerializableObject obj;
/* 浅复制 */
public Object clone() throws CloneNotSupportedException {
Prototype proto = (Prototype) super.clone();
return proto;
}
/* 深复制 */
public Object deepClone() throws IOException, ClassNotFoundException {
/* 写入当前对象的二进制流 */
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
/* 读出二进制流产生的新对象 */
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public SerializableObject getObj() {
return obj;
}
public void setObj(SerializableObject obj) {
this.obj = obj;
}
}
class SerializableObject implements Serializable {
private static final long serialVersionUID = 1L;
}
参考文献
1. http://blog.csdn.net/zhangerqing/article/details/8194653
2. http://www.importnew.com/18390.html