<>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 .


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 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 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.


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 .

©2020 ioDraw All rights reserved
python gui Interface examples -Python GUI Programming complete example IO flow —— Classification of flow ,InputStream,OutputStream,Reader,Writer etc. Thread.setDaemon Set up the Daemons STM32 RS485 modbus Communication source program , The test is effective stay MATLAB 2018 Time series prediction with deep learning vue3 Write component library and publish to npm The process of Questions and answers for the interviewer : How did you get a project in the project team ??? In the applet form Multiple cycles input How to get each input Value of 2021 Front end high frequency interview questions ( You have to watch it !!!)js Array method