Android design mode-factory method mode
1. Definition:
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation to subclasses.
Defines an interface used to create objects, so that the subclass determines which class to instantiate.
2. Meaning:
The factory method extends the class Instantiation to the subclass.
3. Four roles:
3.1 Abstract Factory: the Core of the factory method mode. Any factory class that creates objects in the mode must implement this interface.
3.2. Specific Factory: implements specific JAVA classes for abstract factory interfaces. The specific factory role contains the logic closely related to the business and is called by the user to create an export class.
3.3 abstract role: superclass of the object created in the factory method mode.
3.4. Specific role: an instance of a specific role that implements an abstract role.
4. Advantages:
4.1 The Factory method model fully complies with the principle of opening and closing;
4.2 abandon the disadvantages of the simple factory model;
4.3 The factory model is a typical decoupling mode, which can reduce the coupling between objects;
4.4 The factory model relies on the abstract architecture. It submits the task of instantiating the product to the implementation class for better scalability.
4.5. The code structure can be clear and the changes can be effectively encapsulated.
4.6 shield callers from specific product categories. If the factory mode is used, the caller only cares about the product interface. As for the specific implementation, the caller does not need to care about it. Even if the specific implementation is changed, there is no impact on the caller.
5. Disadvantages:
The factory method mode is not suitable for simple object applications.
6. I wrote a simple demo:
First, it is a common abstract object.
Package com. example. demo. factoryMethod;/*** abstract role * @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public interface Lottery {public String getLotteryName ();}
Implementation:
Package com. example. demo. factoryMethod;/*** specific role * @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public class SSQLottery implements Lottery {@ Overridepublic String getLotteryName () {return "two-color ball" ;}} package com. example. demo. factoryMethod;/*** specific role * @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public class DLTLottery implements Lottery {@ Overridepublic String getLotteryName () {return "Lotto ";}}
Then there is the core Abstract Factory:
Package com. example. demo. factoryMethod;/*** Abstract Factory * @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public interface LotteryFactory {public Lottery getLottery ();}
Specific Factory:
Package com. example. demo. factoryMethod;/*** specific factory * @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public class SSQFactory implements LotteryFactory {@ Overridepublic Lottery getLottery () {return new SSQLottery () ;}} package com. example. demo. factoryMethod;/*** specific role * @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public class DLTLottery implements Lottery {@ Overridepublic String getLotteryName () {return "Lotto ";}}
Usage:
Package com. example. demo. factoryMethod; import android. util. log;/*** use of factory methods ** @ author qubian * @ data June 4, 2015 * @ email naibbian@163.com **/public class UseFactory {private static final String TAG = "UseFactory "; public void use () {// you only need to modify new SSQFactory (); // LotteryFactory factory = new SSQFactory (); // The operation is complete here, interface operations; // that is to say: in the factory mode, the caller only cares about the product interface. // As for the specific implementation, the caller does not need to care about it at all. Even if the specific implementation is changed, there is no impact on the caller. Lottery lottery = factory. getLottery (); Log. I (TAG, lottery. getLotteryName ());}}
It is widely used in Android, including:
1. ArrayList, HashSet, and Iterator can be regarded as a factory method;
Set
Set = new HashSet
(); Iterator
Iterator = set. iterator (); while (iterator. hasNext () {// action} List
List = new ArrayList
(); Iterator
It = list. iterator (); while (it. hasNext () {// specific operation}
Both List and Set are factory interfaces.
/** * A {@code Set} is a data structure which does not allow duplicate elements. * * @since 1.2 */public interface Set
extends Collection
{ /** * Returns an iterator on the elements of this {@code List}. The elements are * iterated in the same order as they occur in the {@code List}. * * @return an iterator on the elements of this {@code List}. * @see Iterator */ public Iterator
iterator();}/** * A {@code List} is a collection which maintains an ordering for its elements. Every * element in the {@code List} has an index. Each element can thus be accessed by its * index, with the first index being zero. Normally, {@code List}s allow duplicate * elements, as compared to Sets, where elements have to be unique. */public interface List
extends Collection
{ /** * Returns an iterator on the elements of this {@code List}. The elements are * iterated in the same order as they occur in the {@code List}. * * @return an iterator on the elements of this {@code List}. * @see Iterator */ public Iterator
iterator();}
Naturally, both ArrayList and HashMap have their own implementations.
public class ArrayList
extends AbstractList
implements Cloneable, Serializable, RandomAccess { @Override public Iterator
iterator() { return new ArrayListIterator(); } private class ArrayListIterator implements Iterator
{ /** Number of elements remaining in this iteration */ private int remaining = size; /** Index of element that remove() would remove, or -1 if no such elt */ private int removalIndex = -1; /** The expected modCount value */ private int expectedModCount = modCount; public boolean hasNext() { return remaining != 0; } @SuppressWarnings("unchecked") public E next() { ArrayList
ourList = ArrayList.this; int rem = remaining; if (ourList.modCount != expectedModCount) { throw new ConcurrentModificationException(); } if (rem == 0) { throw new NoSuchElementException(); } remaining = rem - 1; return (E) ourList.array[removalIndex = ourList.size - rem]; } public void remove() { Object[] a = array; int removalIdx = removalIndex; if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } if (removalIdx < 0) { throw new IllegalStateException(); } System.arraycopy(a, removalIdx + 1, a, removalIdx, remaining); a[--size] = null; // Prevent memory leak removalIndex = -1; expectedModCount = ++modCount; } }}