你们好!今天我分享一下JAVA中23种模式几个常用的模式。我简单介绍观察者模式、工厂模式、装饰模式、单例模式这4种模式。
观察者模式定义:一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知,此种模式通常被用来实现事件处理系统。
一般这种模式的架构创建观察者抽象父类和被观察者抽象父类,在子类写具体方法。
具体介绍一下:
例如:
/**
* 被观察者的父类抽象
*
*/
public interface Subject {
public ArrayList
public void addObserver(Observer obs); //创建3个类,会在具体类进行描述。
public void removeObserver(Observer obs);
public void notifyObserver(float price);
}
/**
* 被观察者的具体类
*
*/
public class IPhone20 implements Subject{
// 添加观察对象
public void addObserver(Observer obs) {
obsList.add(obs);
}
// 移除观察对象
public void removeObserver(Observer obs) {
obsList.remove(obs);
}
// 通知所有观察对象
public void notifyObserver(float price) {
for(Observer obs : obsList){
obs.eventOne(price);
}}}
/**
* 观察者父类抽象
*
*/
public interface Observer {
public void eventOne(float price);
}
/**
* 观察者的具体类
*
*/
public class Student implements Observer{
private String name;
public Student(String name){
this.name = name;
}
@Override
public void eventOne(float price) {
System.out.println(name+"同学你很幸运,苹果20周年降价了,现价:"+price);
}}
//主函数,进行测试
public class Test {
public static void main(String[] args) {
//被观察者对象
Subject iphone = new IPhone10();
//观察者对象
Observer stu = new Student("小吴");
//观察者观察被观察者
iphone.addObserver(stu);
//手机降价了
iphone.notifyObserver(5999);
System.out.println("过了几天.....");
iphone.notifyObserver(4999);
2. 工厂模式:介绍一下静态工厂模式,顾名思义就是用静态方法实现的,比较简单! 工厂模式是由一个工厂对象决定创建出哪一个具体实例。
具体例子:
/**
* 工厂类,用于代工生产实体对象
*
*/
public class Factory {
public static Computer createComputer(String model,String type,String size){
Computer computer = null;
//跟进传递的参数,创建不同类型的对象
if("taishi".equals(model)){
computer = new TaishiCompter(type,size);
}else if("nodebook".equals(model)){
computer = new NodeBookCompter(type,size);
}else if("pad".equals(model)){
computer = new PadComputer(type,size);
}else{
System.out.println("您提供的要求,我们完成不了,没这个技术!后续继续改进");}
return computer;
}}
/**
* 电脑类
*
*/
public abstract class Computer {
public String type;
public String size;
public Computer(String type, String size) {
super();
this.type = type;
this.size = size;}
//运行游戏的函数
public abstract void runGame();}
//电脑类的子类,笔记本电脑。
public class NodeBookCompter extends Computer{
public NodeBookCompter(String type, String size) {
super(type, size);
}
public void runGame() {
System.out.println(type+size+"运行游戏还行!");
}}
//电脑类的子类,台式电脑。
public class TaishiCompter extends Computer{
public TaishiCompter(String type, String size) {
super(type, size);
}
public void runGame() {
System.out.println(type+size+"运行游戏就是爽!");
}}
//测试类
public class Test {
public static void main(String[] args) {
//原始创建对象,耦合性太高
//工厂模式可以解耦,降低耦合性
//需要两台电脑
Computer comput1 = Factory.createComputer("taishi","戴尔","8G");
Computer comput2 = Factory.createComputer("nodebook","联想","16G");
comput1.runGame();
comput2.runGame();
3. 装饰模式:动态地给一个对象添加一些额外的职责。就增添功能来说,Decorator模式相比生成子类更为灵活。
/**
* 饼类,抽象父类
*
*/
public interface Cake {
//价格获取
public float price();
//描述
public String description();
}
public class CrasphingCake implements Cake{
public float price() {
return 3.0f;
}
public String description() {
return "ԭζ˖ץҽ";
}
}
/**
* 装饰者类
*
*/
public abstract class Decorator implements Cake{
//强关联Cake
private Cake cake;
public Decorator(Cake cake){
this.cake = cake;
}
public float price() {
return cake.price();
}
public String description() {
return cake.description();
}}
/**
* 鸡蛋装饰实体
*
*/
public class Egg extends Decorator{
public Egg(Cake cake) {
super(cake);
}
public float price() {
return super.price()+1.5f;
}
public String description() {
return super.description()+"+鸡蛋";
}}
/**
* 牛排装饰实体
*
*/
public class Beef extends Decorator {
public Beef(Cake cake) {
super(cake);
}
public float price() {
return super.price() + 2.0f;
}
public String description() {
return super.description()+"+牛排";
}
public String fire(){
return "牛排需要三分熟";
}}
public class Test {
public static void main(String[] args) {
//有个客户要买原味手抓饼
CrasphingCake cc = new CrasphingCake();
Beef bef = new Beef(cc);
bef.fire();
bef.price();
bef.description();
System.out.println("您选购的是:"+bef.description());
System.out.println("您要支付:"+bef.price()+"元");
}}
4.单例模式:保证当前类有且仅有一个对象,不允许被创建多个实例。
分类:饿汉式、懒汉式
实现思路:构造函数私有化
声明一个本类对象静态引用
提供获取当前实例的静态方法
/**
*
*单例模式
*/
public class Student {
private static Student stu = new Student();// 饿汉式
private Student() {
}
public static Student getInstance() {
return stu;
}}
public class Student1 {
private static Student1 stu;
private Student1() {
}
public static Student1 getInstance() {// 懒汉式 用线程使它出错 ,加上synchronized锁住就不会错
if (stu == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stu = new Student1();
}
return stu;
}}
public class Test {
// 单例
public static void main(String[] args) {
Student stu = Student.getInstance();
Student stu1 = Student.getInstance();
System.out.println(stu == stu1);
}
}
public class Test1 {
static Student1 stu, stu1;
public static void main(String[] args) {
Thread thread_A = new Thread() {
public void run() {
stu = Student1.getInstance();
System.out.println("-------A------");
}};
Thread thread_B = new Thread() {
public void run() {
stu1 = Student1.getInstance();
System.out.println("-------B------");
}};
thread_A.start();// 启动
thread_B.start();// 启动
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(stu == stu1);
这些仅仅是自己学到与自己的见解,如果有不对的地方请多多指导,谢谢!