[关闭]
@yudesong 2018-02-12T02:51:56.000000Z 字数 5041 阅读 468

设计模式之创建型

设计模式


单例模式

这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

  1. // 饿汉模式
  2. public class Wife {
  3. // 一开始就新建一个实例
  4. private static final Wife wife = new Wife();
  5. // 默认构造方法
  6. private Wife() {}
  7. // 获得实例的方法
  8. public static Wife getWife() {
  9. return wife;
  10. }
  11. }
  12. // 懒汉模式
  13. public class Wife {
  14. //一开始没有新建实例
  15. private static Wife wife;
  16. private Wife() { }
  17. // 需要时再新建
  18. public static Wife getWife() {
  19. if (wife == null) {
  20. wife = new Wife();
  21. }
  22. return wife;
  23. }
  24. }
  25. // 双重检验锁
  26. public class Wife {
  27. private volatile static Wife wife;
  28. private Wife() { }
  29. public static Wife getWife() {
  30. if (wife == null) {
  31. synchronized(Wife.class) {
  32. if (wife == null) {
  33. wife = new Wife();
  34. }
  35. }
  36. }
  37. return wife;
  38. }
  39. }

工厂模式

  1. // 二者共同的接口
  2. public interface Human{
  3. public void eat();
  4. public void sleep();
  5. public void beat();
  6. }
  7. // 创建实现类 Male
  8. public class Male implements Human{
  9. public void eat(){
  10. System.out.println("Male can eat.");
  11. }
  12. public void sleep(){
  13. System.out.println("Male can sleep.");
  14. }
  15. public void beat(){
  16. System.out.println("Male can beat.");
  17. }
  18. }
  19. //创建实现类 Female
  20. public class Female implements Human{
  21. public void eat(){
  22. System.out.println("Female can eat.");
  23. }
  24. public void sleep(){
  25. System.out.println("Female can sleep.");
  26. }
  27. public void beat(){
  28. System.out.println("Female can beat.");
  29. }
  30. }
  31. // 创建普通工厂类
  32. public class HumanFactory{
  33. public Human createHuman(String gender){
  34. if( gender.equals("male") ){
  35. return new Male();
  36. }else if( gender.equals("female")){
  37. return new Female();
  38. }else {
  39. System.out.println("请输入正确的类型!");
  40. return null;
  41. }
  42. }
  43. }
  44. // 工厂测试类
  45. public class FactoryTest {
  46. public static void main(String[] args){
  47. HumanFactory factory = new HumanFactory();
  48. Human male = factory.createHuman("male");
  49. male.eat();
  50. male.sleep();
  51. male.beat();
  52. }
  53. }
  1. // 多个工厂方法
  2. public class HumanFactory{
  3. public Male createMale() {
  4. return new Male();
  5. }
  6. public Female createFemale() {
  7. return new Female();
  8. }
  9. }
  10. // 工厂测试类
  11. public class FactoryTest {
  12. public static void main(String[] args){
  13. HumanFactory factory = new HumanFactory();
  14. Human male = factory.createMale();
  15. male.eat();
  16. male.sleep();
  17. male.beat();
  18. }
  19. }
  1. // 多个工厂方法
  2. public class HumanFactory{
  3. public static Male createMale() {
  4. return new Male();
  5. }
  6. public static Female createFemale() {
  7. return new Female();
  8. }
  9. }
  10. // 工厂测试类
  11. public class FactoryTest {
  12. public static void main(String[] args){
  13. Human male = HumanFactory.createMale();
  14. male.eat();
  15. male.sleep();
  16. male.beat();
  17. }
  18. }

抽象工厂

