Java的UML类图(二)

参考:

http://www.cnblogs.com/samchen2009/p/3315999.html 



前言

    本文依次逐个介绍 “实现”、“继承”、“依赖”、“引用”、“聚合”、“组合”;每一个以案例的形势进行表述


1,实现:Realization

实现就是实现定义的接口协议,通常使用implements关键字

    案例A

public interface WindowManager {
    void addView(View view, ViewGroup.LayoutParams params);
    void updateViewLayout(View view, ViewGroup.LayoutParams params);
    void removeView(View view);
}
public class WindowManagerImpl implements WindowManager {
    @Override
    public void addView(View view, ViewGroup.LayoutParams params) {
    }
    @Override
    public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
    }
    @Override
    public void removeView(View view) {
    }
}
     * 就常见的的implements实现

    

    案例B

#IWindowManager.aidl
interface IWindowManager {
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}
public class WindowManagerService extends IWindowManager.Stub {
    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
    }
}

    * aidl特殊方式,实现接口使用的是extends关键字


2,继承:Inherritance

继承在Java中体现为 extends 实体类或抽象类

    案例A

public class PhoneWindow extends Window {
    @Override
    public void setContentView(View view) {
        // TODO
    }
}
public abstract class Window {
    public abstract void setContentView(View view);
}

    * 类(PhoneWindow),继承了另一个类(Window),可以实现定义的抽象方法或重写父类方法


3,依赖:Dependency

使用其它类的 常量或静态方法或作为局部变量使用

    案例A

public class Client {
    public Client() {
        int defaultSize = ImageUtils.getDefaultSize();
    }
}
public class ImageUtils {
    public static int getDefaultSize() {
        return 640;
    }
}

    * 类(Client),调用了另一个类(ImageUtils)的静态方法

    案例B

public class MacbookBuilder {
    public Macbook create() {
        Macbook computer = new Macbook();
        computer.setBoard("英特尔主板");
        computer.setDisplay("Retina 显示器");
        computer.setOS();
        return computer;
    }
}
public class Macbook {
    private String mBoard;
    private String mDisplay;
    private String mOS;
	
    // 设置CPU核心数
    public void setBoard(String board) {
        this.mBoard = board;
    }
    // 设置内存
    public void setDisplay(String display) {
        this.mDisplay = display;
    }
    // 设置操作系统
    public void setOS() {
        mOS = "Mac OS X 10.10";
    }
}

    * 类(MacbookBuilder),仅仅在某个函数调用了另一个类(Macbook)


4,引用:Association

引用表示,某个对象用到了一个其他对象的方法或属性。通常并不会在内部自己创建其他对象,而是传入的

    案例A

public class WindowState {
    final IWindow mClient;
    public WindowState(IWindow c) {
        mClient = c;
    }
}
public interface IWindow {
    void executeCommand(String command);
}

    * 类(WindowState),在构造方法中传入另一个类(IWindow),作为全局对象

    案例B

public class WindowManagerImpl {
    private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
}
public class WindowManagerGlobal {
    private static WindowManagerGlobal sDefaultWindowManager;
    private WindowManagerGlobal() {
    }
    public static WindowManagerGlobal getInstance() {
        synchronized (WindowManagerGlobal.class) {
            if (sDefaultWindowManager == null) {
                sDefaultWindowManager = new WindowManagerGlobal();
            }
            return sDefaultWindowManager;
        }
    }
}

    * 类(WindowManagerImpl),在定义全局参数时,间接获取其他类(WindowManagerGlobal)的对象

    案例C

public class WindowManagerGlobal {
    public static void getWindowManagerService() {
        WindowManagerService.getInstance();
    }
}
public class WindowManagerService {
    private static WindowManagerService sDefaultWindowManager;
    private WindowManagerService() {
    }
    public static WindowManagerService getInstance() {
        synchronized (WindowManagerService.class) {
            if (sDefaultWindowManager == null) {
                sDefaultWindowManager = new WindowManagerService();
            }
            return sDefaultWindowManager;
        }
    }
}
    * 类(WindowManagerGlobal),在调用某个函数的时候,引入其他类(WindowManagerService)


5,聚合:Aggregation

聚合也是引用的一种关系。它强调两个类之间的从属关系,但和组合不同,它不要求两个类的生命周期相同

    案例A

public class TranfficCalculator {
    private Strategy mCaculateStrategy;
    public int caculatePrice(int km) {
        return this.mCaculateStrategy.calculatePrice(km);
    }
    public void setStrategy(Strategy strategy) {
        this.mCaculateStrategy = strategy;
    }
}
public class Strategy {
    int calculatePrice(int km) {
        return (int) (km * 0.01);
    }
}
      * 类(TranfficClculator),使用了另一个类(Strategy)作为一个组件。相对于组合而言,它和组件的生命周期不一致



6,组合:Composition

组合也是一种引用关系;它强调两个类之间整体和局部关系,并且暗示两个类之间有相同的生命周期

    案例A

public class ViewRootImpl {
    final W mWindow;
    public ViewRootImpl() {
        mWindow = new W(this);
    }
    static class W {
        private final WeakReference<ViewRootImpl> mViewAncestor;
        W(ViewRootImpl viewRoot) {
            mViewAncestor = new WeakReference<>(viewRoot);
        }
    }
}

    * 总案例图中有:(ViewRootImpl + ViewRootImpl.W) 、 (PhoneView + PhoneView.DecorView)

    * 类(ViewRootImpl),使用它的内部类(ViewRootImpl.W);在构造方法中创建内部类的全局对象(mWindow)

     案例B
public class WindowManagerGlobal {
    private final ArrayList<ViewRootImpl> mRoots = new ArrayList<>();
    public void addView() {
        ViewRootImpl root = new ViewRootImpl();
        mRoots.add(root);
    }
    public void remoteView(int index) {
        mRoots.remove(index);
    }
    public void closeAll() {
        mRoots.clear();
    }
}
public class ViewRootImpl {
}
         * 总案例图中有:(WindowManagerGlobal + ViewRootImpl[列表]) 、 (WindowManagerService + WindowState[列表])

    * 类(WindowManagerGlobal),默认创建外部类(ViewRootImpl)的列表,并在类中维护

    案例C

public class Session {
    SurfaceSession mSurfaceSession;
    public Session() {
    }
    void windowAddedLocked() {
        if (mSurfaceSession == null) {
            mSurfaceSession = new SurfaceSession();
        }
    }
    void killSessionLocked() {
        if (mSurfaceSession != null) {
            mSurfaceSession = null;
        }
    }
}
public class SurfaceSession {
}
     总案例图中有:(Session + SurfaceSession) 、 (WindowManagerService + Session)

    * 类(Session),持有SurfaceSession,并在内部实现创建和销毁,在类中维护


关于所有UML类图的总结在:Java的UML类图(一)



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值