1.单例模式
1.饿汉式
package com.serlyf.singleton;
/**
* 单例模式-饿汉式
* 1.构造私有
* 2.本类创建静态示例
* 3.getInstance()方法公开
*/
public class SingleTon01 {
//2.本类创建静态示例
private static final SingleTon01 INSTANCE = new SingleTon01();
//1.构造私有
private SingleTon01() {
}
//3.getInstance()方法公开
public static SingleTon01 getInstance() {
return INSTANCE;
}
}
2.饿汉式(静态代码块)
package com.serlyf.singleton;
/**
* 单例模式-饿汉式(静态代码块)
* 1.构造私有
* 2.静态代码块创建静态示例
* 3.getInstance()方法公开
*/
public class SingleTon02 {
//2.本类创建静态示例
private static SingleTon02 instance;
//1.构造私有
private SingleTon02() {
}
static {
instance=new SingleTon02();
}
//3.getInstance()方法公开
public static SingleTon02 getInstance() {
return instance;
}
}
3.懒汉式(线程不安全,不使用)
/**
* 单例模式-懒汉式(线程不安全,不使用)
* 1.构造私有
* 2.静态代码块创建静态示例
* 3.getInstance()方法公开
*/
public class SingleTon03 {
//2.本类创建静态示例
private static SingleTon03 instance;
//1.构造私有
private SingleTon03() {
}
//3.getInstance()方法公开
public static SingleTon03 getInstance() {
if(instance == null){
instance=new SingleTon03();
}
return instance;
}
}
4.懒汉式(线程安全,效率低,不推荐使用)
/**
* 单例模式-懒汉式(线程安全,效率低,不推荐使用)
* 1.构造私有
* 2.静态代码块创建静态示例
* 3.getInstance()方法公开
*/
public class SingleTon04 {
//2.本类创建静态示例
private static SingleTon04 instance;
//1.构造私有
private SingleTon04() {
}
//3.getInstance()方法公开
public static synchronized SingleTon04 getInstance() {
if(instance == null){
instance=new SingleTon04();
}
return instance;
}
}
5.双重检测(推荐使用)
/**
* 单例模式-双重检测(推荐使用)
* 1.构造私有
* 2.静态代码块创建静态示例
* 3.getInstance()方法公开
*/
public class SingleTon05 {
//2.本类创建静态示例
private static volatile SingleTon05 instance;
//1.构造私有
private SingleTon05() {
}
//3.getInstance()方法公开
public static synchronized SingleTon05 getInstance() {
if (instance == null){
synchronized (SingleTon05.class){
if(instance == null){
instance=new SingleTon05();
}
}
}
return instance;
}
}
6.静态内部类(推荐使用)
/**
* 单例模式-双重检测(推荐使用)
* 1.构造私有
* 2.本类定义实例对象
* 3.定义静态内部类
* 4.getInstance()方法公开
*/
public class SingleTon06 {
//2.本类创建静态示例
private static volatile SingleTon06 instance;
//1.构造私有
private SingleTon06() {
}
//3.静态内部类
private static class SingleTonInstance{
private static final SingleTon06 INSTANCE=new SingleTon06();
}
//4.getInstance()方法公开
public static synchronized SingleTon06 getInstance() {
return SingleTonInstance.INSTANCE;
}
}
7.枚举类型(推荐使用)
/**
* 单例模式-枚举类型(推荐使用)
*/
public enum SingleTon07 {
INSTANCE;
public void hello() {
System.out.println("hello~");
}
}
2工厂模式
1.简单工厂
1)定义抽象产品接口类
public interface Car {
String excute();
}
2)定义具体产品实现类
public class BenchiCar implements Car{
public String excute() {
System.out.println("奔驰车生产....");
return "OK";
}
}
public class BwmCar implements Car{
public String excute() {
System.out.println("宝马车生产....");
return "OK";
}
}
public class BydCar implements Car{
public String excute() {
System.out.println("比亚迪车生产....");
return "OK";
}
}
3)定义生产工厂类及静态获取方法
public class CarFactory {
public static Car getCar(String carType) {
Car car = null;
if ("benchi".equals(carType)) {
car = new BenchiCar();
} else if ("bwm".equals(carType)) {
car = new BwmCar();
} else if ("byd".equals(carType)) {
car = new BydCar();
}
return car;
}
}
- demo测试
public class SimpleFactoryDemo {
/**
* 1.定义抽象产品接口
* 2.定义具体产品实现类
* 3.定义生产工厂实现类及静态方法
* 4.demo测试
*
* 优点:不必关心类对象如何创建,实现了解耦,把初始化实例时的工作放到工厂里进行,使代码更容易维护
* 缺点:
* 1,一旦添加新产品就不得不修改工厂类的逻辑,这样就会造成工厂逻辑过于复杂
* 2,静态方法不能被继承和重写
* @param args
*/
public static void main(String[] args) {
Car benchi = CarFactory.getCar("benchi");
benchi.excute();
Car bwm = CarFactory.getCar("bwm");
bwm.excute();
Car byd = CarFactory.getCar("byd");
byd.excute();
}
}
2.抽象工厂
public interface Car {
String excute();
}
public class BenchiCar implements Car {
public String excute() {
System.out.println("奔驰车生产....");
return "OK";
}
}
public class BwmCar implements Car {
public String excute() {
System.out.println("宝马车生产....");
return "OK";
}
}
public class BydCar implements Car {
public String excute() {
System.out.println("比亚迪车生产....");
return "OK";
}
}
public interface Phone {
void call();
}
public class HuaweiPhone implements Phone{
public void call() {
System.out.println("华为手机....");
}
}
public class XiaomiPhone implements Phone{
public void call() {
System.out.println("小米手机....");
}
}
public interface Factory {
Phone getPhone(String type);
Car getCar(String carType);
}
public class CarFactory implements Factory{
public Phone getPhone(String type) {
return null;
}
public Car getCar(String carType) {
Car car = null;
if ("benchi".equals(carType)) {
car = new BenchiCar();
} else if ("bmw".equals(carType)) {
car = new BwmCar();
} else if ("byd".equals(carType)) {
car = new BydCar();
}
return car;
}
}
public class PhoneFactory implements Factory{
public Phone getPhone(String type) {
Phone phone = null;
if ("Huawei".equals(type)) {
phone = new HuaweiPhone();
} else if ("Xiaomi".equals(type)) {
phone = new XiaomiPhone();
}
return phone;
}
public Car getCar(String carType) {
return null;
}
}
public class AbstractFactory {
public static Factory getFactory(String type){
if("car".equals(type)){
return new CarFactory();
}else if ("phone".equals(type)){
return new PhoneFactory();
}
return null;
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
Car bwm = AbstractFactory.getFactory("car").getCar("bmw");
bwm.excute();
}
}
3.原型模式
1.浅拷贝
package com.serlyf.prototype;
import java.io.Serializable;
public class Dog implements Cloneable, Serializable {
String id;
String name;
Integer age;
@Override
public String toString() {
return "Dog{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
void run(){
System.out.println("running...");
}
public Dog() {
}
public Dog(String id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public Dog clone() {
Dog dog=null;
try {
dog = (Dog)super.clone();//q浅拷贝
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return dog;
}
}
public class PrototypeTest {
public static void main(String[] args) {
Dog dog1=new Dog();
dog1.setId("1");
dog1.setName("zhangsan");
dog1.setAge(2);
System.out.println(dog1.clone().toString());
}
}
2.深拷贝
package com.serlyf.prototype;
import java.io.*;
public class Cat implements Serializable,Cloneable {
String name;
Integer age;
Dog dog;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Dog getDog() {
return dog;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
", dog=" + dog +
'}';
}
/**
* 深拷贝第一种方式,通过clone克隆
* @return
*/
@Override
public Object clone() {
Cat cat=null;
try {
cat = (Cat)super.clone();//普通属性浅拷贝
cat.dog=dog.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return cat;
}
/**
* 通过序列化和反序列化的方式克隆
* @return
*/
public Object deepClone(){
Cat cat =null;
//创建流对象
ByteArrayInputStream bis=null;
ByteArrayOutputStream bos =null;
ObjectInputStream ois=null;
ObjectOutputStream oos =null;
try {
//序列化
bos=new ByteArrayOutputStream();
oos =new ObjectOutputStream(bos);
oos.writeObject(this);//当前对象以对象流的方式输出
//反序列化
bis =new ByteArrayInputStream(bos.toByteArray());
ois =new ObjectInputStream(bis);
cat =(Cat)ois.readObject();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
bos.close();
oos.close();
bis.close();
oos.close();
}catch (Exception e){
e.printStackTrace();
}
}
return cat;
}
}
public class PrototypeTest {
public static void main(String[] args) {
Dog dog1=new Dog();
dog1.setId("1");
dog1.setName("zhangsan");
dog1.setAge(2);
System.out.println(dog1.clone().toString());
Cat cat1 =new Cat();
cat1.setAge(3);
cat1.setName("ll");
cat1.setDog(dog1);
System.out.println(dog1.hashCode());
Cat cat2 =(Cat)cat1.clone();
System.out.println(cat2.dog.hashCode());
System.out.println(cat2.toString());
Cat cat3 =new Cat();
cat3.setAge(3);
cat3.setName("ww");
cat3.setDog(dog1);
System.out.println(dog1.hashCode());
Cat cat4 =(Cat)cat3.deepClone();
System.out.println(cat4.dog.hashCode());
System.out.println(cat4.toString());
}
}
4.建造者模式
//1.定义产品
//2.抽象构造者,定义抽象方法
//3.具体建造者,生产具体的建造方法
//4.指挥者,负责生产具体的产品
package com.serlyf.builder;
//产品
public class House {
private String basic;
private String wall;
private String roofed;
public void setBasic(String basic) {
this.basic = basic;
}
public void setWall(String wall) {
this.wall = wall;
}
public void setRoofed(String roofed) {
this.roofed = roofed;
}
public String getBasic() {
return basic;
}
public String getWall() {
return wall;
}
public String getRoofed() {
return roofed;
}
}
public abstract class HouseBuilder {
protected House house=new House();
//将建造的流程写好,抽象方法
public abstract void buildBasic();
public abstract void buildWall();
public abstract void buildRoofed();
//建造房子
public House buildHose(){
return house;
}
}
public class CommonHouse extends HouseBuilder{
@Override
public void buildBasic() {
System.out.println("普通房子Basic....");
}
@Override
public void buildWall() {
System.out.println("普通房子Wall....");
}
@Override
public void buildRoofed() {
System.out.println("普通房子Roofed....");
}
}
public class HighHouse extends HouseBuilder{
@Override
public void buildBasic() {
System.out.println("高层房子Basic....");
}
@Override
public void buildWall() {
System.out.println("高层房子Wall....");
}
@Override
public void buildRoofed() {
System.out.println("高层房子Roofed....");
}
}
//指挥者
public class HouseDirecter {
HouseBuilder houseBuilder=null;
public HouseDirecter(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}
public void setHouseBuilder(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}
public House createHouse(){
houseBuilder.buildBasic();
houseBuilder.buildWall();
houseBuilder.buildRoofed();
return houseBuilder.buildHose();
}
}
public class BuilderDemo {
public static void main(String[] args) {
///需要创建普通房子
HighHouse highHouse=new HighHouse();
//指挥者
HouseDirecter houseDirecter=new HouseDirecter(highHouse);
//产出房子
House house=houseDirecter.createHouse();
}
}
标签:return,String,void,模式,class,原型,new,设计模式,public
From: https://www.cnblogs.com/NIAN2011/p/16633435.html