1.枚举
枚举概述:针对对象个数已经固定,并且个数有限
枚举类中可以有方法、构造方法、属性,但是构造方法不能是公共的且枚举类不能被继承;
public enum Sex {
male("男"),
female("女");
public String test(){
return "ok";
}
private String cnName;
public String cnName(){
return this.cnName;
}
Sex(String name){
this.cnName=name;
}
}
public class EnumTest {
public static void main(String[] args) {
System.out.println(Sex.male.ordinal());
System.out.println(Sex.female.ordinal());
System.out.println(Sex.valueOf("male")==Sex.male);
System.out.println(Sex.male.name());
System.out.println(Sex.male.cnName());
}
}
2.设计模式
2.1 单例模式
概述:虚拟机中这个类的实例只有一个
方法1.饿汉式(在开始时就创建好)
public class singletonDemo1 {
//饿汉式 刚开始就创建
public singletonDemo1() {
}
private static final singletonDemo1 me=new singletonDemo1();
public static singletonDemo1 getMe(){
return me;
}
}
方法2.懒汉式(在调用时创建)
public class singletonDemo2 {
//懒汉式 用到时才创建
public singletonDemo2() {
}
private static singletonDemo2 me;
public static singletonDemo2 getMe(){
return me=new singletonDemo2();
}
}
方法3.利用枚举类实现单例,属于饿汉式
public enum singletonDemo3 {
me;
}
方法4.懒汉式更加体现 (静态内部类创建了它的唯一实例)
public class singletonDemo4 {
static {
System.out.println("singletonDemo4类加载了");
}
public singletonDemo4() {
}
static class holder{
static {
System.out.println("holder类加载了");
}
static singletonDemo4 me=new singletonDemo4();
}
public static singletonDemo4 getme(){
return holder.me;
}
}
2.2 享元模式
享元模式提倡重用已有的对象,而不是创建新的对象
public class flyWeightDemo {
//享元模式 Integer的享元范围 -128 ~ 127
public static void main(String[] args) {
System.out.println(Integer.valueOf(1)==Integer.valueOf(1));
System.out.println(Integer.valueOf(300)==Integer.valueOf(300));
}
}
2.3 原型模式
根据已有对象来创建新的对象, 克隆
使用场景,当对象属性很多,希望新的对象的大部分属性从原有对象复制而来
public class User implements Cloneable {
private String name;
private int age;
// ...
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
2.4 建造器模式
建造器模式的采用是让我们创建对象的过程更为灵活。适用于一步一步构建一个较为复杂的对象
public class Person {
// 目的:让我们创建对象的过程更为灵活。适用于一步一步构建一个较为复杂的对象
private String name;
private int age;
private String weight;
private String height;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getWeight() {
return weight;
}
public String getHeight() {
return height;
}
public Person(String name, int age, String weight, String height) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}
public static class PersonBuilder{
private String name;
private int age;
private String weight;
private String height;
public PersonBuilder name(String name) {
this.name = name;
return this;
}
public PersonBuilder age(int age) {
this.age = age;
return this;
}
public PersonBuilder height(String height) {
this.height = height;
return this;
}
public PersonBuilder weight(String weight) {
this.weight = weight;
return this;
}
public Person build(){
return new Person(this.name,this.age,this.weight,this.height);
}
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", weight='" + weight + '\'' +
", height='" + height + '\'' +
'}';
}
}
public class builderDemo {
public static void main(String[] args) {
Person personBuilder = new Person.PersonBuilder()
.name("张三")
.age(18)
.height("1.78米")
.weight("50kg")
.build();
System.out.println(personBuilder);
}
}
2.5 迭代器模式
以一种一致的对集合内的元素进行遍历,而不用在乎集合内的数据结构
扫描二维码关注公众号,回复:
5402342 查看本文章
![](/qrcode.jpg)
for(Object o : 集合){
Iterator iter = 集合.iterator();
while(iter.hasNext()) {
iter.next();
}
}
ArrayList 数组
LinkedList 链表
HashSet 数组+链表
TreeSet 二叉搜索树-》红黑树
2.6策略模式
java 集合或数组的排序算法
Collections.sort
Arrays.sort
基本类型 双基点快速排序
对象类型 TimSort (早期使用归并排序)
规模小 插入排序
排序算法是固定的,排序的规则能否固定?–》 不能
把排序的规则抽取出来,形成比较器接口(Comparator),不同比较器的实现就称为策略
open close 开闭原则
算法不能改-- 体现的是close原则
比较器可以改 – 体现的是open原则
public class StrategyDemo {
public static void main(String[] args) {
ArrayList<student> list = new ArrayList<>();
list.add(new student("张三",18));
list.add(new student("李四",19));
list.add(new student("王五",16));
Collections.sort(list, (o1, o2) -> o1.getAge()-o2.getAge());
System.out.println(list);
}
}
class student{
private String name;
private int age;
public student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}