The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It was used in an image processing example to extract different processing logic for JPEG and GIF images into separate classes that implement a shared interface. This improved single responsibility and allowed new image types to be added without changing existing code.
2. Abstract
The strategy pattern:
• defines a family of algorithms,
• encapsulates each algorithm, and
• makes the algorithms interchangeable within that family.
• (https://en.wikipedia.org/wiki/Strategy_pattern)
3. In jip-en-janneketaal
• Having a class doing different things
• And conditional choices repeatedly switching between the same
set of variables
• Group the functionality for each variable into a new class
4. Example
if( code = ImageTypes.JPEG) then
begin
FImage.resize();
FImage.resample();
End
else if (code = ImageTypes.GIF) then
begin
FImage.decode();
FImage.resize();
Fimage.encode();
end
else throw ..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( code = ImageTypes.JPEG or code = ImageTypes.GIF) then
begin
FImage.doNormal(HashCode);
if( not Fimage.isNormalized and code = ImageTypes.JPEG)
then Fimage.doFFNormalMan(HashCode shl 12);
if(code = ImageTypes.GIF) then
begin
doSomeVectorStuff(code);
end
end
5. Example
if( code = ImageTypes.JPEG) then
begin
FImage.resize();
FImage.resample();
End
else if (code = ImageTypes.GIF) then
begin
FImage.decode();
FImage.resize();
Fimage.encode();
end
else throw ..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( code = ImageTypes.JPEG or code = ImageTypes.GIF) then
begin
FImage.doNormal(HashCode);
if( not Fimage.isNormalized and code = ImageTypes.JPEG)
then Fimage.doFFNormalMan(HashCode shl 12);
if(code = ImageTypes.GIF) then
begin
doSomeVectorStuff(code);
end
end
if( code = ImageTypes.JPEG) then
begin
FImage.resize();
FImage.resample();
End
else if (code = ImageTypes.GIF) then
begin
FImage.decode();
FImage.resize();
Fimage.encode();
end
else throw ..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( code = ImageTypes.JPEG or code = ImageTypes.GIF) then
begin
FImage.doNormal(HashCode);
if( not Fimage.isNormalized and code = ImageTypes.JPEG)
then Fimage.doFFNormalMan(HashCode shl 12);
if(code = ImageTypes.GIF) then
begin
doSomeVectorStuff(code);
end
end
6. Example
if( code = ImageTypes.JPEG) then
begin
FImage.resize();
FImage.resample();
End
else if (code = ImageTypes.GIF) then
begin
FImage.decode();
FImage.resize();
Fimage.encode();
end
else throw ..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( code = ImageTypes.JPEG or code = ImageTypes.GIF) then
begin
FImage.doNormal(HashCode);
if( not Fimage.isNormalized and code = ImageTypes.JPEG)
then Fimage.doFFNormalMan(HashCode shl 12);
if(code = ImageTypes.GIF) then
begin
doSomeVectorStuff(code);
end
end
if( code = ImageTypes.JPEG) then
begin
FImage.resize();
FImage.resample();
End
else if (code = ImageTypes.GIF) then
begin
FImage.decode();
FImage.resize();
Fimage.encode();
end
else throw ..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( code = ImageTypes.JPEG or code = ImageTypes.GIF) then
begin
FImage.doNormal(HashCode);
if( not Fimage.isNormalized and code = ImageTypes.JPEG)
then Fimage.doFFNormalMan(HashCode shl 12);
if(code = ImageTypes.GIF) then
begin
doSomeVectorStuff(code);
end
end
type IImageprocessor = Interface(Iinterface)
procedure DoNormal;
procedure Shrink;
end
type JPEGImageprocessor = class(IImageprocessor)
procedure DoNormal
begin
FImage.doNormal(HashCode);
Fimage.doFFNormalMan(HashCode shl 12);
end;
procedure Shrink
begin
FImage.resize();
FImage.resample();
end;
type GIFImageprocessor = class(IImageprocessor)
procedure DoNormal
begin
doSomeVectorStuff(code);
end;
procedure Shrink
begin
FImage.decode();
FImage.resize();
Fimage.encode();
end;
7. SOLID
• Single Responsibility Principle
• Each Algorithm has its own focus
• Not just doing one thing
• A class should have only one reason to change
8. Hey, I’m Jane and I’m
responsible for handling JPEG-
images Hi there, I’m Chantal and GIF-
images are my responsibility