@yudesong
2018-02-12T02:51:56.000000Z
字数 5041
阅读 468
设计模式
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
// 饿汉模式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();}// 创建实现类 Malepublic 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.");}}//创建实现类 Femalepublic 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 {@Overridepublic void Send() {System.out.println("this is mailsender!");}}public class SmsSender implements Sender {@Overridepublic void Send() {System.out.println("this is sms sender!");}}public class SendMailFactory implements Provider {@Overridepublic Sender produce(){return new MailSender();}}public class SendSmsFactory implements Provider{@Overridepublic 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;}@Overridepublic 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