سؤال

أنا أستخدم نمط الأمر لأمر عمل من عميل إلى خادم عبر مقبس TCP / IP.سيأخذ الخادم كائن الأمر، وتحلله ثم استدعاء التنفيذ () في كائن الأمر.ومع ذلك، أحتاج إلى تمرير قيمة مرة أخرى إلى المتصل عبر المقبس.هل يسمح نمط الأمر بذلك؟إذا لم يكن كذلك، هل هناك عمل حول؟لقد نظرت إلى مثال تبديل الضوء على Wikipedia، وهو أمر رائع، ولكن لا توجد قيم عودة. أي نصيحة محل تقدير كبير.

هل كانت مفيدة؟

المحلول

You should not have an "execute()" method on the Command sent to the remote server, this is bad in lots of ways, especially in Java. The Command should represent the action the recipient should take. Which in this case is to call a method on some object.

The Command Pattern is to represent actions taken or to be taken, not the implementation of those actions. Think more of a set of instructions to be carried out.

What your are describing is basically an over-engineer RPC call mechanism. Don't re-invent this wheel. Looks at existing RPC mechanisms, there are plenty to choose from in the Java world. Then you need to decide if the RPC is synchronous or asynchronous.

A REST based API is what is popular and will last longer as an API than any native language specific mechanism like RMI.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top