23种设计模式介绍----创建型模式(代码片段)

朝北教室的风筝 朝北教室的风筝     2022-11-09     750

关键词:

这一篇为第一篇介绍创建型模式,创建型模式一共有5种:

  • 工厂模式 
  • 抽象工厂模式 
  • 单例模式
  • 构造者模式
  • 原型模式   

一、工厂模式

  定义:定义一个用于创建对象的接口,让子类决定实例化哪一个类。使一个类的实例化延迟到其子类

  适用:当一个类不知道它所必须创建的对象的类的时候

  类图:

 

  例子代码: 

interface IProduct
class ProductA implements IProduct
class ProductB implements IProduct
interface IFactory
    public IProduct getProduct();

class FactoryA implements IFactory
    public IProduct getProduct()
        return new ProductA();
    

class FactoryB implements IFactory
    public IProduct getProduct()
        return new ProductB();
    

// 工厂方法
class Factory 
    public IProduct getProductA()
        return new ProductA();
    
    public IProduct getProductB()
        return new ProductB();
    
    public IProduct getProduct(int type)
        if (type==1)
            return new ProductA();
        else
            return new ProductB();
        
    

public class TestFactory 
    public static void test()
        IFactory factory = new FactoryA();
        IProduct product =factory.getProduct();
        // 
        factory = new FactoryB();
        product =factory.getProduct();
    

 

二、抽象工厂模式

  定义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类

  适用:一个系统要独立于它的产品的创建、组合和表示时

  与工厂模式的区别:工厂模式的一个工厂接口的子类只能实例化一个产品;抽象工厂能实例多个产品

  类图:

 

  例子代码: 

// 产品1
interface IProduct1
class Product1A implements IProduct1
// 扩展产品1 B系列
class Product1B implements IProduct1
// 产品2
interface IProduct2
class Product2A implements IProduct2
// 扩展产品2 B系列
class Product2B implements IProduct2
// 工厂
interface IFactory
    public IProduct1 getProduct1();
    public IProduct2 getProduct2();
;
// 工厂 A ,生产A系列产品
class FactoryA implements IFactory
    public IProduct1 getProduct1()
        return new Product1A();
    ;
    public IProduct2 getProduct2()
        return new Product2A();
    ;

// 工厂 B ,生产B系列产品
class FactoryB implements IFactory
    public IProduct1 getProduct1()
        return new Product1B();
    ;
    public IProduct2 getProduct2()
        return new Product2B();
    ;

public class testAbstractFactory 
    public void test()
        IFactory factory = new FactoryA();
        IProduct1 product1A = (IProduct1)factory.getProduct1();
        IProduct2 product2A = (IProduct2)factory.getProduct2();


        // 如果扩展产品系列B时,添加 FactoryB、ProductB即可,不需要修改原来代码
        factory = new FactoryB();
        IProduct1 product1B = (IProduct1)factory.getProduct1();
        IProduct2 product2B = (IProduct2)factory.getProduct2();

    

 

三、单例模式

   具体的请参考:Java设计模式 - 单例模式

  单例模式确保某个类只有一个实例,而且自行实现 ,单例模式有以下特点:
  1、单例类只能有一个实例。
  2、单例类必须自己创建自己的唯一实例。
  3、单例类必须给所有其他对象提供这一实例。

定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点。

  适用:当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时

    

 四、构造者模式 

  定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

  适用:当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时

     也可以参考:Java设计模式-Builder构造者模式

 

  例子代码: 

class Person
    private String name;
    private String address;
    private int age;
    private int sex;
    private int height;
    private int weight;
    public void setName(String name) this.name = name;
    public String getName() return name;
    public void setAddress(String address) this.address = address;
    public String getAddress() return address;
    public void setAge(int age) this.age = age;
    public int getAge() return age;
    public void setSex(int sex) this.sex = sex;
    public int getSex() return sex;
    public void setHeight(int height) this.height = height;
    public int getHeight() return height;
    public void setWeight(int weight) this.weight = weight;
    public int getWeight() return weight;

