目 录CONTENT

文章目录

单例模式实现

不争
2024-01-02 / 0 评论 / 0 点赞 / 23 阅读 / 9347 字

单例模式实现

懒汉式

public class Signleton {
    private static Signleton instance = null; //静态私有成员变量

    private Signleton(){ //私有构造函数

    }
    //静态公有工厂方法,返回唯一的实例
    public static Signleton getInstance(){
        if (instance == null){
            instance = new Signleton();
        }
        return instance;
    }
}

懒汉式(双重检查)

public class Signleton {
    private volatile static Signleton instance = null;

    private Signleton(){}

    public static Signleton getInstance(){
        if (instance == null){
            synchronized (Signleton.class){ //synchronized 同步锁
                if (instance == null){
                    instance = new Signleton();
                }
            }
        }
        return instance;
    }
}

饿汉式

public class Signleton {
    private static final Signleton instance = new Signleton(); //静态私有成员变量

    private Signleton(){}

    public static Signleton getInstance(){
        return instance;
    }
}

简单工厂

abstract class Product{
    //所有产品类的公共业务方法
    public void methodSame(){
        System.out.println("公共方法");
        //公共方法的实现
    }
    //声明抽象业务方法
    public abstract void mmethodDiff();
}

class ConcreateProductA extends Product{

    @Override
    public void mmethodDiff() {
        System.out.println("产品A");
    }
}
class ConcreateProductB extends Product{

    @Override
    public void mmethodDiff() {
        System.out.println("产品B");
    }
}

//通过if判断 用户传进来的产品,然后创建不同的对象
class Factory{
    //静态工厂方法
    public static Product getProduct(String arg){
        Product product = null;
        if (arg.equalsIgnoreCase("A")){
            product = new ConcreateProductA();
        } else if (arg.equalsIgnoreCase("B")){
            product = new ConcreateProductB();
        }
        return product;

    }
}

简单工厂(咖啡店点餐)

咖啡店点餐UML图

//定义抽象产品 咖啡
abstract class Coffee{
    //定义公共方法
    public void addMilk(){
        System.out.println("加奶");
    }
    public void addsugar(){
        System.out.println("加糖");
    }
    public abstract void getName();
}

//具体产品
class AmericanCoffee extends Coffee{

    @Override
    public void getName() {
        System.out.println("美式咖啡");
    }
}

class LatteCoffee extends Coffee{

    @Override
    public void getName() {
        System.out.println("拿铁咖啡");
    }
}

//静态工厂
class CoffeeFactory{
    //静态工厂方法
    public static Coffee createCoffee(String type){
        Coffee coffee = null;
        if (type.equalsIgnoreCase("american")){
            coffee = new AmericanCoffee();
        }else if (type.equalsIgnoreCase("latee")){
            coffee = new LatteCoffee();
        }
        return coffee;
    }
}

image-20220610181704149

工厂模式

image-20220611165105438

package com.hspedu.Test12.Factory1;
//一个工厂创建一个产品
public class Client {
    public static void main(String[] args) {
        new HaierTVFactory().produceTV().play();
        new HisenseTVFactory().produceTV().play();

    }
}

//定义产品接口
interface TV{
    public void play();
}
//定义产品工厂
interface TVFactory{
    public TV produceTV();
}

class HaierTV implements TV{

    @Override
    public void play() {
        System.out.println("海尔电视!");
    }
}

class HisenseTV implements TV{

    @Override
    public void play() {
        System.out.println("海信电视!");
    }
}

class HaierTVFactory implements TVFactory{

    @Override
    public TV produceTV() {
        return new HaierTV();
    }
}

class HisenseTVFactory implements TVFactory{

    @Override
    public TV produceTV() {
        return new HisenseTV();
    }
}

2、背景:小成有一间塑料加工厂(仅生产A类产品);随着客户需求的变化,客户需要生产B类产品

package com.hspedu.Test12.Factory2;
//目前只有一个产品
public class Client {
    public static void main(String[] args) {
        new FactoryA().Factory().creat();
    }
}

//抽象产品
interface produce{
    void creat();
}
//具体产品A
class produceA implements produce{

    @Override
    public void creat() {
        System.out.println("生产A!");
    }
}
//抽象工厂
interface Factory{
    public produce Factory();
}

//具体工厂
class FactoryA implements Factory{

    @Override
    public produce Factory() {
        return new produceA();
    }
}

