29 JAN 2020

template-method reflection

designp

Template method pattern

The Template Method pattern provides an outline of a series of steps for an algorithm. Objects that implement these steps retain the original structure of the algorithm but have the option to redefine or adjust certain steps. This pattern is designed to offer extensibility to the client developer.

Template Methods are frequently used in general purpose frameworks or libraries that will be used by other developer An example is an object that fires a sequence of events in response to an action, for example a process request. The object generates a ‘preprocess’ event, a ‘process’ event and a ‘postprocess’ event. The developer has the option to adjust the response to immediately before the processing, during the processing and immediately after the processing.

An easy way to think of Template Method is that of an algorithm with holes. It is up to the developer to fill these holes with appropriate functionality for each step.

Template method dofactory – link
Class code: Template method

Reflection in C#

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them

… full article

Class code: reflection demo #1
Class code: reflection demo #2

Template method + SQL + Reflection

Let’s try to build a generic mssql query using template method and reflection

Class code: MSSQL select
Class code: MSSQL select + generics + reflection

The next step will be to implement the Generic query into the template method pattern …

Links:

26 JAN 2020

singleton factory state recursion

designp

Design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects. The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral

Singleton pattern

The Singleton Pattern limits the number of instances of a particular object to just one. This single instance is called the singleton.

Singletons are useful in situations where system-wide actions need to be coordinated from a single central place. An example is a database connection pool. The pool manages the creation, destruction, and lifetime of all database connections for the entire application ensuring that no connections are ‘lost’.

Singleton dofactory – link
Class code: Singleton
Video: Singleton + design patterns intro [skip to 26:00]

Factory Method pattern

A Factory Method creates new objects as instructed by the client. One way to create objects is by invoking a constructor function with the new operator. There are situations however, where the client does not, or should not, know which one of several candidate objects to instantiate. The Factory Method allows the client to delegate object creation while still retaining control over which type to instantiate.

The key objective of the Factory Method is extensibility. Factory Methods are frequently used in applications that manage, maintain, or manipulate collections of objects that are different but at the same time have many characteristics (i.e. methods and properties) in common. An example would be a collection of documents with a mix of Xml documents, Pdf documents, and Rtf documents.

Factory Method dofactory – link
Class code: Factory method – simple
Class code: Factory method – advanced

Composite pattern

The Composite pattern allows the creation of objects with properties that are primitive items or a collection of objects. Each item in the collection can hold other collections themselves, creating deeply nested structures.

A tree control is a perfect example of a Composite pattern. The nodes of the tree either contain an individual object (leaf node) or a group of objects (a subtree of nodes).

All nodes in the Composite pattern share a common set of properties and methods which supports individual objects as well as object collections. This common interface greatly facilitates the design and construction of recursive algorithms that iterate over each object in the Composite collection.

Composite dofactory – link
Class code: Composite drawing app

State pattern

The State pattern provides state-specific logic to a limited set of objects in which each object represents a particular state. This is best explained with an example.

Say a customer places an online order for a TV. While this order is being processed it can in one of many states: New, Approved, Packed, Pending, Hold, Shipping, Completed, or Canceled. If all goes well the sequence will be New, Approved, Packed, Shipped, and Completed. However, at any point something unpredictable may happen, such as no inventory, breakage, or customer cancelation. If that happens the order needs to be handled appropriately.

Applying the State pattern to this scenario will provide you with 8 State objects, each with its own set of properties (state) and methods (i.e. the rules of acceptable state transitions). State machines are often implemented using the State pattern. These state machines simply have their State objects swapped out with another one when a state transition takes place.

Two other examples where the State pattern is useful include: vending machines that dispense products when a correct combination of coins is entered, and elevator logic which moves riders up or down depending on certain complex rules that attempt to minimize wait and ride times.

State dofactory – link
Class code: State – lightswitch

Recursion

a recursive definition is defined in terms of itself. Recursion is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition so that successive repetitions are processed up to the critical step where the condition is met at which time the rest of each repetition is processed from the last one called to the first.

Sometimes a problem is too difficult or too complex to solve because it is too big. If the problem can be broken down into smaller versions of itself, we may be able to find a way to solve one of these smaller versions and then be able to build up to a solution to the entire problem. This is the idea behind recursion; recursive algorithms break down a problem into smaller pieces which you either already know the answer to, or can solve by applying the same algorithm to each piece, and then combining the results.

Recursion : geeks-for-geeks
Class code: Fibonacci w/o recursion

Time complexity

the time complexity is the computational complexity that describes the amount of time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform.

… full article

Sometime, time complexity in a recursive solution may be significantly larger than a non-recursive solution..,.

Links:

05 JUN 2019

c#basics monitor-wait-pulse-pulseall design-patterns singleton

pulse

We saw how to Start( ) a thread and then Suspend( ) it. Why? Answer: so later on we can (in any point of time in the future) Resume this ready-to-go- thread without “paying” the initialization time. We saw the Pulse/Wait synchronization methodology.  How does it work? Answer: we can set one (or more) threads in a Wait( ) state (=blocking state) which will make the thread wait until a different thread calls the Pulse( ) method and releases the “waiting” thread. Pulse( ) will free only one Waiting thread. PulseAll( ) will free all of the Waiting threads. keep in mind that Wait/Pulse/PulseAll could only be called from a critical section in which is locked by the same key. In our example: we had a mobile technician department which are fixing phones, and a customer-service department which should call each customer after his phone has been fixed. Therefor, we had a thread in a Wait state (the customer-service), and a different thread for the technician which fixes the phone. after the technician completed to fix the phone- he Pulses the customer-service thread (which was in Wait state) – to release the thread and make him “call the customer”.

 

dp

What are design patterns? Answer: a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system. In the last part of the lesson we implemented the Singleton design pattern.

More topics covered:

  • Firing a thread in a Timer
  • Gang of Four

Links:

Design a site like this with WordPress.com
Get started