Head First design mode study notes-Observer mode + modifier Mode

Source: Internet
Author: User

The annotator mode is another design mode in JDK that uses a lot of resources. The last one is the observer mode (which is widely used in Swing). A good API design in the industry is inseparable from the common design mode, we usually refer to the source code to learn the design ideas of the experts. ---- Question
Design ModeObserver mode: defines one-to-multiple dependencies between objects. In this way, when an object changes state, all dependent objects are notified and automatically updated. Modifier mode: dynamically attaches the responsibility to the object. To expand the functionality, the decorator provides a more flexible alternative than the successor.
Design principles(1) Encapsulation changes. (2) multi-purpose combination, less inheritance. (3) programming for interfaces, rather than implementing programming. (4) design effort for loose coupling between interaction objects. The image-like dependency between objects is minimized, which helps your resume to have an elastic OO system and respond to changes. (5) class should be developed for extension and closed for modification. The goal is to allow classes to be easily extended. New behaviors can be combined without modifying the existing code.
Key PointsThe observer mode defines the one-to-many relationship between objects. When the observer mode is used, data can be pushed or pulled from the observer. In observer mode, the state of the topic and the number and type of the observer are changed. In this mode, you can change objects dependent on the topic State without changing the topic. Inheritance is one of the extensions, but it is not necessarily the best way to achieve elastic design. Combinations and delegation can be used to dynamically add new behaviors during runtime. The decorator Can add his or her own behavior before or after the behavior of the decorator, or even replace it to achieve its unique purpose. Decorator will lead to many small objects in the design. Excessive use will complicate the small program.
Java built-in observer Mode: Obverser interface and Obversable class. Disadvantages: (1) Obversable is a class and must be designed to inherit it. If you want to have the behavior of the Obversable class and another super class at the same time, it will be in a dilemma, because Java does not support multiple inheritance. Obversable has no interface and cannot establish its own implementation. (2) Obversable protects the setChange () method. In this way, an Obversable instance cannot be created and combined into its own object unless it is inherited.
Decorator of the real worldThere are too many classes in the java. io package. The following is a typical object set, which is used by the decorator to combine functions to read file data: superclass: InputStream components: FileInputStream, StringBufferInputStream, and ByteArrayInputStream abstract decorator: filterInputStream: PushBackInputStream, BufferedInputStream, DataInputStream, LineNumberInputStream
Observer mode:

Public interface Subject {public void registerObserver (Observer o); public void removeObserver (Observer o); public void policyobservers ();} public interface Observer {public void update (float temp, float humidity, float pressure);} public interface DispalyElement {public void dispaly ();} public class WeatherData implements Subject {private ArrayList observers; private float temperature; private float humidity; private float pressure; public WeatherData () {observers = new ArrayList ();} public void registerObserver (Observer o) {observers. add (o);} public void removeObserver (Observer o) {int I = observers. indexOf (o); if (I> = 0) {observers. remove (I) ;}// notify every observer of public void policyobservers () {for (int I = 0; I <observers. size (); I ++) {Observer observer = (Observer) observers. get (I); observer. update (temperature, humidity, pressure) ;}} public void measurementsChanged () {policyobservers ();} public void setMeasurements (float temperature, float humidity, float pressure) {this. temperature = temperature; this. humidity = humidity; this. pressure = pressure; measurementsChanged ();} public float getTemperature () {return temperature;} public float getHumidity () {return humidity;} public float getPressure () {return pressure ;}} // test code: public class WeatherStation {public static void main (String [] args) {WeatherData weatherData = new WeatherData (); CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay (weatherData); weatherData. setMeasurements (80, 65, 30.4f );}}



Decoration mode:
// Abstract class public abstract class Beverage {String description = "Unknown Beverage"; public String getDescription () {return description;} public abstract double cost ();} // CondimentDecorator must replace Beverage, so CondimentDecorator must replace Beverage, therefore, the Beverage class will be extended to the Beverage class public abstract class CondimentDecorator extends Beverage {// all the seasoning decorators must re-implement the getDescription () method public abstract String getDescription ();} // The Beverage code is extended from Beveragepublic class Espresso extends Beverage {public Espresso () {description = "Espresso" ;}public double cost () {return 1.99 ;}} // modifier public class Soy extends CondimentDecorator {Beverage beverage; // record the modifier public Soy (Beverage beverage) {this. beverage = beverage;} public String getDescription () {return beverage. getDescription () + ", Soy";} public double cost () {return. 15 + beverage. cost () ;}// test code: public class StarbuzzCoffee {public static void main (String args []) {Beverage beverage = new Espresso (); System. out. println (beverage. getDescription () + "$" + beverage. cost (); Beverage beverage2 = new DarkRoast (); beverage2 = new Mocha (beverage2); // layer-by-layer decoration beverage2 = new Mocha (beverage2 ); beverage2 = new Whip (beverage2); System. out. println (beverage2.getDescription () + "$" + response (); Beverage beverage3 = new HouseBlend (); beverage3 = new Soy (beverage3); beverage3 = new Mocha (beverage3 ); beverage3 = new Whip (beverage3); System. out. println (beverage3.getDescription () + "$" + beverage3.cost ());}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.