1.单例设计模式
--1.常见的设计模式包含23种:
总体来说分为三大类:
创建型模式,共五种:工厂方法模式、抽象工厂模式、 单例模式、
建造者模式、原型模式。
结构型模式,共七种:适配器模式、 装饰器模式、代理模式、外观
模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、
迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访
问者模式、中介者模式、解释器模式。
--2.单例设计模式
--2.1保证一个类的对象唯一性。
--2.2私有构造方法。
--2.3在本类的成员位置,创建出自己类对象
--2.4提供公共方法,返回创建的对象。
--2.5单例模式在Java的应用中,Java.lang.runtime
--3 单例-饿汉式
public class singleHungry {
private singleHungry(){
}
private static final singleHungry s = new singleHungry();
public static singleHungry getInstance(){
return s;
}
}
Test:
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
singleHungry singleHungry = com.itheima.workmode.singleHungry.getInstance();
System.out.println(singleHungry);
}
}
--3 单例-懒汉式 : 在多线程并发时线程不安全
com.workmode.singleLazy@23762cd
com.workmode.singleLazy@23762cd
com.workmode.singleLazy@5e990133
com.workmode.singleLazy@6ee35fcc
com.workmode.singleLazy@23762cd
--3.1 懒汉式编写
public class singleLazy {
private singleLazy(){
}
private static singleLazy singleLazy = null;
public static singleLazy getInstance(){
if (singleLazy == null) {
singleLazy = new singleLazy();
}
return singleLazy;
}
}
--3.2编写多线程测试
public class Run implements Runnable {
public void run(){
for (int i = 0; i < 30; i++) {
singleLazy s = singleLazy.getInstance();
System.out.println(s);
}
}
}
--3.3测试
public class Test {
public static void main(String[] args) {
Run run = new Run();
new Thread(run).start();
new Thread(run).start();
new Thread(run).start();
new Thread(run).start();
}
}
1.1单例模式,饿汉式和懒汉式比较
--1.1.1 懒汉式解决线程不安全的方法:加同步锁,不会出现两次或两次以上new 对象
第二个线程调用方法getInstance()的时候,变量s,已经不是Null,被前面的的线程new过。
第二个线程,不需要new,直接return,大大提高了效率。
同步锁最佳状态:只工作一次。
加同步方法也是可以的,但是影响效率。
public class singleLazy {
private singleLazy(){
}
private static singleLazy singleLazy = null;
public static singleLazy getInstance(){
if (singleLazy == null) {
synchronized (singleLazy) {
if (singleLazy == null) {
singleLazy = new singleLazy();
}
}
}
return singleLazy;
}
}
2.工厂模式
--2.1让一个类专门创作对象
--2.2简单工厂模式:将所有的创建对象的任务交给工厂类实现,找工厂类获取即可。
--2.3创建Animal
public abstract class Animal {
public abstract void eat();
}
--2.4创建Cat
public class Cat extends Animal{
public void eat() {
System.out.println("cat eat fish");
}
}
--2.5创建Dog
public class Dog extends Animal{
@Override
public void eat() {
System.out.println("Dog eat meat");
}
}
--2.6创建factory
public class Factory {
public static Animal createAnimal(String name){
if ("cat".equals(name)) {
return new Cat();
}else if ("dog".equals(name)) {
return new Dog();
}
return null;
}
}
--2.7test
public class Test {
public static void main(String[] args) {
Animal animal = Factory.createAnimal("cat");
animal.eat();
animal = Factory.createAnimal("dog");
animal.eat();
}
}
2.1方法工厂
--2.1.1解决简单工厂模式,用户输入不存在对象时返回null的情况。
--Animal
public abstract class Animal {
public abstract void eat();
}
--Cat
public class Cat extends Animal{
public void eat() {
System.out.println("cat eat fish");
}
}
--CatFactory
public class CatFactory implements Factory {
@Override
public Animal createAnimal() {
return new Cat();
}
}
--Dog
public class Dog extends Animal{
@Override
public void eat() {
System.out.println("Dog eat meat");
}
}
--DogFactory
public class DogFactory implements Factory{
@Override
public Animal createAnimal() {
return new Dog();
}
}
--Factory
public interface Factory {
public abstract Animal createAnimal();
}
--test
public class Test {
public static void main(String[] args) {
Factory factory = new CatFactory();
Animal animal = factory.createAnimal();
animal.eat();
factory = new DogFactory();
Animal animal2 = factory.createAnimal();
animal2.eat();
}
}