본문 바로가기

IT/ETC

Design ToolBox

반응형

object composition - composition simple defines HAS-A relation. For example, abstract factory pattern uses object composition, having many interfaces in the abstract factory showing a has-a relationship. 

 

OO Basics 

 - Abstraction

 - Encapsulation - wrapping up of data under a single unit. (varies variables under a class)

 - Polymorphism - the ability of an object to take on many forms. (person, employee, boss)

 - Inheritance

 

OO Principles 

 - Encapsulate what varies -

 

Take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don't. (chapter 1, duck flying and quack behavior)

(Hide the potentional change inside the interface. When things change, the function inside the interface might need to change but software written using the interface does not.) 

 

 - Favor composition over inheritance -

Inheritance is abstract concept while composition is actual existing component. Implementing a class cannot be changed during runtime while compositions can be changed and be used dynamically. (See design pattern strategy pattern's strategy part of strategy vs concept)

 

 - Program to interfaces(supertype), not implementations -

 

the declared type of the variables should be a supertype, usually an abstract class or interfact, so that the objects assigned to those variables can be of any concrete implementation of the supertype. 

 

Instead of creating a class and implementing it, create a seperate interface or abstract and sets of classes for certain behaviors. (ex. Duck flying behavior in chapter 1)

(use interface for polymorphism reasons etc not for its implementations. )

 

Programming to an implementation would be :

Dog d = new Dog();

 

Programing to an interface/supertype : 

Animal animal = new Dog(); 

 

assign the concrete implementation object a runtime :

a = getAnimal();

a.makeSound();

 

 - Strive for loosely coupled designs between objects that interact

 - Depend on abstractions. Do not depend on concrete classes. (Dependency Inversion Principle)

 

OO patterns 

 - Strategy : defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. 

 - Observer : defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 

 - Decorator : attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. 

 - Abstract Factory : Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

 - Factory Method : Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to the subclasses. 

- Singleton : Ensure a class only has one instance and provide a global point of access to it. 

반응형

'IT > ETC' 카테고리의 다른 글

Checked vs Unchecked  (0) 2020.03.05
Git  (0) 2020.03.04
Chapter 2  (0) 2020.02.28
Chapter 1  (0) 2020.02.27
HLS vs DASH  (0) 2020.02.24