标签:
synchronized public static ImCache getInstance (Context context) {
if (sInstance == null) {
sInstance = new ImCache(context);
}
return sInstance;
}
ImSession session = sb.looper(mImModule.getLooper())
.listener(mImModule)
.config(mImModule.getImConfig())
.imsService((IImServiceInterface)HandlerFactory.getStackAdaptor(IImServiceInterface.class))
.slmService((ISlmServiceInterface)HandlerFactory.getStackAdaptor(ISlmServiceInterface.class))
.uriGenerator(mImModule.getUriGenerator())
.mnoStrategy(mImModule.getMnoSpecificStrategy())
.chatId(StringIdGenerator.generateChatId(participants, participants.size() > 1))
.participantsUri(participants)
.ownPhoneNum(mImModule.getOwnPhoneNum())
.subject(event.mWelcomeNote)
.contributionId(event.mContributionId)
.conversationId(event.mConversationId)
.sdpContentType(event.mSdpContentType)
.direction(ImDirection.INCOMING)
.rawHandle(event.mIsDeferred ? null : event.mRawHandle)
.capability(mImModule.getChatSessionCapability())
.sessionType(event.mSessionType)
.build();
public interface IDataAdapter {
/**
* this method will adapt the xml element to profileData
*
* @param element the input xml element
* @return the output profileData
*/
public ProfileData buildProfileData(Element element);
/**
* this method will adapt the profileData to xml element
*
* @param profileData the input profileData
* @param document the input W3G document, which will used to make element
* @return the output xml element
*/
public Element buildXmlElement(ProfileData profileData, Document document);
/**
* compare the two ContentValues and return the compare result
* <p>
* this method is used to compare two profileInfo,
*
* @param valuesA the input ContentValues to be compared
* @param valuesB the input ContentValues to be compared
* @return the compare result: {@link CompareResult}
*/
public int compare(ContentValues valuesA, ContentValues valuesB);
public String getMimeType();
public String getTagName();
public String getElementType();
public interface CompareResult {
public final static int SAME = 0;
public final static int SHOULD_UPADATE = 1;
public final static int NOT_RELATED = -1;
}
}
更好的复用性
系统需要使用现有的类,而此类的接口不符合系统的需要。那么通过适配器模式就可以让这些功能得到更好的复用。
更好的扩展性
在实现适配器功能的时候,可以调用自己开发的功能,从而自然地扩展系统的功能。
mPersister = new ImPersister(mContext, mImModule);
addObserver(mPersister);
chatData.triggerObservers(ImCacheAction.INSERTED);
public void update(Observable observable, Object data) {
ChatData chatData = (ChatData) observable;
ImCacheAction action = (ImCacheAction) data;
if (action == ImCacheAction.INSERTED) {
insertSession(chatData);
} else if (action == ImCacheAction.UPDATED) {
onSessionUpdated(chatData);
} else if (action == ImCacheAction.DELETED) {
deleteSession(chatData);
}
}
观察者和被观察者之间是抽象耦合
private static IMnoStrategy createMnoStrategy(Context ctx) {
String mcc = getMccFromConfiguration();
String mnc = getMncFromConfigurtion();
Mno mno = new Mno(Mcc.parseMcc(mcc), Mnc.parseMnc(mnc));
Log.d(LOG_TAG, "createMnoStrategy " + mno);
if (sMnoSpecificStrategyGenerator.containsKey(mno)) {
Class<?> cls = sMnoSpecificStrategyGenerator.get(mno);
return (IMnoStrategy) cls.getConstructor(Context.class).newInstance(ctx);
}
策略模式主要用来分离算法,根据相同的行为抽象来做不同的具体策略实现。
通过以上也可以看出策略模式的优缺点:
ICapabilityServiceEventListener.Stub mEventProxy = new
ICapabilityServiceEventListener.Stub() {
@Override
public void onOwnCapabilitiesChanged() throws RemoteException {
if (mRelay == null) {
Log.d(LOG_TAG, "no listener for ICapabilityServiceEventListener");
throw new RemoteException();
} else {
mRelay.onOwnCapabilitiesChanged();
}
}
给对象增加了本地化的扩展性,增加了存取操作控制
ServiceModuleManager
总体管理各个Module
output = new BufferedOutputStream(new FileOutputStream (mRequest.mFilePath, mTransferred > 0), bufferSize);
EVENT_SEND_MESSAGE
-->InitialState
MessageBase imMsg = (MessageBase) msg.obj;
onStartSession(imMsg, mIsRejoinable);
-->EstablishedState
onSendImMessage((MessageBase) msg.obj);
-->ClosingState
....
它将与特定状态相关的行为局部化,并且将不同状态的行为分割开来
标签:
原文地址:http://blog.csdn.net/shcalm/article/details/44812899