Java几种设计模式

Java设计模式

设计模式的分类

创建型模式 对象的创建
结构型模式 对象的组成
行为型模式 对象的行为 

简单工厂方法模式(静态工厂方法模式)

      该模式里面需要提供一个类:工厂类(用它制造一些需要的对象)
  特点:
   构造方法私有化,外界不能直接创建它的对象
  提供静态功能,每一种静态都会产生所需要的对象...
  缺点:不利于后期维护
如果添加新的对象,需要创建它的类还需要在静态工厂类提供一些功能!

定义一个动物抽象类:
public abstract class Animal {

	public abstract void eat();
}
定义两个动物具体类,继承动物抽象类,重写里面的eat()方法
public class Cat extends Animal {

	@Override
	public void eat() {
		System.out.println("猫吃鱼....");
	}

}

public class Dog extends Animal {

	@Override
	public void eat() {
		System.out.println("狗吃骨头...");
	}

}
再创建一个动物工厂类,提供猫和狗这两种动物

public class AnimalFactory {

	private AnimalFactory() {
		
	}
	//提供猫和狗这两种动物
	public static Animal createAnimal(String type) {
		if("dog".equals(type)) {
			return new Dog();
		}else if("cat".equals(type)){
			return new Cat();
		}else {
			return null;
		}
	}
}

测试类:
public class AnimalDemo {

	public static void main(String[] args) {
			Dog dog = new Dog();
			dog.eat();
			Cat cat = new Cat();
			cat.eat();
			System.out.println("---------------");
			
			Animal a = AnimalFactory.createAnimal("cat");
			a.eat();
			 a = AnimalFactory.createAnimal("dog");
			 a.eat();
			 a = AnimalFactory.createAnimal("erdan") ;
			
			 if(a != null) {
				 a.eat(); 
			 }else {
				 System.out.println("目前工厂类没有提供该动物...");
			 }
	}

}

工厂模式

工厂方法模式:
特点:
  需要提供一个抽象类,以及每个动物的具体类和接口(工厂接口)
  该接口中的抽象方法的返回值是该抽象类
  针对每个具体动物都提供一些对应的工厂类--->实现该接口--->返回值就是需要具体的动物对象
 
  弊端:代码量大.
定义一个动物抽象类:
public abstract class Animal {

	public abstract void eat() ;
}
定义两个动物具体类,继承动物抽象类,重写里面的eat()方法
public class Cat extends Animal {
public interface Factory {

	public abstract Animal createAnimal();
}

@Overridepublic void eat() {System.out.println("猫吃鱼....");}}
定义工厂类接口 
   
public interface Factory {

	public abstract Animal createAnimal();
}

分别定义cat和dog的工厂类,实现工厂接口
public class CatFactory implements Factory {

	@Override
	public Animal createAnimal() {
	
		return new Cat();
	}

}

public class DogFactory implements Factory {

	@Override
	public Animal createAnimal() {
		
		return new Dog();
	}

}
测试类
public class AnimalDemo {

	public static void main(String[] args) {
		//猫工厂生产猫
		DogFactory d = new DogFactory();
		Animal a = d.createAnimal();
		a.eat();
		System.out.println("------------");
		//狗工厂生产狗
		CatFactory c = new CatFactory();
		 a = c.createAnimal();
		 a.eat();
	}
}

单例模式


  定义:在内存中始终只有一个对象
  饿汉式
  特点:当前某一个一加载,就会创建一个对象
  需要将该类的无参构造私有化
  在成员变量创建该类的实例
需要提供公共的访问方法(实际开发中使用这种方式)
  懒汉式
  特点:并不是加载某个类就直接创建对象,而是需要的时候在创建对象
  1)懒加载(延迟加载)
2)线程的安全问题
  线程安全问题检验标准:
  a:是否是多线程环境
  b:是否有共享数据
   c:是否有多条语句对共享数据进行操作!
 
   使用同步代码块或同步方法(synchronized)进行解决
饿汉式:

public class Student {

	//无参构造私有化,目的是为了让外界不创建对象
	private Student() {
		
	}
	//在成员变量的位置创建该类的实例
	//静态只能访问静态,所以需要加上静态修饰
	//为了不让外界修改当前这个实例,所以加入private修饰
	private static Student s = new Student();
	
	public static Student getStudent() {
		return s;	
	}
public class StudentDemo {

	public static void main(String[] args) {
		//利用静态的公共访问方法创建对象,防止外界修改当前Student类的中实例
		Student s1 = Student.getStudent() ;
		Student s2 = Student.getStudent();
		System.out.println(s1==s2);
		System.out.println(s1);
		System.out.println(s2);


	}
}
懒汉式:

public class Teacher {

	private Teacher() {}
	
	//在成员变量位置声明变量
		private static Teacher t = null ; //共享数据
		
		//提供公共的方法访问
		public synchronized static Teacher getTeacher(){ //Teacher.class锁对象
			
			//判断 当当前该对象没有更多引用的时候,才创建对象
			if(t==null){
				
				t = new Teacher() ;
			}
			return t ;
		}

}

public class TeacherDemo {

	public static void main(String[] args) {
		
		Teacher t1 = Teacher.getTeacher();
		Teacher t2 = Teacher.getTeacher();
		System.out.println(t1==t2);//true
		System.out.println(t1);
		System.out.println(t2);
		
	}
}


面试题: 简单阐述下单例模式有几种,并且写出一个单例模式代码!(写懒汉式),
   就想问你能否想到懒汉式中多线程的安全问题...
 
  
   如果是开发中,使用单例模式,那么使用的   --->饿汉式(不会出现问题的单例模式)
 
  如果是面试中,使用单例模式,----->懒汉式(想问你能否想到懒汉式中多线程的安全问题以及解决办法)



猜你喜欢

转载自blog.csdn.net/dreamer_96/article/details/78907696