標籤:設計模式 android
1、定義:
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation to subclasses.
定義一個用於建立對象的介面,讓子類決定執行個體化哪個類。
2、意義:
Factory 方法使得類的執行個體化延伸到子類。
3、四大角色:
3.1、抽象工廠:Factory 方法模式的核心,任何在模式中建立對象的工廠類必須實現這個介面。
3.2、具體工廠:實現了抽象工廠介面的具體JAVA類。具體工廠角色含有與業務密切相關的邏輯,並且受到使用者的調用以建立匯出類。
3.3、抽象角色:Factory 方法模式所建立的對象的超類。
3.4、具體角色:實現抽象角色的某個具體角色的執行個體。
4、優點:
4.1、Factory 方法模式是完全符合開閉原則的;
4.2、擯棄了簡單原廠模式的缺點;
4.3、原廠模式是一種典型的解耦模式,可以降低對象之間的耦合度;
4.4、原廠模式是依靠抽象架構的,它把執行個體化產品的任務交由實作類別完成,擴充性比較好。
4.5、可以使代碼結構清晰,有效地封裝變化。
4.6、對調用者屏蔽具體的產品類。如果使用原廠模式,調用者只關心產品的介面就可以了,至於具體的實現,調用者根本無需關心。即使變更了具體的實現,對調用者來說沒有任何影響。
5、缺點:
簡單的對象應用時,不適合Factory 方法模式。
6、寫了一個簡單的demo:
首先是普通的 抽象 對象
<span style="font-size:14px;">package com.example.demo.FactoryMethod;/** * 抽象</span><span style="font-size:12px;"><span style="font-family: Arial;">角色</span></span><span style="font-size:14px;"> * @author qubian * @data 2015年6月4日 * @email [email protected] * */public interface Lottery {public String getLotteryName();}</span>實現:
package com.example.demo.FactoryMethod;/** * 具體角色 * @author qubian * @data 2015年6月4日 * @email [email protected] * */public class SSQLottery implements Lottery{@Overridepublic String getLotteryName() {return "雙色球";}}package com.example.demo.FactoryMethod;/** * 具體角色 * @author qubian * @data 2015年6月4日 * @email [email protected] * */public class DLTLottery implements Lottery{@Overridepublic String getLotteryName() {return "大樂透";}}
然後是核心的 抽象工廠:
package com.example.demo.FactoryMethod;/** * 抽象工廠 * @author qubian * @data 2015年6月4日 * @email [email protected] * */public interface LotteryFactory {public Lottery getLottery();}
具體工廠:
package com.example.demo.FactoryMethod;/** * 具體工廠 * @author qubian * @data 2015年6月4日 * @email [email protected] * */public class SSQFactory implements LotteryFactory{@Overridepublic Lottery getLottery() {return new SSQLottery();}}package com.example.demo.FactoryMethod;/** * 具體角色 * @author qubian * @data 2015年6月4日 * @email [email protected] * */public class DLTLottery implements Lottery{@Overridepublic String getLotteryName() {return "大樂透";}}
使用:
package com.example.demo.FactoryMethod;import android.util.Log;/** * Factory 方法的使用 * * @author qubian * @data 2015年6月4日 * @email [email protected] * */public class UseFactory {private static final String TAG="UseFactory";public void use(){//若是需要修改,只需要修改 new SSQFactory(); 即可;//LotteryFactory factory= new SSQFactory();//此處的操作完成是,介面的操作;// 也就是說:使用原廠模式,調用者只關心產品的介面就可以了,//至於具體的實現,調用者根本無需關心。即使變更了具體的實現,對調用者來說沒有任何影響。Lottery lottery = factory.getLottery();Log.i(TAG,lottery.getLotteryName());}}
在Android中的使用廣泛,其中:
1、 關於ArrayList,HashSet,與 Iterator 之間都能算是一種Factory 方法;
Set<String> set = new HashSet<String>();Iterator<String> iterator = set.iterator();while(iterator.hasNext()){// 具體操作}List<String> list =new ArrayList<String>();Iterator<String> it = list.iterator();while(it.hasNext()){// 具體操作}
其中List和Set都是工廠介面
/** * A {@code Set} is a data structure which does not allow duplicate elements. * * @since 1.2 */public interface Set<E> extends Collection<E>{ /** * 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<E> 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<E> extends Collection<E> { /** * 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<E> iterator();}
自然,ArrayList與HashMap 都有關於其中的實現了。
public class ArrayList<E> extends AbstractList<E> implements Cloneable, Serializable, RandomAccess { @Override public Iterator<E> iterator() { return new ArrayListIterator(); } private class ArrayListIterator implements Iterator<E> { /** 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<E> 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; } }}
Android設計模式--Factory 方法模式