单例模式:
/**
* 单例模式
* 1.每次从getInstance()都能返回一个且唯一的一个对象。
* 2.资源共享情况下,getInstance()必须适应多线程并发访问。
* 3.提高访问性能。
* 4.懒加载(Lazy Load),在需要的时候才被构造。
*/
public class SingletonMode {
/**
* 单例模式 懒汉式 缺点:不适应多线程并发访问。
*/
public static class SingletonModeA {
private static SingletonMode instance;
public static SingletonMode getInstance() {
if (instance == null) {
instance = new SingletonMode();
}
return instance;
}
}
/***
* 单例模式 懒汉式 缺点:加锁性能瓶颈 耗时大
*/
public static class SingletonModeB {
private static SingletonModeB instance;
public static synchronized SingletonModeB getInstance() {
if (instance == null) {
instance = new SingletonModeB();
}
return instance;
}
}
/**
* 单例模式 饿汉式 缺点:不能带参数
*/
public static class SingletonModeC {
private static SingletonModeC instance = new SingletonModeC();
public static SingletonModeC getInstance() {
return instance;
}
}
/**
* 单例模式 懒汉式的 优点:同时读取实例的时候不会进行同步,没有性能缺陷;也不依赖JDK版本。
*/
public static class SingletonModeD implements Serializable {
private static class SingletonHoulder {
static final SingletonModeD sinletonMode = new SingletonModeD();
}
public static SingletonModeD getInstance() {
return SingletonHoulder.sinletonMode;
}
// private的构造函数用于避免外界直接使用new来实例化对象
private SingletonModeD() {
}
//readResolve方法应对单例对象被序列化时候
public Object readResolve() {
return getInstance();
}
}
Build模式:
/**
* build模式
* Created by LG on 2018/8/20.
*/
public class BuildMode {
private int icon;
private String title;
private String message;
private boolean isCancel;
//使用
public void use() {
BuildMode.Buidler buidler = new BuildMode.Buidler();
buidler.setIcon(1)
.setCancel(true)
.setMessage("build模式")
.setTitle("标题")
.builder();
}
private BuildMode(Buidler buidler) {
this.icon = buidler.icon;
this.title = buidler.title;
this.message = buidler.message;
this.isCancel = buidler.isCancel;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isCancel() {
return isCancel;
}
public void setCancel(boolean cancel) {
isCancel = cancel;
}
static class Buidler {
private int icon;
private String title;
private String message;
private boolean isCancel;
public int getIcon() {
return icon;
}
public Buidler setIcon(int icon) {
this.icon = icon;
return this;
}
public String getTitle() {
return title;
}
public Buidler setTitle(String title) {
this.title = title;
return this;
}
public String getMessage() {
return message;
}
public Buidler setMessage(String message) {
this.message = message;
return this;
}
public boolean isCancel() {
return isCancel;
}
public Buidler setCancel(boolean cancel) {
isCancel = cancel;
return this;
}
public BuildMode builder() {
return new BuildMode(this);
}
}
观察者模式:
/**
* 观察者模式
* Created by LG on 2018/8/20.
*/
public class ObserverMode {
// 使用
public void use() {
//添加观察者
MyObservable myObservable = new MyObservable();
MyObserver myObserver = new MyObserver(1);
myObservable.addObserver(myObserver);
//改变数据
myObservable.setName("观察者模式");
myObservable.setAge(10);
myObservable.setSex("男");
}
/**
* 被观察者
*/
class MyObservable extends Observable {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
setChanged();
notifyObservers();
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
setChanged();
notifyObservers();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
setChanged();
notifyObservers();
}
@Override
public String toString() {
return "MyPerson [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
/**
* 观察者
*/
class MyObserver implements Observer {
private int id;
private MyObservable myObservable;
public MyObserver(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public MyObservable getMyObservable() {
return myObservable;
}
public void setMyObservable(MyObservable myObservable) {
this.myObservable = myObservable;
}
@Override
public void update(Observable observable, Object o) {
this.myObservable = (MyObservable) observable;
}
}
工厂模式:
/**
* 工厂模式
* Created by LG on 2018/8/20.
*/
public class FactoryMode {
/**
* 简单工厂模式
*/
static class SimpleFactory {
public void use() {
INoodles noodles = SimpleNoodlesFactory.createNoodles(SimpleNoodlesFactory.TYPE_LZ);
noodles.desc();
}
//抽象方法
public abstract static class INoodles {
public abstract void desc();
}
//产品类
public static class LzNoodles extends INoodles {
@Override
public void desc() {
System.out.println("刀削面");
}
}
//产品类
public static class PaoNoodles extends INoodles {
@Override
public void desc() {
System.out.println("泡面");
}
}
//工厂类
public static class SimpleNoodlesFactory {
public static final int TYPE_LZ = 1;//刀削面
public static final int TYPE_PM = 2;//泡面
public static INoodles createNoodles(int type) {
switch (type) {
case TYPE_LZ:
return new LzNoodles();
case TYPE_PM:
return new PaoNoodles();
default:
return new PaoNoodles();
}
}
}
}
/**
* 抽象工厂模式
*/
static class AbstractFactory {
//使用
public void use() {
IFactory factory = new Factory();
factory.createProduct1().show();
factory.createProduct2().show();
}
interface IProduct1 {
public void show();
}
interface IProduct2 {
public void show();
}
class Product1 implements IProduct1 {
public void show() {
System.out.println("这是1型产品");
}
}
class Product2 implements IProduct2 {
public void show() {
System.out.println("这是2型产品");
}
}
interface IFactory {
public IProduct1 createProduct1();
public IProduct2 createProduct2();
}
public class Factory implements IFactory {
public IProduct1 createProduct1() {
return new Product1();
}
public IProduct2 createProduct2() {
return new Product2();
}
}
}
适配器模式:
/**
* 适配器模式
* Created by LG on 2018/8/20.
*/
public class AdapterMode {
/**
* 对象/类 适配器模式
*/
static class ClassAdapter {
public void use() {
Xiaomi mi1 = new Xiaomi();
mi1.takeAlong();
mi1.store();
}
public interface Usb {
void store();
void takeAlong();
}
public class Phone {
public void call() {
System.out.println("Phone call");
}
public void sms() {
System.out.println("Phone sms");
}
public void takeAlong() {
System.out.println("Phone takeAlong");
}
}
/**
* 类的适配器模式
* phone + Usb
* 将Phone的功能扩展到Usb里
*/
public class Xiaomi extends Phone implements Usb {
@Override
public void store() {
// TODO Auto-generated method stub
System.out.println("store implements usb");
}
}
/**
* 对象的适配器模式
*/
public class XiaomiWrapper implements Usb {
private Phone phone;
public XiaomiWrapper(Phone phone) {
this.phone = phone;
}
@Override
public void store() {
// TODO Auto-generated method stub
System.out.println("store implements usb");
}
@Override
public void takeAlong() {
// TODO Auto-generated method stub
phone.takeAlong();
}
}
/**
* 接口适配器
*/
static class ImplementAdapter {
public void use() {
Phone1 p1 = new Phone1();
Phone2 p2 = new Phone2();
p1.store();
p2.takeAlong();
}
/**
* 接口的适配器模式
* 1.借助于一个抽象类,该抽象类实现了该接口,实现了所有的方法
* 2.继承类可以选择性的实现接口中的方法
*/
public abstract class AbsPhone implements Usb {
public void store() {
System.out.println("AbsPhone implements usb's store methond");
}
public void takeAlong() {
System.out.println("AbsPhone implements usb's takeAlong methond");
}
}
public class Phone1 extends AbsPhone {
public void call() {
System.out.println("Phone1 call");
}
public void sms() {
System.out.println("Phone1 sms");
}
public void store() {
System.out.println("Phone1 need usb's store methond");
}
}
public class Phone2 extends AbsPhone {
public void call() {
System.out.println("Phone2 call");
}
public void sms() {
System.out.println("Phone2 sms");
}
public void takeAlong() {
System.out.println("Phone2 need usb's takeAlong methond");
}
}
}
}
还差一个代理模式,会及时更新。