class PersonBuilder
    private Person person;
    public PersonBuilder()
        this.person = new Person();
    
    public PersonBuilder name(String name)
        this.person.setName(name);
        return this;
    
    public PersonBuilder address(String address)
        this.person.setAddress(address);
        return this;
    
    public PersonBuilder age(int age)
        this.person.setAge(age);
        return this;
    
    public PersonBuilder sex(int sex)
        this.person.setSex(sex);
        return this;
    
    public PersonBuilder height(int height)
        this.person.setHeight(height);
        return this;
    
    public PersonBuilder weight(int weight)
        this.person.setWeight(weight);
        return this;
    

public class TestBuilder 
    public test()
        PersonBuilder builder = new PersonBuilder();
        Person person = builder.name("lion")
                .address("america")
                .age(18)
                .sex(2)
                .height(180)
                .weight(150);
    

 

 五、原型模式

  定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象

  适用:当要实例化的类是在运行时刻指定时;或者需要创建多个对象并且这些对象内部状态相差不大

  例子代码: 

class Car implements Cloneable
    private int id;
    public int getId() return id;
    public void setId(int id) this.id = id;

    public Car clone()
        try 
            return (Car)super.clone();
        catch (CloneNotSupportedException e) 
            e.printStackTrace();
            return null;
        
    

class Prototype implements Cloneable
    private int id;
    private Car car;
    public Car getCar() return car;
    public void setCar(Car car) this.car = car;
    public int getId() return id;
    public void setId(int id) this.id = id;
    public Object clone()
        try 
            boolean deep = true;
            if (deep)
                /**
                 * 深复制,复制出了两辆车
                 * */
                Prototype prototype = (Prototype)super.clone();
                prototype.setCar((Car)this.car.clone());
                // 继续复制其他引用对象
                return prototype;

            else
                /**
                 * 浅复制 ,是同一辆车
                 * */
                return super.clone();
            
         catch (CloneNotSupportedException e) 
            e.printStackTrace();
            return null;
        
    

public class TestPrototype 
    public void test()
        Prototype p1 = new Prototype();
        p1.setCar(new Car());
        p1.setId(1);
        // 复制
        Prototype p2 = (Prototype)p1.clone();
        p2.setId(2);
    
 

 

 
 

23种设计模式创建型模式详细介绍之工厂模式(代码片段)

23种设计模式【创建型模式】详细介绍之【工厂模式】设计模式的分类和应用场景总结工厂模式抽象工厂模式设计模式的分类和应用场景总结可以查看专栏设计模式:设计模式工厂模式工厂模式是一种创建型设计模式,它... 查看详情

23种设计模式创建型模式详细介绍之工厂模式(代码片段)

23种设计模式【创建型模式】详细介绍之【工厂模式】设计模式的分类和应用场景总结工厂模式抽象工厂模式设计模式的分类和应用场景总结可以查看专栏设计模式:设计模式工厂模式工厂模式是一种创建型设计模式,它... 查看详情

23种设计模式创建型模式详细介绍之工厂模式(代码片段)

23种设计模式【创建型模式】详细介绍之【工厂模式】设计模式的分类和应用场景总结工厂模式抽象工厂模式设计模式的分类和应用场景总结可以查看专栏设计模式:设计模式工厂模式工厂模式是一种创建型设计模式,它... 查看详情

23种设计模式介绍----创建型模式

由于设计模式篇幅比较大,如果在一篇文章讲完所有的设计模式的话不利于阅读。于是我把它分为三篇文章23种设计模式介绍(一)----创建型模式23种设计模式介绍(二)----结构型模式23种设计模式介绍(三)----行为型模式 ... 查看详情

23种设计模式介绍----结构型模式(代码片段)

由于设计模式篇幅比较大,如果在一篇文章讲完所有的设计模式的话不利于阅读。于是我把它分为三篇文章23种设计模式介绍(一)----创建型模式23种设计模式介绍(二)----结构型模式23种设计模式介绍(三)----行为型模式  ... 查看详情

23种设计模式----创建型模式(代码片段)

创建型模式的主要关注点是“怎样创建对象?”,它的主要特点是“将对象的创建与使用分离”。这样可以降低系统的耦合度,使用者不需要关注对象的创建细节,对象的创建由相关的工厂来完成。创建型模式分为... 查看详情

