Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refreshing Java

Refreshing Java

Java 101 refresher for those who want to get into Android but feel uncertain in Java.

For demo resource, see https://github.com/RobGThai/JavaRefresher

Poohdish Rattanavijai

December 02, 2014
Tweet

More Decks by Poohdish Rattanavijai

Other Decks in Technology

Transcript

  1. Object-Oriented Programming Object-oriented programming (OOP) is a programming language model

    organised around objects rather than actions and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data. — Google
  2. Class & Object • Class: A class can be defined

    as a template/blue print that describes the behaviors/states that object of its type support. • Object: Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating. An object is an instance of a class.
  3. Example • Class: หมา มีชื่อ มีขา เห่าได้ หมุนตัวได้ ลองขอมือได้ •

    Object: อีแดง หมาพันธุ์ไทยหลังอาน ชอบเห่า จะหมุนตัวเวลาคลุกขี้ ขอมือจะกัด public class Dog{ private String name, bark, spin, giveHand; private int legs; } Dog อีแดง = new Dog();
  4. Access Modifiers Modifiers limit ability to access each member (property/

    method/class) from external class. • private: No entry. • protected: Accessible via Child class. • public: Accessible from everywhere. • default: Accessible via classes within the same package.
  5. Class Hierarchy Class Hierarchy: In object-oriented programming, a class is

    a template that defines the state and behaviour common to objects of a certain kind. A class can be defined in terms of other classes. For example, a truck and a racing car are both examples of a car. Some famous example, car-truck, canine-dog-wolf. Still confused?
  6. Inheritance Inheritance is when an object or class is based

    on another object or class, using the same implementation (inheriting from a class) or specifying implementation to maintain the same behaviour (realizing an interface; inheriting behavior). The relationships of objects or classes through inheritance give rise to a hierarchy.
  7. Generics Generics allow you to abstract over types and add

    stability to your code by making more of your bugs detectable at compile time.
  8. Collection A Collection is an object that can hold references

    to other objects. The collection interfaces declare the operations that can be performed on each type of collection. You can NOT create fully functional app without using some form of Collection.
  9. Collection Well known Java Collections are as following: • Stack

    • LinkedList • ArrayList • Set • HashMap • TreeMap
  10. Thread A Thread of execution is the smallest sequence of

    programmed instructions that can be managed independently by a scheduler. In mobile world, there's always one main thread (aka UI thread). Any long duration or intensive process should be ran on background thread only. This is to ensure UI thread get all the cycle it wants to draw the screen.
  11. Thread Common thread implementation include using Thread, Runnable, or Executor.

    Thread-safe implementation is also available by the framework such as AtomicInteger, AtomicLong, and Atomic*.
  12. Exception Handling Exception handling is the process of responding to

    the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing. There are three main components involved in Exception Handling in Java, Throwable, Exception, Error.
  13. S

  14. Single Responsibility principle: Don't ! public class PostingActivity { private

    void prepareLayout() { ... } private void bindData() { ... } private boolean validateForm() { ... } private View generateAttributes() { ... } }
  15. Single Responsibility principle: Do ! public class ViewController { //

    My job is to control components for the view private ViewBinder binder; // Let me handle the binding then public void bindData(Data data) { binder.bind(data); } }
  16. O

  17. Open/Closed principle: Don't ! public class ViewBinder { public void

    binData(Data d) { ... if(isLandscapeMode && screenSize > 680) { useTabletView(); } if(isPrettify) { prettify(); } } private void prettify() { ... } private void useTabletView() { ... } }
  18. Open/Closed principle: Do ! public class ViewBinder { public void

    binData(Data d) { ... } } public class PrettyViewBinder extends ViewBinder { @Override public void binData(Data d) { // By doing this we ensure ViewBinder still working properly ... prettify(); } }
  19. L

  20. Liskov Substitution principle Objects in a program should be replaceable

    with instances of their subtypes without altering the correctness of that program.
  21. Liskov Substitution principle: Don't ! public class ViewBinder { public

    void binData(Data d) { ... } } public class PrettyViewBinder extends ViewBinder { @Override public void binData(Data d) { if(isLoading) { return ; } ... } }
  22. Liskov Substitution principle: Do ! public class ViewBinder { public

    void binData(Data d) { ... } } public class PrettyViewBinder extends ViewBinder { @Override public void binData(Data d) { if(isLoading) { // Use placeholder instead of loading image } ... // These task will be execute regardless. // This ensure you get the view with data binded just like inside ViewBinder } }
  23. I

  24. Interface segregation principle: Don't ! public interface IDisplayView { public

    Data loadData(String url); public View bindData(Data d); public void onViewClick(); public void onViewSwipe(); }
  25. Interface segregation principle: Do ! public interface IDisplayView { public

    View bindData(Data d); } public interface IDataLoader { public Data loadData(String url); } public interface IViewInteract { public void onViewClick(); public void onViewSwipe(); }
  26. D

  27. Dependency Inversion principle: Don't ! public class ViewController { private

    ViewBinder binder; public ViewController(ViewBinder binder) { this.binder = binder; } public void bindData(Data data) { binder.bind(data); } }
  28. Dependency Inversion principle: Do ! public class ViewController { private

    IDisplayView binder; public ViewController(IDisplayView binder) { this.binder = binder; } public void bindData(Data data) { binder.bind(data); } } public class ViewBinder implements IDisplayView { @Override public void bind(Data d) { ... } }