Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC310 Lecture 09

CSC310 Lecture 09

Strategy, Decorator, Observer
(20260925)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

October 04, 2026

Transcript

  1. Agenda • This is Week 06 • Week 08 -

    Mond y October 12 - Midterm Review • Wednesd y October 14 - Midterm Ex m a a a 2
  2. Patterns in our Sprint 2 libraries 4 Builder XChart How

    do I construct a complex object clearly? Factory JFreeChart / Jzy3D Centralize object creation Strategy Tribuo Swap the algorithm Decorator Swing, Files, etc Wrap and augment behavior
  3. Example • InputStre m input = new FileInputStre m("d t

    .txt"); • → re ds from ile • Bu eredInputStre m inputBu ered = new Bu eredInputStre m(input); • → dds bu ering • D t InputStre m inputBu eredD t = new D t InputStre m(inputBu ered ); • → dds re dInt(), re dDouble(), re dBoole n(), etc. ff a a a a a a ff a a a a a ff ff a f a a a ff a a a a a a ff a 9
  4. • JButton button - new JButton("Click me") • JTextField inputText

    - new JTextField("Write here") • p nel. dd( notherP nel); • p nel. dd(button); • notherP nel. dd(inputText); a a a a a a a a a a a a a a 10 a a • JP nel p nel = new JP nel(); • JP nel notherP nel = new JP nel();
  5. • Both llow you to ch nge how n object

    beh ves. • The decor tor p ttern llows you to extend (decor te) the function lity of n object t runtime. a a a a a a a a a a a a a 18 a • Inherit nce dds beh vior t compil tion time. a a Decorator vs Inheritance
  6. Decorator abstract class Decorator extends Component { protected Component component;

    public void setComponent(Component component) { this.component = component; } @override public void operation() { if (component != null) { component.operation(); } } } 26 https://github.com/CSC3100/Patterns
  7. Main dr w dr w dr w a a a

    29 https://github.com/CSC3100/Patterns
  8. • Both llow you to ch nge how n object

    beh ves. • The decor tor p ttern llows you to extend (decor te) the function lity of n object t runtime. a a a a a a a a a a a a a 31 a • Inherit nce dds beh vior t compil tion time. a a Decorator vs Inheritance
  9. Important Do not reinvent the wheel! (Do not make another

    implementation for classes Observer or Observable -they already exist in libraries!) 43
  10. Singleton class Singleton{ private static Singleton _instance; // Constructor is

    'protected' protected Singleton() {} public static Singleton getInstance(){ if (_instance == null){ _instance = new Singleton(); } return _instance; } } https://github.com/CSC3100/Patterns 52
  11. Warning Do not Abuse of the Singleton You are transforming

    OOP into Structural Programming!! 🙈 54
  12. CSC 3100 Software Engineering Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2026

    Copyright. These slides may be used only as study material for CSC 5100 within the California State University system. They may not be distributed or used for any other purpose.