RSS

Category Archives: Design Patterns

HMVC -Examples-2-

HMVC part two with examples

For Experienced developers in codeigniter

Hello ,

Now that we have our HMVC enabled instance of CodeIgniter , if you don’t know how to do that please check the following link
First let’s examine some examples
We will made in this tutorial hmvc  example login module

1-  First create the “login” folder module in our “hmvc_ci/application” directory. It should end up looking like this in your web directory

  1. hmvc_ci/application/modules/login/models/
  2. hmvc_ci/application/modules/login/views/
  3. hmvc_ci/application/modules/login/controllers

2-  Second   Create the “site” folder module in our “hmvc_ci/application” directory. It should end up looking like this

  1. hmvc_ci/application/modules/site/models/
  2. hmvc_ci/application/modules/site/views/
  3. hmvc_ci/application/modules/site/controllers

3-    Copy this files to your login module you

Note  : can download source code for this code from here

  1. hmvc_ci/application/modules/login/models/membership_model.php
  2. hmvc_ci/application/modules/login/views/login_form.php
  3. hmvc_ci//application/modules/login/views/signup_form.php
  4. hmvc_ci//application/modules/login/views/signup_successful.php
  5. hmvc_ci//application/modules/login/controllers/login.php

4-   Next  copy this files into site module

hmvc_ci/application/modules/site/controllers/site.php
hmvc_ci/application/modules/site/views/logged_in_area.php

5-  Open “hmvc_ci/application/config/autoload.php” and edit it to look like the this:

 

If you have not already done so from step one, open “hmvc_ci/application/config/config.php” and edit it so that the base url is set to your appropriate location.

$config[‘base_url’]     = ‘http://localhost/hmvc_ci‘;

6-  Open “hmvc_ci/application/config/database.php” and add the appropriate links to your database.

[php]

$db[‘default’][‘hostname’] = “localhost”;                                       //  location of DB serverdb[‘default’]
[‘username’] = “YOUR USERNAME HERE”;                              // username you use to connect
$db[‘default’][‘password’] = “YOUR PASSWORD HERE”;       // associated password
$db[‘default’][‘database’] = “hmvc_ci”;                                         // The database you want to use

[/php]

7-   Create this table from sql in phpmyadmin

[php]