冲突:改变原有塑料加工厂的配置和变化非常困难,假设下一次客户需要再发生变化,再次改变将增大非常大的成本;

解决方案:小成决定置办塑料分厂B来生产B类产品;

package com.hspedu.Test12.Factory2;

public class Client {
    public static void main(String[] args) {
        new FactoryA().Factory().creat();
        new FactoryB().Factory().creat();
    }
}

//抽象产品
interface produce{
    void creat();
}
//具体产品A
class produceA implements produce{

    @Override
    public void creat() {
        System.out.println("生产A!");
    }
}
class produceB implements produce{

    @Override
    public void creat() {
        System.out.println("生产B!");
    }
}
//抽象工厂
interface Factory{
    public produce Factory();
}

//具体工厂
class FactoryA implements Factory{

    @Override
    public produce Factory() {
        return new produceA();
    }
}

class FactoryB implements Factory{

    @Override
    public produce Factory() {
        return new produceB();
    }
}

抽象工厂练习

image-20220611173508423

package com.hspedu.Test12.Factory3;

public class Client {
    public static void main(String[] args) {
        new ConcreteFactory1().product1().show();
        new ConcreteFactory1().product2().show();

        new ConcreteFactory2().product1().show();
        new ConcreteFactory2().product2().show();

    }
}

//抽象产品1
interface Product1{
    void show();
}
//抽象产品2
interface Product2{
    void show();
}
//具体产品
class ConcreteProduct11 implements Product1{

    @Override
    public void show() {
        System.out.println("产品11");
    }
}

class ConcreteProduct12 implements Product1{
    @Override
    public void show() {
        System.out.println("产品12");
    }
}
class ConcreteProduct21 implements Product2{
    @Override
    public void show() {
        System.out.println("产品21");
    }
}
class ConcreteProdu22 implements Product2{
    @Override
    public void show() {
        System.out.println("产品22");
    }
}

//抽象工厂
interface AbstractFactory {
    public Product1 product1();

    public Product2 product2();
}
//具体工厂
class ConcreteFactory1 implements AbstractFactory{

    @Override
    public Product1 product1() {
        return new ConcreteProduct11();
    }

    @Override
    public Product2 product2() {
        return new ConcreteProduct21();
    }
}
class ConcreteFactory2 implements AbstractFactory{

    @Override
    public Product1 product1() {
        return new ConcreteProduct12();
    }

    @Override
    public Product2 product2() {
        return new ConcreteProdu22();
    }
}

建造者模式

  1. 抽象建造者
  2. 具体建造者
  3. 产品
  4. 指挥者
package com.hspedu.Test12.BuilderPattern;



public class Client {
    public static void main(String[] args) {
        KFCWaiter kfcWaiter = new KFCWaiter();
        kfcWaiter.setMb(new SubMealBuilderA());
        Meal meal;
        meal = kfcWaiter.construct();
        System.out.println(meal);
    }
}

class Meal{
    private String food;
    private String drink;

    public Meal() {
    }

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    public String getDrink() {
        return drink;
    }

    public void setDrink(String drink) {
        this.drink = drink;
    }

    @Override
    public String toString() {
        return "Meal{" +
                "food='" + food + '\'' +
                ", drink='" + drink + '\'' +
                '}';
    }
}
//抽象建造者
abstract class MealBuilder{
    protected Meal meal = new Meal();

    public abstract void buildFood();
    public abstract void builDrink();
    public Meal getMeal(){
        return meal;
    }
}

//具体建造者
class SubMealBuilderA extends MealBuilder{

    @Override
    public void buildFood() {
        //System.out.println("鸡腿堡");
        meal.setFood("鸡腿堡");
    }

    @Override
    public void builDrink() {
        //System.out.println("可乐");
        meal.setFood("可乐");

    }
}

class SubMealBuilderB extends MealBuilder{

    @Override
    public void buildFood() {
        //System.out.println("鸡肉卷");
        meal.setFood("鸡肉卷");

    }

    @Override
    public void builDrink() {
        //System.out.println("橙汁");
        meal.setFood("橙汁");

    }
}
//指挥者
class KFCWaiter{
    private MealBuilder mb;

    public void setMb(MealBuilder mb) {
        this.mb = mb;
    }
    public Meal construct(){
        mb.buildFood();
        mb.builDrink();
        Meal meal = mb.getMeal();
        return meal;
    }
}
0

评论区