title : 每日深耕,勤练不缀之java设计模式
java设计模式、
创建型设计模式:工厂模式(Factory、Abstract Factory)、单例模式(Singleton)、构建器模式(Builder)、原型模式(ProtoType)
结构型模式:是针对软件设计结构的总结,关注于类、对象继承、组合方式的实践经验。常见的结构型模式,包括桥接模式(Bridge)、适配器模式(Adapter)、装饰者模式(Decorator)、代理模式(Proxy)、组合模式(Composite)、外观模式(Facade)、享元模式(Flyweight)等
行为型模式:是从类或对象之间交互、职责划分等角度总结的模式。比较常见的行为型模式有策略模式(Strategy)、解释器模式(Interpreter)、命令模式(Command)、观察者模式(Observer)、迭代器模式(Iterator)、模板方法模式(Template Method)、访问者模式(Visitor)
谈谈你知道的设计模式?
手动实现单例模式
spring等框架使用哪些模式?
刚刚学过io框架,我们知道了还有InputStream的抽象类
标准类库中提供了FileputStream、ByteArrayInputStream等各种不同的子类,这就是装饰器模式
装饰器模式:包装同类型实例,对目标对象的调用,往往会通过包装类覆盖过的方法,迂回调用被包装的实例。
例子:
BufferedInputStream经过包装,为输入流过程增加缓存
public BufferedInputStream(InputStream in)
单例模式(创建型模式)
给别人提供一个别人没有的单例
public class Singleton {
private static Singleton instance;
private Singleton(){}
public static synchronized Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
工厂模式
造商品
//产品类接口
interface IProduct {
public void productMethod();
}
//产品类实体
class Product implements IProduct {
public void productMethod() {
System.out.println("产品");
}
}
//工厂类接口
interface IFactory {
public IProduct createProduct();
}
//工厂类实体
class Factory implements IFactory {
public IProduct createProduct() {
return new Product();
}
}
//用户
public class Client {
public static void main(String[] args) {
IFactory factory = new Factory();
IProduct prodect = factory.createProduct();
prodect.productMethod();
}
}
代理模式
代理模式就是在一个我们已知的方法in use时,我们可以采用代理模式实现方法的扩展
//源对象接口
public interface Sourceable {
public void method();
}
//源对象实现类
public class Source implements Sourceable {
@Override
public void method() {
System.out.println("the original method!");
}
}
//代理类
public class Proxy implements Sourceable {
private Source source;
public Proxy(){
this.source = new Source();
}
@Override
public void method() {
if (source == null){
source = new Source ();
}
source.method();
}
}
装饰器模式(结构性模式)
通过一个更加灵活的方式动态的为某一个对象添加一些额外的职责
被扩展的类接口–>实现被扩展的类
|
创建实现了Shape接口的抽象装饰类
|
继承抽象接口进一步扩展该性能
//需被拓展的类的接口
public interface Shape {
void draw();
}
//需被拓展的类
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Rectangle");
}
}
//创建实现了 Shape 接口的抽象装饰类。
public abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape){
this.decoratedShape = decoratedShape;
}
public void draw(){
decoratedShape.draw();
}
}
//扩展了 ShapeDecorator 类的实体装饰类
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
@Override
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape){
System.out.println("Border Color: Red");
}
}
观察者模式:(行为型模式)
//创建被观察者
public class Subject {
private List<Observer> observers
= new ArrayList<Observer>();
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
public void attach(Observer observer){
observers.add(observer);
}
public void notifyAllObservers(){
for (Observer observer : observers) {
observer.update();
}
}
//创建观察者抽象类
public abstract class Observer {
protected Subject subject;
public abstract void update();
}
//观察者实现类1
public class BinaryObserver extends Observer{
public BinaryObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Binary String: "
+ Integer.toBinaryString( subject.getState() ) );
}
}
//观察者实现类2
public class OctalObserver extends Observer{
public OctalObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Octal String: "
+ Integer.toOctalString( subject.getState() ) );
}
}
//改变被观察者状态
public class ObserverPatternDemo {
public static void main(String[] args) {
Subject subject = new Subject();
new OctalObserver(subject);
new BinaryObserver(subject);
System.out.println("First state change: 15");
subject.setState(15);
System.out.println("Second state change: 10");
subject.setState(10);
}
}