CREATE TABLE  `hmvc_ci`.`membership` (

`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`first_name` VARCHAR( 32 ) NOT NULL ,
`last_name` VARCHAR( 32 ) NOT NULL ,
`email_address` VARCHAR( 64 ) NOT NULL ,
`username` VARCHAR( 32 ) NOT NULL ,
`password` VARCHAR( 32 ) NOT NULL

) ENGINE = MYISAM ;

[/php]

8 – So we have our triads grouped, but we are still not quite in HMVC mode yet. In the site controller we find a function called is_logged_in() which check if user logged in or not .

[php]

function is_logged_in(){

$is_logged_in = $this->session->userdata(‘is_logged_in’);
if(!isset($is_logged_in) || $is_logged_in != true){

echo ‘You don’t have permission to access this page. <a href=”../login”>Login</a>’;
die();

}

}

[/php]

This is a login related function. In MVC mode, this is required because site cannot access login. With HMVC we can fix this.
In other way we collect related functions to other class and run what we need from it it’s good for big projects based on many views we handle that by hmvc and here we go i’ll demonstrate that in login function

  • Cut the is_logged_in() function out of “applications/modules/site/controllers/site.php
  • Save site.php without the is_logged_in() function.
  • Open “applications/modules/login/controllers/login.php“.
  • Paste the is_logged_in() function into the class.
  • Save login.php and code will be like that

9 – Open “applications/modules/site/controllers/site.php“.

 

Note : for CI 2.x like my examples login function should be like this

[php]

function is_logged_in($session){

$is_logged_in = $session->userdata(‘is_logged_in’);
if(!isset($is_logged_in) || $is_logged_in != true)
{

echo ‘You don’t have permission to access this page. <a href=”../login”>Login</a>’;
die();
//$this->load->view(‘login_form’);

}

}

[/php]

 

10-       Site controller  __construct should be like this

 

In chapter three we will demonstrate more example on HMVC for user profile member
after user login we will give every user control panel
Wait our demo !
Best regards 🙂

 

 
Leave a comment

Posted by on November 18, 2011 in Back-end, Design Patterns, Featured, Programming

 

Design Patterns

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Design patterns reside in the domain of modules and interconnections. At a higher level there are architectural patterns that are larger in scope, usually describing an overall pattern followed by an entire systemDesign patterns reside in the domain of modules and interconnections. At a higher level there are architectural patterns that are larger in scope, usually describing an overall pattern followed by an entire system

There are many types of design patterns:

>> Algorithm strategy patterns- addressing concerns related to high-level strategies describing how to exploit application characteristic on a computing platform.

>> Computational design patterns- addressing concerns related to key computation identification.

>> Execution patterns- that address concerns related to supporting application execution, including strategies in executing streams of tasks and building blocks to support task synchronization .

>>Implementation strategy patterns- addressing concerns related to implementing source code to support: >

1.program organization

2.the common data structures specific to parallel programming.

>>Structural design patterns- addressing concerns related to high-level structures of applications being develop

 Domain-specific patterns Efforts have also been made to codify design patterns in particular domains, including use of existing design patterns as well as domain specific design patterns. Examples include user interface design patterns,information visualization, secure design, “secure usability”, Web design and business model design.

 
Leave a comment

Posted by on July 23, 2011 in Design Patterns, Featured, Software

 

‫‪Factory Pattern‬‬

‫‪Factory Pattern‬‬
‫اول :- الموضوع بالعربى يعنى هتقراه هتقراه ما لكش حجة‬
‫ً‬
‫ثانيا :- الموضوع شيق جدا وسهل جدا جدا ..
‫ثالثا :- الكود مترقم عشان سعادتك لو مفهمتش اى سطر تبقى تسأل برقم السطر‬
‫الموضوع بيتكلم عن حاجة اسمها ‪ Factory pattern‬يبقى أكيد متعلق بموضوع ال ‪Design Pattern‬‬
‫برافو عليك هو ده بالظبط اللى كنت عايز اقولهولك‬
‫يبقى إيه هو ال ‪ Factory Pattern‬ده :-‬
‫هو زى ما بيقولوا “لكل شخص من اسمه نصيب ” يعنى فع ً مصنع بس بيصنع )‪ ( Objects‬مش‬
‫ل‬
‫فاهمك ياض‬
‫بص ياسيدى الراجل ده اللى هو ال ‪ Factory‬عنده ‪ method‬اسمها ‪ getClass‬انت هتنادى على الميثود‬
‫دى وتقولها هاتيلى أوبجكت من الكلس اللى اسمها كذا تروح هيا على طول جايبالك الوبجكت اللى‬
‫انت قولت عليه )ما شاء ال عليها شطورة وبتسمع الكلم (‬
‫طيب الميثود دى هتعمل الموضوع ده إزاى وهتجيب الوبجكت منين – بعيد عن السحر والشعوذة -‬
‫تعالى ياسيدى نتخيل الموقف التالى‬

‫– إيه بقى يا سيدى الموقف اللى تخيلناه ده إن شاء ال -‬
‫– اهدى عليا الموضوع وما فيه إنى عندى كلس اسمها ‪ X‬عندى ليها كلستين وارثينها بالحيا‬
‫أل وهم ‪ XY‬و ‪XZ‬‬
‫خلى بالك‬

    حاول دايما ما تخليش الكلاس بتاعتك – فخفخينا – ما تحطش الكود اللى فى البرنامج كله فى الكلاس (ما تقلقش انا كنت بعمل كده برده ) دايما تخلى الكلاس بتاعتك متخصصة فى فى الحاجات اللى هى بتعملها يعنى على قد ما تقدر افصل الكود عن بعضه

    و ده يا سيدى مبدأ فى الDesign الناس المفترية دول بيقولولك حاول دايما إنك تقطع العلاقات” – والعياذ بالله – بين الكلاسات وبعضها وده عشان إن الكود ما يكنش معتمد على بعضه وشوية كلام تانى ربنا يسهل ونبقى نتكلم عنه بعدين

وعندى كمان كلاس اللى هيا أساس الليلة كلها الـ Factory ودى فيها ميثود ودى بقى أساس اساس الليلة getClass الميثود دى بتاخد arguements اللى هم الـ abc اللى فى الشكل اللى ملهمش محل من الإعراب لحد دلوقتى . وعلى طول الميثود دى بناء على الـ arguements دى بتطلعلك الكلاس اللى تتناسب مع الموقف اللى سعادتك فيه.

لو لسه ما اتخنقتش ولسه عايش والحمد لله وصحتك كويسة خش على المثال اللى جاى بقى

تخيل ياسيدى – تانى ؟ إن اننا شغالين فى بروجكت كبير حبتين بيقوم بمهمة عظيمة حبتين ألا وهى إنك بتدخل له اسم واحد فهو بيعرف الاسم الأول والاسم الأخير لوحده – ومالك متقل على نفسك أوى كده – وهو بيعمل الكلام ده سواء الرجل كتب الاسم الأول وبعدين الأخير أو العكس إذا كتب الاسم الأخير الأول وبعدين الاسم الأول – طبعاً لازم تبين إنت كاتب أنهى اسم الأول عشان البرنامج مش معاه شجرة العيلة يا حبيبى

يعنى يا إما هيكون بالشكل ده “firstname lastname” أو بالشكل ده “lastname, firstname”

يبقى من الآخر الـ Comma هى سيدة الموقف أوعى تقولى بقى ولازمتها إيه من الاول وديزين باترن وواجع دماغنا من الصبح دا يخلص فى سطر كود – استر عليا ربنا يستر على ولاياك –

نيجى بقى لأول كلاس


زى ما انت شايف الكلاس بتشرح نفسها

ودى بقى الكلاس اللى كان اسمها فى الشكل اللى فوق X

زى ما قلنا أنها موروثة بالحيا وهيا هنا معاها ولدين

واحد اسمه FirstFirst وده اللى هيشتغل لما يكون الراجل كاتب اسمه زى البنى آدمين يعنى الاسم الأول وبعدين الأخير

والتانى اسمه LastFirst وده لما يكون الراجل حالة متشقلب وكاتب الاسم الأخير الأول وبعدين الاسم الأول – طبعا فاصل بينهم بـ Comma –

تعالى بقى نشوف أول كلاس

‫هنا بقى ياسيدى أول حاجة الراجل ده بيعملها إنه بياخد السم فى الـ ‪ Constructior‬بتاعه ويفصل‬
‫الأسم الأول عن الخير والكلس دى هنستخدمها فى حالة ما إذا كان الرجل كاتب الأسم الأول‬
‫وبعدين الأخير‬

وهنا ياسيدى الكلاس الثانى

وهنا لما يكون الراجل مشقلب نفسه يعنى كاتب الاسم الأخير وبعدين الأول الكود هيشوف الـ Comma فين ويقطع مره من قبلها ومرة من بعدها

يلا بينا نبنى المصنع ( همتكم يا رجالة)

هنا بقى ياعم الحج الراجل بياخد الاسم الكريم بتاع سعادتك ويشوف لو فيه Comma يجرى ينادى على الكلاس المتشقلبة ولو مفيهوش ينادى على الكلاس اللى مش متشقلبة وبكده يبقى انهينا المهمة – استنى انهيت إيه الكود هيشتغل من إين يا اسطى

نيجى بقى للكلاس الى هتشغل الكود ده كله

هتلاحظ هنا إن اسمك لما بتكتبه بأى شكل بيطبع مظبوط وبكده يبقى احنا فعلاً أنهينا المهمة

Ashraf Fouda

eng.ashraf.fouda@gmail.com

 
Leave a comment

Posted by on February 10, 2011 in Design Patterns, Software

 

N-Tier Architecture

N-Tier Architecture

كلما زادت تعقيدات ومتطلبات وظائف المشروع يصبح مطلب إلزامي الاعتماد على التطبيقات المتعددة الطبقاتN-Tierفي هذا المقال أحاول تقديم هذا المفهوم للمبتدئين

.

 

من الصورة المغرية السابقة واضح انى هشرح طريقة عمل الهامبورجر …. فعلا هى دى الحقيقة مش بس لانه من الرموز الصارخة للعولمة ولكن لانه اقرب مثال عملى من واقع الحياة على التصميم متعدد الطبقات :d

اولا نبدأ المكونات خبز + تشكيلة متنوعة من الخضروات + شريحة من اللحم

ثانيا الطريقة :-

السفلية،تبدأ الطبقة العلوية Upper Tier بقطعة من الخبز التي تمثل الواجهة العلوية والمغلفة للساندويتش وهي نفس الطبقة

ومن ثم طبقة تحتوي على تشكيلة متنوعة من الخضروات Vegetable Tier،

وبعدها الطبقة الرئيسية (طبقة شريحة اللحم Meat Tier وهي أهم ما في الوجود (المحرك الأساسي للنظام( Core Engine)

طيب هنستفاد بايه من الطريقة دى ؟؟

ولا طريقة عمل الساندويتش بالشكل ده مفيدة جدا جدا وطبعا مش من منظور الطعم او الشكل

  1. Specialisation او مفهوم التخصص بمعنى ان كل طبقة من الطبقات المنفصلة عن بعضها اقدر احصل عليها من متخصص ليها وده طبعا هيزود الجودة والكفاءة الانتاجية

مثال .. طبقة الخبز مثلا هحصل عليها من الخباز ببساطة لانه شخص ذو خبرة عالية ودراية بالدقيق ومكونات الخبز وكذلك الخضروات ممكن نعتمد على أحد المراكز التي تعرض أنقى وأفضل أنواع الخضراوات الطازجة، وكذلك الحال مع طبقة اللحم، يمكننا أن نتعامل مع مطبخ يأتي يوميا بلحوم طازجة ذبحت للتو، وبذلك تزيد الجودة Quality للمنتج بشكل خرافي بسبب تخصص كل شخص في بناء الطبقة المناسبة، ويبقى علينا تجميع الطبقات ومن ثم بيعها للمستخدم النهائي.

  1. Debugging او مفهوم تعديل المنتج مثلا هنحاول نعمل نسخة أخرى خفيفة من المنتج لاشخاص بيعملوا دايت على سبيل المثال (على افتراض ان حد بيعمل دايت وياكل همبورجر: d) كل اللى هنعمله هنغير طبقة الخبز العادى بطبقة خبز أسمر من غير ما اضطر لتعديل اى طبقة تانية.
  2. Internationalization او Localizations او مراعاة القواعد الاقليمية والدولية فالمسألة لن تتعدى طبقة اللحم Meat Tier بمعنى ان المسلمين لا يأكلون لحوم الخنزير أو زيوت من مشتقاته، وكذلك اليهود ، فلابد أن تكون طريقة ذبح اللحم حلال Halal، وعند الهندوز فلا يمكن أن يكون لحم بقري (فهو محرم أكلها بعقيدتهم) ، كل ما نحتاجه تعديل في طبقة اللحم فقط وتبقى باقى الطبقات كما هى دون تغيير.
  3. واخر حاجة عند ظهور شوائب Bugs او أخطاء في هذا المنتج، فيمكن إصلاح المشكلة في الطبقة التي ينتمي لها ((فقط)) دون ان تتأثر ودون أن نحتاج الى تعديل الطبقات الاخرى.

ماذا لو ؟؟

تعالوا نتخيل ان الهامبورجر بالشكل ده نفس المكونات بالظبط بس بدون طبقات

.

هنا أي تعديل او تطوير او صيانة ستستهلك جهد ووقت اضافي (إن لم تكن مستحيلة)، بل سنحتاج الى اعادة البناء من جديد عند التعديلات الجوهرية، وكل المزايا التي عرضناها بالفقرات السابقة واختصار الوقت وتوفير الاموال كلها لا تصلح بالشكل ده وبالتالى اى تعديل ولو بسيط يحتاج تغيير كامل للمنتج.

بعيدا عن الهامبرجر

يقال أنه توجد علاقة عكسية بين عمل العقل وعمل المعدة، فعندما تعمل المعدة يتعطل العقل، وبما أني بحاجة ماسة لاستهلاك كل طاقة عقلك الإنتاجية، فسأتوقف هنا عن عروض الهامبرجر

Multitier architecture

(Often referred to as n-tier architecture) is a client–server architecture in which the presentation, the application processing, and the data management are logically separate processes.

Presentation tier

This is the topmost level of the application. The presentation tier displays information related to such services as browsing merchandise, purchasing, and shopping cart contents. It communicates with other tiers by outputting results to the browser/client tier and all other tiers in the network.

Application tier

(Business logic, logic tier, data access tier, or middle tier)The logic tier is pulled out from the presentation tier and, as its own layer; it controls an application’s functionality by performing detailed processing.

Data tier

This tier consists of database servers. Here information is stored and retrieved. This tier keeps data neutral and independent from application servers or business logic. Giving data its own tier also improves scalability and performance.

The benefits of the 3-tier model:

  1. Scalability: The key 3-tier benefit is improved scalability since the application servers can be deployed on many machines. Also, the database no longer requires a connection from every client — it only requires connections from a smaller number of application servers. In addition, TP monitors or ORBs can be used to balance loads and dynamically manage the number of application server(s) available.
  2. Better Re-use: The same logic can be initiated from many clients or applications. If an object standard like COM/DCOM or CORBA is employed (as discussed in tool dependence), then the specific language implementation of the middle tier can be made transparent.
  3. Improved Data Integrity: since all updates go through the middle tier, the middle tier can ensure that only valid data is allowed to be updated in the database and the risk of a rogue client application corrupting data is removed.
  4. Improved Security: Security is improved since it can be implemented at multiple levels (not just the database). Security can be granted on a service-by-service basis. Since the client does not have direct access to the database, it is more difficult for a client to obtain unauthorized data. Business logic is generally more secure since it is placed on a more secure central server.
  5. Reduced Distribution: Changes to business logic only need to be updated on the application servers and do not have to be distributed to all the clients.
  6. Improved Availability: mission-critical applications can make use of redundant application servers and redundant database servers. With redundant servers, it is possible to architect an application so that it can recover from network or server failures.
  7. Hidden Database Structure: since the actual structure of the database is hidden from the caller, it is possible that many database changes can be made transparently. Therefore, a service in the middle tier that exchanges information/data with other applications could retain its original interface while the underlying database structure was enhanced during a new application release.

The drawbacks of the 3-tier model:

  1. Increased Complexity/Effort: In general, it is more difficult to build a 3-tier application compared to a 2-tier application. The points of communication are doubled (client to middle tier to server, instead of simply client to server) and many handy productivity enhancements provided by client tools (e.g. Visual Basic, PowerBuilder, Delphi) will be foregone or their benefit will be reduced with 3-tier architecture.
  1. Fewer Tools: There are many more tools available for a 2-tier model (e.g. most reporting tools). It is likely that additional programming effort will be required to manage tasks that an automated tool might handle in a 2-tier environment.

Model–View–Controller

Model–View–Controller (MVC) is a software architecture, currently considered an architectural pattern used in software engineering. The pattern isolates “domain logic” (the application logic for the user) from the user interface (input and presentation), permitting independent development, testing and maintenance of each (separation of concerns).

The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react.

The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.

The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

Comparison with the 3-tier architecture

The three tiers may seem similar to the model-view-controller (MVC) concept. However, topologically they are different. A fundamental rule in three tier architecture is the client tier never communicates directly with the data tier; in a three-tier model all communication must pass through the middleware tier. Conceptually the three-tier architecture is linear. However, the MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model.

 
Leave a comment

Posted by on December 9, 2010 in Design Patterns, Software

 

Decorator pattern

Asalam Alaikum,

This is Saad Talaat how are you hope you are fine.

This post will be about Design patterns. and we will be talking about the Decorator pattern.

The Decorator Pattern is about Decorating(*) an Object with another so object will be Like “Nested” in each others so to get out a value from an object we need to count all values that are in other Objects that Decorate the the object we need to get the value out of.
Read the rest of this entry »

 
2 Comments

Posted by on August 29, 2010 in Design Patterns, Software