https://www.flickr.com/photos/hobo4evar/51859595715/
Wrap it up!
abstract class Duck {
quack() {}
fly() {}
}
class Turkey {
gobble() {}
fly() {} //only short distances
}
function runPondSim() {
const ducks: Duck[] = []; //things with the Duck interface
ducks.push(new Turkey()); //DOES NOT COMPILE :(
//Different interfaces
//... lots of work, including:
for(const duck of ducks) {
duck.quack();
duck.fly();
}
}
//is a duck!!
class TurkeyAdapter extends Duck {
constructor(private turkey: Turkey){}
quack() {
this.turkey.gobble();
}
fly() {
this.turkey.fly();
}
}
function runPondSim() {
const ducks: Duck[] = [];
ducks.push(new TurkeyAdapter(new Turkey())); // YAY!
for(const duck of ducks) {
duck.quack();
duck.fly();
}
}
Your task: In a group, research and learn one of the following patterns (assigned): Decorator (175), Facade (185), Proxy (207), or Mediator (273)
Each person in the group should be able to communicate:
Fill in the slides to help: https://docs.google.com/presentation/d/
19gmat_5dxgfIkIqJrhZ4hJGGfzZkIRrqeSvx49IR4cI/edit?usp=sharing
Your task: Reform (jigsaw) groups with one person who knows each pattern.
Teach your pattern to your new group!
function PrivateRoute(props) {
//...determine if user is logged in
if(userIsLoggedIn) { //show the normal route
return <Route {...props} />
}
else { //otherwise, redirect
return <Redirect to="/" />
}
}
function App(props) {
return (
<Switch>
<Route exact path="/">
<Static.WelcomePage user={currentUser} />
</Route>
<Route path="/signin">
<SignInPage user={currentUser} loginFunction={loginUser} />
</Route>
<PrivateRoute path="/profile" user={currentUser}>
<ProfilePage user={currentUser} />
</PrivateRoute>
</Switch>
)
}//https://www.robinwieruch.de/react-hooks-higher-order-components/
//higher-order functions
const withError = function(Component) {
return function(props) {
if (props.error) {
return <div>Something went wrong ...</div>;
}
return <Component {...props} />;
}
};
//combine functions
const DataTableWithFeedback = compose(withError, withLoading)(DataTable);
//render
function App(props) {
//...
return (
<DataTableWithFeedback columns={columns} data={data} error={error} isLoading={isLoading} />
);
};//example from old react-redux documentation!
const mapStateToProps = (state, ownProps) => ({
// ... computed data from state and optionally ownProps
})
const mapDispatchToProps = {
// ... normally is an object full of action creators
}
// `connect` returns a new function that accepts the component to wrap:
const connectToStore = connect(
mapStateToProps,
mapDispatchToProps
)
// and that function returns the connected, wrapper component:
const ConnectedComponent = connectToStore(Component)
// We normally do both in one step, like this:
connect(
mapStateToProps,
mapDispatchToProps
)(Component)Wear a hat
Wear a cape
Join a rock band