Service side includes 3 components: Service, Service_client and Service_Impementation
Service: it will be regiestered to ServiceManager, so Client can locate it, then call Service to create an Service_Client.
Service_Client: it will handle each play command ( start, stop, seek, etc. ) from Client. it is actually a peer of Client on Service side.
Serivce_Implemenation: it is the function doer, Service_Client will call it when receiving Client request. In other words, Service_Client is an adapter from Client to Serivce_Implemenation
Client: Running on Apps process Called by Java via JNI or other native apps to request video play.
Below diagram shows the sequences of 3 catagories
1) Create:
Apps -> MediaPlayer -> MediaPlayerServcie -> MediaPlayerServcie::Client -> MediaPlayerBase ( concrete class is like: StagefrightPlayer )
2) Start ( Call forward)
Apps -> MediaPlayer ->MediaPlayerServcie::Client-> MediaPlayerBase (StagefrightPlayer)
3) Call back
MediaPlayerBase (StagefrightPlayer) -> MediaPlayerServcie::Client ->MediaPlayer
(check more from the diagram)
现在很多人在利用比较流行的开源游戏引擎cocos2d-x开发游戏,在游戏中免不了使用状态机,这里给大家一种我自认为好的状态机的实现O(∩_∩)O~。
先贴上代码:
#ifndef BASESTATE_H #define BASESTATE_H template <class entity_type> class BaseState { public: //BaseState(void){}; virtual void Enter(entity_type*)=0; virtual void Execute(entity_type*)=0; virtual void Exit(entity_type*)=0; virtual ~BaseState(void){}; }; #endif // !BASESTATE_H
以上为状态类代码,O(∩_∩)O~。简单的只有 三个主要调用的函数,进入,执行,离开。
然后我们继续查看下状态机的基类,废话不多说,先上代码,这样新手可以直接拿走直接使用。、
#ifndef BASESTATEMACHINE_H #define BASESTATEMACHINE_H ////////////////////////////////////////////////////////////////////////// // /// @file 状态机基类 /// @brief 负责状态机的跳转 /// @version 2.0 ////////////////////////////////////////////////////////////////////////// #include "BaseState.h" #include <assert.h> ////////////////////////////////////////////////////////////////////////// /// @class BaseStateMachine /// @brief 状态机基类 /// /// \n本类负责模板状态机的所有处理 template <class entity_type> class BaseStateMachine { private: entity_type *m_pOwner; ///<指向拥有这个了实例的智能体的指针 BaseState<entity_type> *m_pCurrentState; ///<智能体的当前状态 BaseState<entity_type> *m_pPreviousState; ///<智能体的上一个状态 BaseState<entity_type> *m_pGlobalState; ///<每次FSM被更新时,这个状态被调用 public: BaseStateMachine(entity_type* owner): m_pOwner(owner), m_pCurrentState(nullptr), m_pPreviousState(nullptr), m_pGlobalState(nullptr) { } //////////////////////////////////////////////////////////////////// ///@brief 设置当前状态 ///@param [in]s 要设置的状态 ///@return 无返回值 //////////////////////////////////////////////////////////////////// void SetCurrentState(BaseState<entity_type> *s) { m_pCurrentState = s; } //////////////////////////////////////////////////////////////////// ///@brief 设置全局状态 ///@param [in]s 要设置的状态 ///@return 无返回值 //////////////////////////////////////////////////////////////////// void SetGlobalState(BaseState<entity_type> *s) { m_pGlobalState = s; } //////////////////////////////////////////////////////////////////// ///@brief 设置前面的状态 ///@param [in]s 要设置的状态 ///@return 无返回值 //////////////////////////////////////////////////////////////////// void SetPreviousState(BaseState<entity_type> *s) { m_pPreviousState = s; } //////////////////////////////////////////////////////////////////// ///@brief 更新状态 /// ///@return 无返回值 //////////////////////////////////////////////////////////////////// void Update()const { if (m_pGlobalState) { m_pGlobalState->Execute(m_pOwner); } if (m_pCurrentState) { m_pCurrentState->Execute(m_pOwner); } } //////////////////////////////////////////////////////////////////// ///@brief 改变状态 ///@param [in]s 要设置的状态 ///@return 无返回值 //////////////////////////////////////////////////////////////////// void ChangeState(BaseState<entity_type> *pNewState) { //assert(PNewState && "<BaseStateMachine::ChangeState>:trying to change to a null state"); ///保留前一个状态记录 m_pPreviousState = m_pCurrentState; ///调用现有状态的退出方法 m_pCurrentState->Exit(m_pOwner); ///改变到一个新状态 m_pCurrentState= pNewState; ///调用新状态的进入方法 m_pCurrentState->Enter(m_pOwner); } //////////////////////////////////////////////////////////////////// ///@brief 改变到上一状态 /// ///@return 无返回值 //////////////////////////////////////////////////////////////////// void RevertToPreviousState() { ChangeState(m_pPreviousState); } //////////////////////////////////////////////////////////////////// ///@brief 查看当前状态 /// ///@return BaseState<entity_type>*当前状态 //////////////////////////////////////////////////////////////////// BaseState<entity_type>* CurrentState() const { return m_pCurrentState; } //////////////////////////////////////////////////////////////////// ///@brief 查看全局状态 /// ///@return BaseState<entity_type>* 全局状态 //////////////////////////////////////////////////////////////////// BaseState<entity_type>* GlobalState() const { return m_pGlobalState; } //////////////////////////////////////////////////////////////////// ///@brief 查看前一状态 /// ///@return BaseState<entity_type>*前一状态 //////////////////////////////////////////////////////////////////// BaseState<entity_type>* PreviousState() const { return m_pPreviousState; } //class passed as a parameter. bool isInState(const BaseState<entity_type>& st)const { return typeid(*m_pCurrentState) == typeid(st); } }; #endif // !BASESTATEMACHINE_H
这个是状态机的基类,使用的时候只需要在每个具体实例中申明一个状态机的类,然后完成自己的状态类的填写,即可完成一个高质量的状态机。注释很全有中文的也有英文的,有兴趣的童鞋可以讨论下这个设计是否合理。
一个user类
package www.xxxx.spring.dao;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
CollectionBean类
package www.xxxx.spring.dao;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionBean {
public Set sets;
//set集合依赖注入
public void setSets(Set sets) {
this.sets = sets;
}
public List<User> users;
//list集合依赖注入
public void setUsers(List<User> users) {
this.users = users;
}
public Properties props;
//Properties集合依赖注入
public void setProps(Properties props) {
this.props = props;
}
public Map<Integer,User> map;
//Map集合依赖注入
public void setMap(Map<Integer, User> map) {
this.map = map;
}
}
spring-CollectionBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="collectionBean" class="www.xxxx.spring.dao.CollectionBean" scope="singleton" lazy-init="default">
<property name="sets">
<set>
<value>张三</value>
<value>李四</value>
<value>王五</value>
<value>赵六</value>
<value>钱八</value>
</set>
</property>
<property name="users">
<list>
<ref bean="u1"/>
<ref bean="u2"/>
<ref bean="u3"/>
<ref bean="u4"/>
</list>
<!-- <array>
<ref bean="u1"/>
<ref bean="u2"/>
<ref bean="u3"/>
<ref bean="u4"/>
</array> -->
</property>
<property name="map">
<map>
<entry key="1" value-ref="u1"/>
<entry key="2">
<ref bean ="u2"/>
</entry>
<entry key="3" value-ref="u3"/>
</map>
</property>
<property name="props">
<props>
<prop key="1">jdbc:oracle</prop>
<prop key="2">jdbc:mysql</prop>
<prop key="3">jdbc:access</prop>
</props>
</property>
</bean>
<bean id="u1" class="www.xxxx.spring.dao.User">
<property name="name" value="zhang" />
<property name="age" value="22"></property>
</bean>
<bean id="u2" class="www.xxxx.spring.dao.User">
<property name="name" value="zhang" />
<property name="age" value="22"></property>
</bean>
<bean id="u3" class="www.xxxx.spring.dao.User">
<property name="name" value="zhang" />
<property name="age" value="22"></property>
</bean>
<bean id="u4" class="www.xxxx.spring.dao.User">
<property name="name" value="zhang" />
<property name="age" value="22"></property>
</bean>
</beans>
测试类
package www.xxxx.spring.dao;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
@Test
public void testSets(){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-CollectionBean.xml");
CollectionBean bean = context.getBean("collectionBean",CollectionBean.class);
//set集合
Set<String> sets = bean.sets;
Iterator<String> it = sets.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("----------------------");
//list集合
List<User> users = bean.users;
for(User user:users){
System.out.println(user.getName()+"sss"+user.getAge());
}
System.out.println("-----------------------");
//map集合
Map<Integer,User> map =bean.map;
//得到map集合的key键值的set集合
Set<Integer> setkeys= map.keySet();
//得到key键值set集合的迭代器
Iterator<Integer> itkeys=setkeys.iterator();
while(itkeys.hasNext()){
//得到一个具体的键值
Integer key=itkeys.next();
//通过 map集合get方法获取key键值对应的value值
User user = map.get(key);
System.out.println(key+"dddddd"+user.getName()+"sss"+user.getAge());
}
System.out.println("sdfd-----------------");
//map集合2
//获取实体对象的set集合
Set<Entry<Integer, User>> setentry = map.entrySet();
//获取实体对象的迭代器
Iterator<Entry<Integer,User>> itentry = setentry.iterator();
while(itentry.hasNext()){
Entry<Integer,User> entry = itentry.next();
//通过
System.out.println(entry.getKey()+"sssaaa"+entry.getValue().getName()+"sssdd"+entry.getValue().getAge());
}
System.out.println("=========================");
//Properties集合
Properties props= bean.props;
//得到这个结合键值的Key的set集合
Set<String> setprops = props.stringPropertyNames();
//String 集合迭代
Iterator<String> keystr = setprops.iterator();
while(keystr.hasNext()){
String key = keystr.next();
System.out.println(key+"ssssssssss"+props.getProperty(key));
}
}
}