Iterator Mode
Provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
Package com. ruishenh. designPatter. action. iterator; import java. util. arrayList; import java. util. list; public class IteratorClient {public static void main (String [] args) {ICollection
List = new Collection
(); List. add (new Module (1, "h1"); list. add (new Module (2, "h2"); list. add (new Module (3, "h3"); Iterator
Moduls = list. iterator (); while (moduls. hasNext () {Module m = moduls. next (); System. out. println (m. toString () ;}}// Iterator interface Iterator
{Boolean hasNext (); T next () ;}// aggregator interface icycler
{Boolean add (T t); boolean remove (T t);} // container role interface ICollection
Extends IAssembler
{Iterator
Iterator ();} // class Collection of a specific container role
Implements ICollection
{Private List
List; @ Override public Iterator
Iterator () {return new ContretelIterator
(This. list) ;}@ Override public boolean add (T t) {if (list = null) {list = new ArrayList
();} Return list. add (t) ;}@ Override public boolean remove (T t) {return list. remove (t) ;}// the specific iterator role class ContretelIterator
Implements Iterator
{Private transient List
List; public ContretelIterator (List
List) {this. list = list;} int cursor = 0; @ Override public boolean hasNext () {return cursor! = List. size () ;}@ Override public T next () {return list. get (cursor ++) ;}} class Module {public Module (int id, String name) {this. id = id; this. name = name;} int id; String name; public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Override public String toString () {return "id:" + id + ", name:" + name ;}}
The core task of the iterator mode separates operations and generation of data entities. containers are responsible for generating corresponding data, and the iterator is responsible for returning the set to users for one operation.
Observer Mode
Define one-to-multiple dependencies between objects. When the status of an object changes, all objects dependent on it are automatically updated.
Package com. ruishenh. designPatter. action. observer; import java. util. arrayList; import java. util. list; public class ObserverClient {public static void main (String [] args) {// initialize the Container and install the core program Container
, ICore> container = new Container
, ICore> (new Wash (); // container registration device 1 App1 a1 = new App1 (); container. add (a1); // container register device 1 App2 a2 = new App2 (); container. add (a2); // device operation a1.function (); a2.function (); // update the core program container. upgrade (new Clear (); // device operation a1.function (); a2.function () ;}// interface IDependent
{Void update (E e, String msg); void setCore (E, String msg);} // interface ICore {void function ();} class Wash implements ICore {@ Override public void function () {System. out. println ("wash clothes ...... ") ;}} class Clear implements ICore {@ Override public void function () {System. out. println ("Clear garbage ...... ") ;}} // manage the registered Container class Container
, E> {public Container (E e) {this. core = e; System. out. println ("initialize the core program...");} E core; List
List; String msg = "updated the latest program, updated from 1.0 to 1.1"; void upgrade (E core) {this. core = core; notfiy ();} void add (T t) {if (list = null) list = new ArrayList
(); List. add (t); t. setCore (core, "initialization program, current version 1.0");} void remove (T t) {if (list = null) return; list. remove (t);} void notfiy () {for (T t: list) {t. update (core, msg) ;}} class App1 implements IDependent
{ICore core; @ Override public void setCore (ICore e, String msg) {this. core = e; System. out. print ("I am an App1 program:"); System. out. println (msg) ;}@ Override public void update (ICore e, String msg) {System. out. print ("I am an App1 program:"); System. out. println (msg); this. core = e;} void function () {core. function () ;}} class App2 implements IDependent
{ICore core; @ Override public void setCore (ICore e, String msg) {this. core = e; System. out. print ("I am an App2 program:"); System. out. println (msg) ;}@ Override public void update (ICore e, String msg) {System. out. print ("I am an App2 program:"); System. out. println (msg); this. core = e;} void function () {core. function ();}}
The core task of the observer mode is to improve the maintainability of dependent theme changes.
When the dependent thing changes, other objects dependent on it can update the changes in a timely manner.
Template Method
Defines the skeleton of an algorithm in an operation and delays some steps to the subclass. TemplateMethod allows the subclass to redefine the algorithm without changing the structure of an algorithm.
Package com. ruishenh. designPatter. action. templateMethod; public class TemplateMethodClient {public static void main (String [] args) {Shop shop = new IceCreamShop (); shop. buy () ;}} abstract class Shop {// void buy () {pay (); take () ;} public abstract void pay (); public abstract void take ();} class IceCreamShop extends Shop {@ Override public void pay () {System. out. println ("payment for ice cream") ;}@ Override public void take () {System. out. println ("the waiter gave me ice cream ");}}
The core of the template method is to set the framework of one thing to be done, and some specific operations are left to its subclass for completion.