23种设计模式介绍----行为型模式

 由于设计模式篇幅比较大,如果在一篇文章讲完所有的设计模式的话不利于阅读。于是我把它分为三篇文章23种设计模式介绍(一)----创建型模式23种设计模式介绍(二)----结构型模式23种设计模式介绍(三)----行为型模式... 查看详情

23种设计模式介绍----行为型模式

  由于设计模式篇幅比较大,如果在一篇文章讲完所有的设计模式的话不利于阅读。于是我把它分为三篇文章23种设计模式介绍(一)----创建型模式23种设计模式介绍(二)----结构型模式23种设计模式介绍(三)----行为型... 查看详情

23种设计模式归纳总结——创建型(代码片段)

目录1.单例模式2.工厂模式3.Builder模式4.原型模式1.单例模式1.饿汉式:在类加载时进行实例的创建与初始化publicclassIdGeneratorprivateAtomicLongid=newAtomicLong(0);privatestaticfinalIdGeneratorinstance=newIdGenerator();privateIdGenerator( 查看详情

23种设计模式归纳总结——创建型(代码片段)

目录1.单例模式2.工厂模式3.Builder模式4.原型模式主要解决“对象的创建”问题,将创建和使用代码解耦1.单例模式1.饿汉式:在类加载时进行实例的创建与初始化publicclassIdGeneratorprivateAtomicLongid=newAtomicLong(0);privatestaticfinalIdGen... 查看详情

23种设计模式归纳总结——创建型(代码片段)

目录1.单例模式2.工厂模式3.Builder模式4.原型模式主要解决“对象的创建”问题,将创建和使用代码解耦1.单例模式1.饿汉式:在类加载时进行实例的创建与初始化publicclassIdGeneratorprivateAtomicLongid=newAtomicLong(0);privatestaticfinalIdGen... 查看详情

23种设计模式----创建型模式(代码片段)

创建型模式的主要关注点是:如何创建对象?它主要目的是将对象的创建和使用分离。其中包括:单例(Singleton)模式:某个类只能生成一个实例,该类提供了一个全局访问点供外部获取该实例,其拓展是有限多例模式。原型(... 查看详情

面向对象23种设计模式系列-创建型设计模式(代码片段)

本章是面向对象23种设计模式系列开篇,首先我们来看下什么是设计模式?面向对象23种设计模式:  1、面向对象语言开发过程中,遇到的种种场景和问题,提出了解决方案和思路,沉淀下来就变成了设计模式。  2、解决具... 查看详情

23种设计模式归纳总结——创建型(代码片段)

目录1.单例模式2.工厂模式3.Builder模式4.原型模式主要解决“对象的创建”问题,将创建和使用代码解耦1.单例模式1.饿汉式:在类加载时进行实例的创建与初始化publicclassIdGeneratorprivateAtomicLongid=newAtomicLong(0);privatestaticfinalIdGen... 查看详情

23种设计模式总结

基础介绍:【设计模式】——基础入门创建型:【设计模式|创建型】——单例模式【设计模式|创建型】——工厂模式【设计模式|创建型】——原型模式【设计模式|创建型】——建造者模式结构型:【设计模式|结构型】——适... 查看详情

23种设计模式介绍以及单例模式的学习

1、GOF23设计模式总共分成创建型模式、结构型模式和行为型模式三种:    a、创建型模式:    -单例模式、工厂模式、抽象工厂模式、建造者模式、原型模式    b、构建型模式: &nb... 查看详情

23种设计模式的分类和应用场景总结设计模式

23种设计模式的分类和应用场景总结【设计模式】设计模式分类创建型模式结构型模式行为型模式设计模式的几种原则应用场景总结各种模式的详细介绍创建型模式设计模式分类23种设计模式可以分为三大类:创建型模式、结... 查看详情

23种设计模式的分类和应用场景总结设计模式

23种设计模式的分类和应用场景总结【设计模式】设计模式分类创建型模式结构型模式行为型模式设计模式的几种原则应用场景总结各种模式的详细介绍创建型模式设计模式分类23种设计模式可以分为三大类:创建型模式、结... 查看详情