工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码。因为抽象工厂不太好理解,我们先看看图,然后就和代码,就比较容易理解。

  1. public interface Sender {
  2. public void Send();
  3. }
  4. public class MailSender implements Sender {
  5. @Override
  6. public void Send() {
  7. System.out.println("this is mailsender!");
  8. }
  9. }
  10. public class SmsSender implements Sender {
  11. @Override
  12. public void Send() {
  13. System.out.println("this is sms sender!");
  14. }
  15. }
  16. public class SendMailFactory implements Provider {
  17. @Override
  18. public Sender produce(){
  19. return new MailSender();
  20. }
  21. }
  22. public class SendSmsFactory implements Provider{
  23. @Override
  24. public Sender produce() {
  25. return new SmsSender();
  26. }
  27. }
  28. public interface Provider {
  29. public Sender produce();
  30. }
  31. public class Test {
  32. public static void main(String[] args) {
  33. Provider provider = new SendMailFactory();
  34. Sender sender = provider.produce();
  35. sender.Send();
  36. }
  37. }

建造者模式

  1. /**
  2. * 构建器Builder模式
  3. * Android中的AlertDialog的构建器模式
  4. */
  5. public class Lunch {
  6. private String cake;
  7. private String meat;
  8. private String milk;
  9. private String drink;
  10. public static class Builder{
  11. private String meat; //必须要初始化的参数
  12. private String cake;
  13. private String milk;
  14. private String drink;
  15. public Builder(String meat){
  16. this.meat = meat;
  17. }
  18. public Builder addCake(String cake){
  19. this.cake = cake;
  20. return this;
  21. }
  22. public Builder addMilk(String milk){
  23. this.milk = milk;
  24. return this;
  25. }
  26. public Builder addDrink(String drink){
  27. this.drink = drink;
  28. return this;
  29. }
  30. public Lunch create(){
  31. return new Lunch(this);
  32. }
  33. }
  34. private Lunch(Builder builder){
  35. this.meat = builder.meat;
  36. this.cake = builder.cake;
  37. this.milk = builder.milk;
  38. this.drink = builder.drink;
  39. }
  40. @Override
  41. public String toString() {
  42. return "Lunch [cake=" + cake + ", drink=" + drink + ", meat=" + meat
  43. + ", milk=" + milk + "]";
  44. }
  45. public static void main(String[] args) {
  46. Lunch.Builder builder = new Lunch.Builder("meat");
  47. Lunch lunch = builder.addCake("cake")
  48. .addDrink("drink")
  49. //.addMilk("milk")
  50. .create();
  51. System.out.println(lunch.toString());
  52. }
  53. }

原型模式

  1. public class Prototype implements Cloneable {
  2. public Object clone() throws CloneNotSupportedException {
  3. Prototype proto = (Prototype) super.clone();
  4. return proto;
  5. }
  6. }
  1. public class Prototype implements Cloneable, Serializable {
  2. private static final long serialVersionUID = 1L;
  3. private String string;
  4. private SerializableObject obj;
  5. /* 浅复制 */
  6. public Object clone() throws CloneNotSupportedException {
  7. Prototype proto = (Prototype) super.clone();
  8. return proto;
  9. }
  10. /* 深复制 */
  11. public Object deepClone() throws IOException, ClassNotFoundException {
  12. /* 写入当前对象的二进制流 */
  13. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  14. ObjectOutputStream oos = new ObjectOutputStream(bos);
  15. oos.writeObject(this);
  16. /* 读出二进制流产生的新对象 */
  17. ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
  18. ObjectInputStream ois = new ObjectInputStream(bis);
  19. return ois.readObject();
  20. }
  21. public String getString() {
  22. return string;
  23. }
  24. public void setString(String string) {
  25. this.string = string;
  26. }
  27. public SerializableObject getObj() {
  28. return obj;
  29. }
  30. public void setObj(SerializableObject obj) {
  31. this.obj = obj;
  32. }
  33. }
  34. class SerializableObject implements Serializable {
  35. private static final long serialVersionUID = 1L;
  36. }

参考文献
1. http://blog.csdn.net/zhangerqing/article/details/8194653
2. http://www.importnew.com/18390.html

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注