In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the iterator pattern, the intermediary pattern and the observer pattern". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what is the iterator pattern, intermediary mode and observer mode"!
Iterator mode
The name sounds familiar, and traversing collections is a common operation for java programmers. You can access the data without worrying about the type, using the iterator pattern.
Aggregate (Abstract Container): abstract classes that should be implemented by all classes, and scientific research traversed by iterators. Similar to Collection interface
Iterator (abstract iterator): defines operations to traverse container objects and return objects
Concrete Aggregate (concrete container)
Concrete Iterator (concrete iterator)
Iterator UML:
Aggregate:
Public interface Aggregate {MyIterator createIterator ();}
Iterator:
Public interface MyIterator {Object next (); boolean hasNext ();}
Concrete Aggregate:
There is an inner class ConCreateIterator inside this concrete container.
Public class ConcreteAggregate implements Aggregate {private String [] values; public ConcreteAggregate (String [] values) {this.values = values;} @ Override public MyIterator createIterator () {return new ConcreteIterator ();} / / inner class, specific iterator class private class ConcreteIterator implements MyIterator {private int position @ Override public Object next () {if (this.hasNext ()) {return values [position++];} return null;} @ Override public boolean hasNext () {return position
< values.length; } }} Client: public class Client { public static void main(String[] args) { String[] arr = {"h", "e", "l", "l", "o"}; ConcreteAggregate strarr = new ConcreteAggregate(arr); for (MyIterator it = strarr.createIterator(); it.hasNext(); ) { System.out.println(it.next()); } }}观察者模式 观察者模式定义对象间的一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象皆得到通知并被自动更新。Subject (topic): an observable interface that is usually implemented by a class. Generally, there are adding and deleting observers, and reminding observers to update
Concrete Subject (specific theme): implements the theme interface and notifies observers of updates
Observer (Observer): the interface of the observer, there is usually a update method
Concrete Observer
Observer UML:
Observer:
Public interface Observer {void update (String message);}
Concrete Observer:
Public class ConcreteObserver implements Observer {@ Override public void update (String message) {System.out.println ("the official account of the subscription has new news:" + message);}}
Subject:
Public interface Subject {List subscribers = new ArrayList (); void attach (Observer observer); void detach (Observer observer); void notifySubscribers (String message);}
Concrete Subject:
Public class ConcreteSubjectDesignPattern implements Subject {@ Override public void attach (Observer observer) {subscribers.add (observer); System.out.println ("add a user to follow");} @ Override public void detach (Observer observer) {subscribers.add (observer); System.out.println ("one user cancels following") } @ Override public void notifySubscribers (String message) {for (Observer subscriber: subscribers) {subscriber.update (message);}} / / this method is not universal, and each specific topic may have a different method public void deliver (String message) {notifySubscribers (message);}} intermediary mode
Intermediary model, also known as mediation model, in our real life, there will be many intermediaries, housing intermediaries, contract intermediaries, what the intermediary model in java will be like.
Now there is such a demand, moba competitive games generally have five positions, field, spell output, physical output, auxiliary, tank. For simplicity, let's take a look at three positions.
The mage can consume himself, but he will also ask for support. In addition to fighting wild monsters, he also has to go to gank, helping to gain everyone's view and go back to the city to make up for his status.
Intermediary UML
Ordinary mode
Apc class:
Public class Apc {public void poke () {System.out.println ("mage consumption");} / / request support, public void help () {Jungle jungle = new Jungle (); Support support = new Support (); System.out.println ("mage request support"); jungle.daye (); support.vision ();}}
Jungle class:
Public class Jungle {public void daye () {System.out.println);} / / gank is coming, mage and assistant should cooperate with public void gank () {Apc apc = new Apc (); Support support = new Support (); System.out.println ("start arresting people:"); apc.poke (); support.vision () }}
Support class:
Public class Support {public void vision () {System.out.println ("Auxiliary Vision");} / / Auxiliary returns to the city compensation state first, you first Don't overextend public void back () {Apc apc = new Apc (); Jungle jungle = new Jungle (); System.out.println ("Auxiliary temporarily returned to the city:"); apc.poke (); jungle.daye () }}
Client class:
Before using the test method, I don't think it's very good, so I'd like to add a client class to my friends in the future.
Public class Client {public static void main (String [] args) {Apc apc = new Apc (); Jungle jungle = new Jungle (); Support support = new Support (); apc.help (); jungle.gank (); support.back ();}}
It is true that various positions can cooperate with each other as we imagined, but each time we need to new other objects before cooperation, which is actually not conducive to loose coupling. In order to reduce the degree of coupling, the intermediary model appears.
Intermediary model
Of course, the requirements are still the above requirements, but there are a few more classes, abstract intermediaries, concrete intermediaries, abstract colleague classes, concrete colleague classes.
Mediator: the abstraction defines how participants interact.
Concrete Mediator
Colleague: this is an abstract class or interface that defines how participants who need mediation interact
Concrete Colleague
AbstractMediator class:
Public abstract class AbstractMediator {public abstract void help (); public abstract void gank (); public abstract void back ();}
Mediator class:
Public class Mediator extends AbstractMediator {/ / specific intermediary classes need to know who they mediate for Apc apc; Jungle jungle; Support support; public Mediator () {this.apc = new Apc (this); this.jungle = new Jungle (this); this.support = new Support (this);} public void help () {System.out.println ("mage request support") Jungle.daye (); support.vision ();} public void gank () {System.out.println ("start arresting people in the field:"); apc.poke (); support.vision ();} public void back () {System.out.println ("help temporarily return to the city:"); apc.poke (); jungle.daye ();}}
AbstractHero class (abstract colleague class):
Public abstract class AbstractHero {/ / has a relationship with a specific intermediary Mediator mediator; public AbstractHero (Mediator mediator) {this.mediator = mediator;}}
In order to reduce the space, I will only take the Jungle category for the specific hero category. Everything else is the same.
Jungle class (specific colleague class):
Public class Jungle extends AbstractHero {/ / first inject the intermediary public Jungle (Mediator mediator) {super (mediator);} / / what you do is still the same, you can also use several more methods public void daye () {System.out.println ("playing wild"). } / / when designing other people's affairs, when several people work together, you don't have to contact others yourself, just talk to the intermediary and give it to the intermediary to handle public void gank () {this.mediator.gank ();}}.
As for other colleagues, they are all the same, inject an intermediary and give the highly coupled things to the intermediary.
Client class:
Public class Client {public static void main (String [] args) {Mediator mediator = new Mediator (); Apc apc = new Apc (mediator); Jungle jungle = new Jungle (mediator); Support support = new Support (mediator); apc.help (); System.out.println ("-") Jungle.gank (); System.out.println ("- -"); support.back () }} Thank you for your reading. the above is the content of "what is the iterator pattern, the intermediary pattern and the Observer pattern". After the study of this article, I believe you have a deeper understanding of what is the iterator pattern, intermediary pattern and observer mode, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.