21.2.07

OO-Basics and Patterns

Object-oriented programming is a programming paradigm that uses "objects" to design applications and computer programs.


  1. Abstraction: In object-oriented programming theory, abstraction is the facility to define objects that represent abstract "actors" that can perform work, report on and change their state, and "communicate" with other objects in the system.

  2. Encapsulation: The principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. Protecting a design decision involves providing a stable interface which shields the remainder of the program from the implementation (the details that are most likely to change).

  3. Polymorphism: is the ability of objects belonging to different types to respond to function calls of methods of the same name, each one according to an appropriate type-specific behaviour. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding). Polymorphism allows client programs to be written based only on the abstract interfaces of the objects which will be manipulated (interface inheritance). This means that future extension in the form of new types of objects is easy, if the new objects conform to the original interface. In particular, with object-oriented polymorphism, the original client program does not even need to be recompiled (only relinked) in order to make use of new types exhibiting new (but interface-conformant) behaviour (many pieces of code being controlled by shared control code).

  4. Inheritance: is a way to form new classes (instances of which are called objects) using classes that have already been defined. The new classes, known as derived classes, take over (or inherit) attributes and behaviour of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse of existing code with little or no modification. The main advantage of inheritance is that modules with sufficiently similar interfaces can share a lot of code, reducing the complexity of the program. Inheritance is also sometimes called generalization, because the is-a relationships represent a hierarchy between classes of objects.


Patterns

Patterns shows you how to build systems with OO design qualities. Good OO-Designs are reusable, extensible and maintainable. Patterns are proven OO-experience and they give general solutions to design problems, which can be applied to any specific application (they don't give the code).


Strategy Pattern

Strategy Pattern defines a family of algorthms, encapsulates each one and makes them interchangeable. Strategy lets the algorithm vary independentyl from clients that use it. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them. Each set of behaviour is thought as a family of algorithms, which are intercahngeable (algorithms can be selected on-the-fly at runtime).

The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure.

Observer Pattern

Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. An example should be a newspaper subscription service with its publisher (i.e. Subject Object) and subscribers (i.e. Observers, one-to-many relationship). The Observer Pattern provides an object design where subjects and observers are loosely coupled, which means;



  • Changes to either the subject or an observer will not affect each other,

  • Subjects or observers can be used independently of each other,

  • Subject need not to be modified in order to add new types of observers,

  • New observers can be add any time,

  • The only thing the subject knows about an observer is: observer implements a certain interface.


8.2.07

Milliseconds in C#

If you want to see how long it takes executing a function in .Net (C#), you can use either Millisecond function or format a DateTime object with "f"-"ffffff" pattern.
using System;
using System.Collections;

public class MyClass
{
public static void Main()
{
DateTime start;
String format1;
format1
= "HH:mm:ss.fff";
start
= DateTime.Now;

WL(
"ToLongTimeString and Millisecond " +
DateTime.Now.ToLongTimeString()
+ "." +
DateTime.Now.Millisecond);

WL(
"");
WL(start.ToString(format1));
WL(String.Format(
"{0:D}", DateTime.Now));
RL();
}

+
#region Helper methods

}

7.2.07

Open and create a text file in VB

strInputFile = "c:\input.txt"
strOutputFile = "c:\output.txt"

intInputFileNr = FreeFile
Open strInputFile For Input As #intInputFileNr
intOutputFileNr = FreeFile
Open strOutputFile For Output As #intOutputFileNr

While Not EOF(intInputFileNr)
Line Input #intInputFileNr, strGetLine
If strGetLine <> "" Then
strGetLine = Mid(strGetLine, InStr(strGetLine, Chr(9)) + 1)
strGetSplitted = Split(strGetLine, ";")

For intLocIO = 0 To UBound(strGetSplitted)
strWriteOut = strDatenSatz & ";" & strGetSplitted(intLocIO)
Print #intOutputFileNr, strWriteOut
Next

End If
Wend
Close #intOutputFileNr
Close #intInputFileNr

Register DLL and OCX Files from Context-Menu

This adds the ability to Right-Click on a .dll- or .ocx-File
and get the Register / UnRegister options, which then registers using the "regsvr32.exe"-Program.

Copy and paste the following lines in your text-editor, save as "RegisterDLL.reg"-file and then run the file with double-click.


REGEDIT4

; .DLL files
[HKEY_CLASSES_ROOT\.dll]
"Content Type"="application/x-msdownload"
@="dllfile"
[HKEY_CLASSES_ROOT\dllfile]
@="Application Extension"
[HKEY_CLASSES_ROOT\dllfile\Shell\Register\command]
@="regsvr32.exe \"%1\""
[HKEY_CLASSES_ROOT\dllfile\Shell\UnRegister\command]
@="regsvr32.exe /u \"%1\""

; .OCX files
[HKEY_CLASSES_ROOT\.ocx]
@="ocxfile"
[HKEY_CLASSES_ROOT\ocxfile]
@="OCX"
[HKEY_CLASSES_ROOT\ocxfile\Shell\Register\command]
@="regsvr32.exe \"%1\""
[HKEY_CLASSES_ROOT\ocxfile\Shell\UnRegister\command]
@="regsvr32.exe /u \"%1\""

Nvidia's GauGan App

NVIDIA's GauGAN AI Machine Learning Tool creates photorealistic images from Simple Hand Doodling http://nvidia-research-mingyuliu.com/...