<>Android Factory method pattern of source code

<> Introduction of factory method model

Factory approach model (Factory Pattern), Is one of the creative design patterns . The factory method pattern is a simple one , such as Android Medium Activity
Life cycle methods in , with onCreate Methods as an example , It can be seen as a factory approach , We can construct in it View, adopt setContentView Return to
framework handle .

<> Definition of factory method pattern

The factory method pattern defines an interface for creating objects , Let subclasses decide which class to instantiate .

<>Iterable

with List and Set take as an example ,List and Set Both inherited from Collection Interface , and Collection Interface inherited from Iterable
Interface ,Iterable The interface is simple , There is one iterator method .Java 1.8 Defined forEach and spliterator method .
public interface Iterable<T> { Iterator<T> iterator(); ... }
<>ArrayList

ArrayList Yes iterator method , Return one Itr Iterator objects .
public Iterator<E> iterator() { return new Itr(); } private class Itr
implements Iterator<E> { ... protected int limit = ArrayList.this.size; int
cursor; // index of next element to return int lastRet = -1; // index of last
element returned; -1 if no such int expectedModCount = modCount; public boolean
hasNext() { return cursor < limit; } @SuppressWarnings("unchecked") public E
next() { if (modCount != expectedModCount) throw new
ConcurrentModificationException(); int i = cursor; if (i >= limit) throw new
NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if
(i >= elementData.length) throw new ConcurrentModificationException(); cursor =
i+ 1; return (E) elementData[lastRet = i]; } public void remove() { if (lastRet
< 0) throw new IllegalStateException(); if (modCount != expectedModCount) throw
new ConcurrentModificationException(); try { ArrayList.this.remove(lastRet);
cursor= lastRet; lastRet = -1; expectedModCount = modCount; limit--; } catch (
IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } }
@Override @SuppressWarnings("unchecked") public void forEachRemaining(Consumer<?
super E> consumer) { Objects.requireNonNull(consumer); final int size =
ArrayList.this.size; int i = cursor; if (i >= size) { return; } final Object[]
elementData= ArrayList.this.elementData; if (i >= elementData.length) { throw
new ConcurrentModificationException(); } while (i != size && modCount ==
expectedModCount) { consumer.accept((E) elementData[i++]); } // update once at
end of iteration to reduce heap write traffic cursor = i; lastRet = i - 1; if (
modCount!= expectedModCount) throw new ConcurrentModificationException(); } }
Itr The iterator object provides hasNext,next,remove,forEachRemaining Implementation of .

<>HashSet

HashSet Of iterator Method returns its member variable map Of keySet Of Iterator objects .
private transient HashMap<E,Object> map; public Iterator<E> iterator() { return
map.keySet().iterator(); } final class KeySet extends AbstractSet<K> { public
final int size() { return size; } public final void clear() { HashMap.this.clear
(); } public final Iterator<K> iterator() { return new KeyIterator(); } ... }
KeySet Of iterator Method returned a KeyIterator.

<>onCreate

Android Of Activity Of onCreate Method is a factory method pattern .
public class AActivity extends Activity { @Override protected void onCreate(
@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a); } }
We are here AActivity Of onCreate Method View, And set to the current interface ContentView Return to framework
handle , If there is another one now BActvity, You just need to onCreate adopt setContentView Set up additional View, This is a factory model structure .

Technology
©2020 ioDraw All rights reserved
Vue Export page as PDF file vscode Used in Sass, And automatically compile to CSS file use python Program draw some simple cartoon characters Python Execution sequence of decorator ajax Method encapsulation Mining virus processing record Give Way Mac How to set the system not to sleep all the time C Language life game source code ValueError: Shape of passed values is (6, 3), indices imply (6, 6)C++——list Super detailed introduction