java设计模式学习
1.观察者
1 package designpattern; 2 3 import java.util.ArrayList; 4 5 public class Observer { 6 7 public static void main(String[] args) { 8 Button b = new Button(); 9 b.addActionListener(new MyactionListener()); 10 b.addActionListener(new MyactionListener2()); 11 b.btnPressed(); 12 } 13 14 } 15 16 class Button { 17 ArrayList<ActionListener> listeners = new ArrayList<ActionListener>(); 18 19 public void btnPressed() 20 { 21 ActionEvent e = new ActionEvent(this); 22 for (int i = 0; i < listeners.size(); i++) { 23 listeners.get(i).actionPerform(e); 24 } 25 } 26 27 public void addActionListener(ActionListener i){ 28 listeners.add(i); 29 } 30 31 32 } 33 34 interface ActionListener{ 35 public void actionPerform(ActionEvent e); 36 } 37 38 class ActionEvent{ 39 40 private long when ; 41 42 private Object source ; 43 44 public ActionEvent(Object source) { 45 when = System.currentTimeMillis(); 46 } 47 48 public Object getSource() { 49 return source; 50 } 51 52 public void setSource(Object source) { 53 this.source = source; 54 } 55 56 public long getWhen() { 57 return when; 58 } 59 60 } 61 62 class MyactionListener implements ActionListener{ 63 64 @Override 65 public void actionPerform(ActionEvent e) { 66 p.echo("presess1"); 67 } 68 69 } 70 71 class MyactionListener2 implements ActionListener{ 72 73 @Override 74 public void actionPerform(ActionEvent e) { 75 p.echo("presess2"); 76 } 77 78 }
2.单例模式
package com.singleton; import java.util.ArrayList; import java.util.List; //单例模式 public class Singleton { /** * @param args */ public static void main(String[] args) { Car2 car1 = Car2.getInstance(); Car2 car2 = Car2.getInstance(); p.echo(car1 == car2); } } class Car{ //单例 private static Car car = new Car(); //多例 private static List<Car> cars = new ArrayList<Car>(); //不可以new这个类 private void car(){} public static Car getInstance(){ return car; } } //另一种实现方式 class Car2{ private static Car2 car; static{ Car2 car = new Car2(); } //不可以new这个类 private void Car2(){} public static Car2 getInstance(){ return car; } }
3.简单工厂
工厂模式的好处:可以有效控制对象的生产过程,如果写在构造方法里面则无法控制
package com.simplefactory; /** * 简单工厂 (只要是控制了对象的生产过程的都可以叫做工厂) * 任意定制交通工具(Moveable 限制 有多态存在), 控制交通工具的产生过程() * @author root * */ public class Factory { public static void main(String[] args) { MoveFactory factory = new CarFactory(); // MoveFactory factory = new PlanFactory(); Moveable m = factory.create(); m.run(); } } //限制任意定制交通工具 interface Moveable{ void run(); } //汽车 class Car implements Moveable{ @Override public void run() { p.echo("汽车。。。。。"); } } class Plan implements Moveable{ @Override public void run() { p.echo("飞机。。。。。"); } } //接口比抽象类会更好,因为接口可以有多继承 interface MoveFactory{ Moveable create(); } /*abstract class MoveFactory{ abstract void create(); }*/ //汽车工厂 class CarFactory implements MoveFactory{ @Override public Moveable create() { return new Car(); } } //飞机工厂 class PlanFactory implements MoveFactory{ @Override public Moveable create() { return new Plan(); } }
3.2抽象工厂
package com.abstructfactory; /** * 控制一系列产品的产生(替换一系列的产品) * * @author songjiankang * */ public class AbstructFactory { public static void main(String[] args) { // MyAbstructFactory = new DefaultFactory(); MyAbstructFactory factory = new MagicFactory(); factory.createVehicle().run(); factory.createWeaWeapon().shoot(); } } abstract class Vehicle{ abstract public void run(); } class Car extends Vehicle{ public void run() { p.echo("汽车。。。"); } } class Broom extends Vehicle{ public void run() { p.echo("扫帚。。。"); } } abstract class Weapon{ abstract public void shoot(); } class AK47 extends Weapon{ public void shoot(){ p.echo("开枪。。。"); } } class Stick extends Weapon{ public void shoot(){ p.echo("魔棍。。。"); } } abstract class MyAbstructFactory{ abstract public Vehicle createVehicle(); abstract public Weapon createWeaWeapon(); } class DefaultFactory extends MyAbstructFactory{ @Override public Vehicle createVehicle() { return new Car(); } @Override public Weapon createWeaWeapon() { return new AK47(); } } class MagicFactory extends MyAbstructFactory{ @Override public Vehicle createVehicle() { return new Broom(); } @Override public Weapon createWeaWeapon() { return new Stick(); } }
简单工厂是在产品方面进行扩展方便,但系列会很麻烦,工厂泛滥
抽象工厂在产生产品系列时方便,但产生产品品种则很麻烦
spring 的 Bean 工厂实现方式
把类名以key、value的格式存入到 xml中,然后又一个BeanFactory类读取这个文件,把读取的这个文件以key、value对应的Object格式存到一个map里,
当获取某个id对应的Object时从map中直接得到
抽象工厂简单示意图
自定义输出类:
1 class p{ 2 3 public static void echo(Object o) { 4 System.out.println(o); 5 } 6 }